Skip to content

Commit 122eb48

Browse files
committed
Add .when(cond, cond, cond), .perform(op, op, op), .otherwise(op, op, op) to ConfigurationBuilder API
1 parent c960607 commit 122eb48

5 files changed

Lines changed: 206 additions & 25 deletions

File tree

‎api-tests/src/test/java/org/ocpsoft/rewrite/config/ConfigurationBuilderTest.java‎

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
*/
3838
public class ConfigurationBuilderTest
3939
{
40+
private int performCount = 0;
41+
private int performOtherwiseCount = 0;
4042
private boolean performed = false;
4143
private boolean performedOtherwise = false;
4244
private Operation operation;
@@ -53,13 +55,15 @@ public void before()
5355
@Override
5456
public void perform(final Rewrite event, final EvaluationContext context)
5557
{
58+
performCount++;
5659
performed = true;
5760
}
5861
};
5962
otherwise = new Operation() {
6063
@Override
6164
public void perform(final Rewrite event, final EvaluationContext context)
6265
{
66+
performOtherwiseCount++;
6367
performedOtherwise = true;
6468
}
6569
};
@@ -73,6 +77,22 @@ private void execute(Configuration config)
7377
}
7478
}
7579

80+
@Test
81+
public void testBuildConfigurationCompositeWhenPerformOnly()
82+
{
83+
Assert.assertFalse(performed);
84+
Assert.assertFalse(performedOtherwise);
85+
86+
Configuration config = ConfigurationBuilder.begin().addRule()
87+
.when(Direction.isInbound(), new True())
88+
.perform(operation);
89+
90+
execute(config);
91+
92+
Assert.assertTrue(performed);
93+
Assert.assertFalse(performedOtherwise);
94+
}
95+
7696
@Test
7797
public void testBuildConfigurationPerformOnly()
7898
{
@@ -89,6 +109,24 @@ public void testBuildConfigurationPerformOnly()
89109
Assert.assertFalse(performedOtherwise);
90110
}
91111

112+
@Test
113+
public void testBuildConfigurationPerformMultipleOnly()
114+
{
115+
Assert.assertFalse(performed);
116+
Assert.assertFalse(performedOtherwise);
117+
118+
Configuration config = ConfigurationBuilder.begin().addRule()
119+
.when(And.all(Direction.isInbound(), new True()))
120+
.perform(operation, operation, operation);
121+
122+
execute(config);
123+
124+
Assert.assertEquals(3, performCount);
125+
Assert.assertEquals(0, performOtherwiseCount);
126+
Assert.assertTrue(performed);
127+
Assert.assertFalse(performedOtherwise);
128+
}
129+
92130
@Test
93131
public void testBuildConfigurationPerformOnlyNegative()
94132
{
@@ -105,6 +143,22 @@ public void testBuildConfigurationPerformOnlyNegative()
105143
Assert.assertFalse(performedOtherwise);
106144
}
107145

146+
@Test
147+
public void testBuildConfigurationCompositeConditionPerformOnlyNegative()
148+
{
149+
Assert.assertFalse(performed);
150+
Assert.assertFalse(performedOtherwise);
151+
152+
Configuration config = ConfigurationBuilder.begin().addRule()
153+
.when(new True(), new False())
154+
.perform(operation);
155+
156+
execute(config);
157+
158+
Assert.assertFalse(performed);
159+
Assert.assertFalse(performedOtherwise);
160+
}
161+
108162
@Test
109163
public void testBuildConfigurationOtherwiseOnly()
110164
{
@@ -121,6 +175,40 @@ public void testBuildConfigurationOtherwiseOnly()
121175
Assert.assertTrue(performedOtherwise);
122176
}
123177

178+
@Test
179+
public void testBuildConfigurationCompositeConditionOtherwiseOnly()
180+
{
181+
Assert.assertFalse(performed);
182+
Assert.assertFalse(performedOtherwise);
183+
184+
Configuration config = ConfigurationBuilder.begin().addRule()
185+
.when(new True(), new False())
186+
.otherwise(otherwise);
187+
188+
execute(config);
189+
190+
Assert.assertFalse(performed);
191+
Assert.assertTrue(performedOtherwise);
192+
}
193+
194+
@Test
195+
public void testBuildConfigurationOtherwiseMultipleOnly()
196+
{
197+
Assert.assertFalse(performed);
198+
Assert.assertFalse(performedOtherwise);
199+
200+
Configuration config = ConfigurationBuilder.begin().addRule()
201+
.when(new False())
202+
.otherwise(otherwise, otherwise);
203+
204+
execute(config);
205+
206+
Assert.assertEquals(0, performCount);
207+
Assert.assertEquals(2, performOtherwiseCount);
208+
Assert.assertFalse(performed);
209+
Assert.assertTrue(performedOtherwise);
210+
}
211+
124212
@Test
125213
public void testBuildConfigurationOtherwiseNegative()
126214
{
@@ -137,6 +225,22 @@ public void testBuildConfigurationOtherwiseNegative()
137225
Assert.assertFalse(performedOtherwise);
138226
}
139227

