CaseBuilder returns "this" SwitchInterceptor from the then() method, so it can't be static. Needs to have a reference to its enclosing SwitchInterceptor.
On Mon, Jul 29, 2013 at 11:12 AM, Matt Benson <gudnabr...@gmail.com> wrote: > CaseBuilder could be static as well. > > Matt > > > On Mon, Jul 29, 2013 at 10:10 AM, <jcar...@apache.org> wrote: > >> Author: jcarman >> Date: Mon Jul 29 15:10:07 2013 >> New Revision: 1508094 >> >> URL: http://svn.apache.org/r1508094 >> Log: >> PROXY-20: Changing API around a bit to be more "fluent" >> >> Modified: >> commons/proper/proxy/branches/version-2.0-work/ (props changed) >> >> commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java >> >> commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java >> >> Propchange: commons/proper/proxy/branches/version-2.0-work/ >> >> ------------------------------------------------------------------------------ >> --- svn:ignore (original) >> +++ svn:ignore Mon Jul 29 15:10:07 2013 >> @@ -5,3 +5,5 @@ >> target >> >> commons-proxy2-parent.iml >> + >> +.idea >> >> Modified: >> commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java >> URL: >> http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java?rev=1508094&r1=1508093&r2=1508094&view=diff >> >> ============================================================================== >> --- >> commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java >> (original) >> +++ >> commons/proper/proxy/branches/version-2.0-work/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java >> Mon Jul 29 15:10:07 2013 >> @@ -17,6 +17,8 @@ >> >> package org.apache.commons.proxy2.interceptor; >> >> +import org.apache.commons.lang3.tuple.ImmutablePair; >> +import org.apache.commons.lang3.tuple.Pair; >> import org.apache.commons.proxy2.Interceptor; >> import org.apache.commons.proxy2.Invocation; >> >> @@ -36,7 +38,7 @@ public class SwitchInterceptor implement >> // Fields >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> >> - private final List<Case> cases = new CopyOnWriteArrayList<Case>(); >> + private final List<Pair<InvocationMatcher, Interceptor>> cases = new >> CopyOnWriteArrayList<Pair<InvocationMatcher, Interceptor>>(); >> >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> // Constructors >> @@ -46,11 +48,6 @@ public class SwitchInterceptor implement >> { >> } >> >> - public SwitchInterceptor(InvocationMatcher matcher, Interceptor >> interceptor) >> - { >> - cases.add(new Case(matcher, interceptor)); >> - } >> - >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> // Interceptor Implementation >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> @@ -58,11 +55,11 @@ public class SwitchInterceptor implement >> @Override >> public Object intercept(Invocation invocation) throws Throwable >> { >> - for (Case currentCase : cases) >> + for (Pair<InvocationMatcher, Interceptor> currentCase : cases) >> { >> - if(currentCase.matcher.matches(invocation)) >> + if (currentCase.getLeft().matches(invocation)) >> { >> - return currentCase.interceptor.intercept(invocation); >> + return currentCase.getRight().intercept(invocation); >> } >> } >> return invocation.proceed(); >> @@ -72,25 +69,28 @@ public class SwitchInterceptor implement >> // Other Methods >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> >> - public SwitchInterceptor onCase(InvocationMatcher matcher, >> Interceptor interceptor) >> + public CaseBuilder when(InvocationMatcher matcher) >> { >> - cases.add(new Case(matcher, interceptor)); >> - return this; >> + return new CaseBuilder(matcher); >> } >> >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> // Inner Classes >> >> >> //---------------------------------------------------------------------------------------------------------------------- >> >> - private static final class Case implements Serializable >> + public class CaseBuilder >> { >> - private InvocationMatcher matcher; >> - private Interceptor interceptor; >> + private final InvocationMatcher matcher; >> >> - private Case(InvocationMatcher matcher, Interceptor interceptor) >> + public CaseBuilder(InvocationMatcher matcher) >> { >> this.matcher = matcher; >> - this.interceptor = interceptor; >> + } >> + >> + public SwitchInterceptor then(Interceptor interceptor) >> + { >> + cases.add(new ImmutablePair<InvocationMatcher, >> Interceptor>(matcher, interceptor)); >> + return SwitchInterceptor.this; >> } >> } >> } >> >> Modified: >> commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java >> URL: >> http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java?rev=1508094&r1=1508093&r2=1508094&view=diff >> >> ============================================================================== >> --- >> commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java >> (original) >> +++ >> commons/proper/proxy/branches/version-2.0-work/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java >> Mon Jul 29 15:10:07 2013 >> @@ -34,8 +34,8 @@ public class TestSwitchInterceptor exten >> public void testWithMultipleAdvices() throws Throwable >> { >> SwitchInterceptor interceptor = new SwitchInterceptor(); >> - interceptor.onCase(new MethodNameMatcher("echo"), new >> ConstantInterceptor("bar")); >> - interceptor.onCase(new MethodNameMatcher("echoBack"), new >> ConstantInterceptor("baz")); >> + interceptor.when(new MethodNameMatcher("echo")).then(new >> ConstantInterceptor("bar")); >> + interceptor.when(new MethodNameMatcher("echoBack")).then(new >> ConstantInterceptor("baz")); >> Method method = Echo.class.getMethod("echoBack", String.class); >> Invocation invocation = new MockInvocation(method, "foo", "foo"); >> assertEquals("baz", interceptor.intercept(invocation)); >> @@ -51,7 +51,7 @@ public class TestSwitchInterceptor exten >> >> public void testWithSingleAdviceWhichDoesNotMatch() throws Throwable >> { >> - SwitchInterceptor interceptor = new SwitchInterceptor(new >> MethodNameMatcher("echoBackZZZZ"), new ConstantInterceptor("bar")); >> + SwitchInterceptor interceptor = new SwitchInterceptor().when(new >> MethodNameMatcher("echoBackZZZZ")).then(new ConstantInterceptor("bar")); >> Method method = Echo.class.getMethod("echoBack", String.class); >> Invocation invocation = new MockInvocation(method, "foo", "foo"); >> assertEquals("foo", interceptor.intercept(invocation)); >> @@ -59,7 +59,7 @@ public class TestSwitchInterceptor exten >> >> public void testWithSingleAdviceWhichMatches() throws Throwable >> { >> - SwitchInterceptor interceptor = new SwitchInterceptor(new >> MethodNameMatcher("echoBack"), new ConstantInterceptor("bar")); >> + SwitchInterceptor interceptor = new SwitchInterceptor().when(new >> MethodNameMatcher("echoBack")).then(new ConstantInterceptor("bar")); >> Method method = Echo.class.getMethod("echoBack", String.class); >> Invocation invocation = new MockInvocation(method, "foo", "foo"); >> assertEquals("bar", interceptor.intercept(invocation)); >> >> >> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org For additional commands, e-mail: dev-h...@commons.apache.org