PROXY-20: Changing API around a bit to be more "fluent"
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/proxy/branches/version-2.0-work@1508094 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java b/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
index 56b463d..fe7eff5 100644
--- a/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
+++ b/core/src/main/java/org/apache/commons/proxy2/interceptor/SwitchInterceptor.java
@@ -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 @@
// 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 SwitchInterceptor(InvocationMatcher matcher, Interceptor interceptor)
- {
- cases.add(new Case(matcher, interceptor));
- }
-
//----------------------------------------------------------------------------------------------------------------------
// Interceptor Implementation
//----------------------------------------------------------------------------------------------------------------------
@@ -58,11 +55,11 @@
@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 @@
// 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;
}
}
}
diff --git a/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java b/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
index cc4c260..9de7759 100644
--- a/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
+++ b/core/src/test/java/org/apache/commons/proxy2/interceptor/TestSwitchInterceptor.java
@@ -34,8 +34,8 @@
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 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 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));