228+
@Test
229+
public void testBuildConfigurationCompositeConditionOtherwiseNegative()
230+
{
231+
Assert.assertFalse(performed);
232+
Assert.assertFalse(performedOtherwise);
233+
234+
Configuration config = ConfigurationBuilder.begin().addRule()
235+
.when(new True(), new True())
236+
.otherwise(otherwise);
237+
238+
execute(config);
239+
240+
Assert.assertFalse(performed);
241+
Assert.assertFalse(performedOtherwise);
242+
}
243+
140244
@Test
141245
public void testBuildConfigurationPerformAndOtherwise() throws Exception
142246
{
@@ -218,15 +322,14 @@ public void testMetadataAPI() throws Exception
218322
.withMetadata(null, null)
219323
.addRule();
220324

221-
222325
ConfigurationBuilder.begin().addRule()
223326
.when(new False())
224327
.perform(operation)
225328
.withId(null)
226329
.withPriority(12)
227330
.withMetadata(null, null)
228331
.addRule();
229-
332+
230333
ConfigurationBuilder.begin().addRule()
231334
.when(new False())
232335
.perform(operation)
@@ -352,6 +455,10 @@ public void testFluentAPI() throws Exception
352455
.addRule()
353456
.perform(operation).addRule();
354457

458+
ConfigurationBuilder.begin()
459+
.addRule()
460+
.perform(operation, operation).addRule();
461+
355462
ConfigurationBuilder.begin()
356463
.addRule()
357464
.perform(operation)
@@ -370,6 +477,20 @@ public void testFluentAPI() throws Exception
370477
.withId("id")
371478
.withPriority(0).addRule();
372479

480+
ConfigurationBuilder.begin()
481+
.addRule()
482+
.perform(operation, operation)
483+
.otherwise(otherwise, otherwise)
484+
.withId("id")
485+
.withPriority(0).addRule();
486+
487+
ConfigurationBuilder.begin()
488+
.addRule()
489+
.perform(operation)
490+
.otherwise(otherwise, otherwise)
491+
.withId("id")
492+
.withPriority(0).addRule();
493+
373494
ConfigurationBuilder.begin()
374495
.addRule()
375496
.when(new False())

‎api/src/main/java/org/ocpsoft/rewrite/config/ConfigurationRuleBuilder.java‎

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
*/
1616
package org.ocpsoft.rewrite.config;
1717

18+
import java.util.Arrays;
1819
import java.util.HashSet;
20+
import java.util.LinkedList;
1921
import java.util.List;
2022
import java.util.Set;
2123

2224
import org.ocpsoft.rewrite.context.EvaluationContext;
2325
import org.ocpsoft.rewrite.event.Rewrite;
24-
import org.ocpsoft.rewrite.param.Parameter;
2526
import org.ocpsoft.rewrite.param.ParameterStore;
2627
import org.ocpsoft.rewrite.param.Parameterized;
2728
import org.ocpsoft.rewrite.param.ParameterizedRule;
@@ -62,38 +63,56 @@ public ConfigurationRuleBuilderCustom addRule()
6263
return wrapped.addRule();
6364
}
6465

65-
/**
66-
* Configure the {@link Parameter} with the given name.
67-
*/
6866
@Override
6967
public ConfigurationRuleParameterBuilder where(String name)
7068
{
7169
return new ConfigurationRuleParameterBuilder(this, rule.where(name));
7270
}
7371

74-
/**
75-
* Set the {@link Condition} of this {@link Rule} instance.
76-
*/
7772
@Override
7873
public ConfigurationRuleBuilder when(final Condition condition)
7974
{
8075
rule.when(condition);
8176
return this;
8277
}
8378

84-
/**
85-
* Perform the given {@link Operation} when the conditions set in this {@link Rule} are met.
86-
*/
79+
@Override
80+
public ConfigurationRuleBuilderWhen when(Condition condition, Condition... conditions)
81+
{
82+
List<Condition> list = new LinkedList<Condition>();
83+
list.add(condition);
84+
list.addAll(Arrays.asList(conditions));
85+
rule.when(And.all(list.toArray(new Condition[list.size()])));
86+
return this;
87+
}
88+
8789
@Override
8890
public ConfigurationRuleBuilder perform(final Operation operation)
8991
{
9092
rule.perform(operation);
9193
return this;
9294
}
9395

94-
/**
95-
* Perform the given {@link Operation} when the conditions set in this {@link Rule} fail to be met.
96-
*/
96+
@Override
97+
public ConfigurationRuleBuilderPerform perform(Operation operation, Operation... operations)
98+
{
99+
OperationBuilder builder = Operations.wrap(operation);
100+
if (operation instanceof OperationBuilder)
101+
{
102+
builder = (OperationBuilder) operation;
103+
}
104+
105+
if (operations != null)
106+
{
107+
for (int i = 0; i < operations.length; i++) {
108+
builder = builder.and(operations[i]);
109+
}
110+
}
111+
112+
rule.perform(builder);
113+
return this;
114+
}
115+
97116
@Override
98117
public ConfigurationRuleBuilder otherwise(final Operation operation)
99118
{
@@ -104,21 +123,36 @@ public ConfigurationRuleBuilder otherwise(final Operation operation)
104123
return this;
105124
}
106125

107-
/**
108-
* Set the priority of this {@link Rule} instance. If {@link #priority()} differs from the priority of the
109-
* {@link ConfigurationProvider} from which this rule was returned, then relocate this rule to its new priority
110-
* position in the compiled rule set.
111-
*/
126+
@Override
127+
public ConfigurationRuleBuilderOtherwise otherwise(Operation operation, Operation... operations)
128+
{
129+
OperationBuilder builder = Operations.wrap(operation);
130+
if (operation instanceof OperationBuilder)
131+
{
132+
builder = (OperationBuilder) operation;
133+
}
134+
135+
if (operations != null)
136+
{
137+
for (int i = 0; i < operations.length; i++) {
138+
builder = builder.and(operations[i]);
139+
}
140+
}
141+
142+
wrapped.addOtherwiseRule(rule)
143+
.when(Not.any(rule))
144+
.perform(builder);
145+
146+
return this;
147+
}
148+
112149
@Override
113150
public ConfigurationRuleBuilder withPriority(int priority)
114151
{
115152
rule.withPriority(priority);
116153
return this;
117154
}
118155

119-
/**
120-
* Set the ID for the current {@link Rule}. This may be used in logging and for rule lookup purposes.
121-
*/
122156
@Override
123157
public ConfigurationRuleBuilder withId(String id)
124158
{

‎api/src/main/java/org/ocpsoft/rewrite/config/ConfigurationRuleBuilderCustom.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,19 @@ public interface ConfigurationRuleBuilderCustom
2727
*/
2828
ConfigurationRuleBuilderWhen when(Condition condition);
2929

30+
/**
31+
* Set the {@link Condition}s of this {@link Rule} instance. This creates an implicit {@link And} between all
32+
* provided {@link Condition} instances.
33+
*/
34+
ConfigurationRuleBuilderWhen when(Condition condition, Condition... conditions);
35+
3036
/**
3137
* Perform the given {@link Operation} when the conditions set in this {@link Rule} are met.
3238
*/
33-
ConfigurationRuleBuilderPerform perform(Operation condition);
39+
ConfigurationRuleBuilderPerform perform(Operation operation);
40+
41+
/**
42+
* Perform the given {@link Operation} instances when the conditions set in this {@link Rule} are met.
43+
*/
44+
ConfigurationRuleBuilderPerform perform(Operation operation, Operation... operations);
3445
}

‎api/src/main/java/org/ocpsoft/rewrite/config/ConfigurationRuleBuilderPerform.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public interface ConfigurationRuleBuilderPerform extends ConfigurationBuilderRoo
3030
*/
3131
ConfigurationRuleBuilderOtherwise otherwise(Operation condition);
3232

33+
/**
34+
* Perform the given {@link Operation} instances when the conditions set in this {@link Rule} fail to be met.
35+
*/
36+
ConfigurationRuleBuilderOtherwise otherwise(Operation operation, Operation ... operations);
37+
3338
/**
3439
* Set the ID for the current {@link Rule}. This may be used in logging and for rule lookup purposes.
3540
*/

‎api/src/main/java/org/ocpsoft/rewrite/config/ConfigurationRuleBuilderWhen.java‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ public interface ConfigurationRuleBuilderWhen
2828
*/
2929
ConfigurationRuleBuilderPerform perform(Operation condition);
3030

31+
/**
32+
* Perform the given {@link Operation} instances when the conditions set in this {@link Rule} are met.
33+
*/
34+
ConfigurationRuleBuilderPerform perform(Operation operation, Operation ... operations);
35+
3136
/**
3237
* Perform the given {@link Operation} when the conditions set in this {@link Rule} fail to be met.
3338
*/
34-
ConfigurationRuleBuilderOtherwise otherwise(Operation condition);
39+
ConfigurationRuleBuilderOtherwise otherwise(Operation operation);
40+
41+
/**
42+
* Perform the given {@link Operation} instances when the conditions set in this {@link Rule} fail to be met.
43+
*/
44+
ConfigurationRuleBuilderOtherwise otherwise(Operation operation, Operation ... operations);
3545
}

0 commit comments

Comments
 (0)