Skip to content

Commit cff52e3

Browse files
committed
Reverted & Refactored .otherwise() to use negated rule appending instead of Operation splitting.
1 parent a5a01bc commit cff52e3

22 files changed

Lines changed: 157 additions & 266 deletions

File tree

‎annotations-impl/src/main/java/org/ocpsoft/rewrite/annotation/context/ClassContextImpl.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ public RuleBuilder getRuleBuilder()
3434
{
3535
if (configurationRuleBuilder == null) {
3636
configurationRuleBuilder = config.addRule(new Rule() {
37-
@Override
38-
public void otherwise(Rewrite event, EvaluationContext context)
39-
{}
40-
4137
@Override
4238
public void perform(Rewrite event, EvaluationContext context)
4339
{}

‎api-servlet/src/main/java/org/ocpsoft/rewrite/servlet/http/HttpRewriteProvider.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@
2525
/**
2626
* A {@link org.ocpsoft.rewrite.spi.RewriteProvider} that only operates on {@link HttpServletRequest} and
2727
* {@link HttpServletResponse} request cycle types.
28-
*
28+
*
2929
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3030
*/
31-
public abstract class HttpRewriteProvider extends
32-
ServletRewriteProvider<HttpServletRewrite>
31+
public abstract class HttpRewriteProvider extends ServletRewriteProvider<HttpServletRewrite>
3332
{
3433
@Override
3534
public boolean handles(final Rewrite event)

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
/**
3434
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
35-
*
35+
*
3636
*/
3737
public class ConfigurationBuilderTest
3838
{
@@ -64,12 +64,12 @@ public void perform(final Rewrite event, final EvaluationContext context)
6464
};
6565
}
6666

67-
private void execute(Rule rule)
67+
private void execute(Configuration config)
6868
{
69-
if (rule.evaluate(rewrite, context))
70-
rule.perform(rewrite, context);
71-
else
72-
rule.otherwise(rewrite, context);
69+
for (Rule rule : config.getRules()) {
70+
if (rule.evaluate(rewrite, context))
71+
rule.perform(rewrite, context);
72+
}
7373
}
7474

7575
@Test
@@ -82,8 +82,7 @@ public void testBuildConfigurationPerformOnly()
8282
.when(And.all(Direction.isInbound(), new True()))
8383
.perform(operation);
8484

85-
Rule rule = config.getRules().get(0);
86-
execute(rule);
85+
execute(config);
8786

8887
Assert.assertTrue(performed);
8988
Assert.assertFalse(performedOtherwise);
@@ -99,8 +98,7 @@ public void testBuildConfigurationPerformOnlyNegative()
9998
.when(And.all(new False()))
10099
.perform(operation);
101100

102-
Rule rule = config.getRules().get(0);
103-
execute(rule);
101+
execute(config);
104102

105103
Assert.assertFalse(performed);
106104
Assert.assertFalse(performedOtherwise);
@@ -116,8 +114,7 @@ public void testBuildConfigurationOtherwiseOnly()
116114
.when(new False())
117115
.otherwise(otherwise);
118116

119-
Rule rule = config.getRules().get(0);
120-
execute(rule);
117+
execute(config);
121118

122119
Assert.assertFalse(performed);
123120
Assert.assertTrue(performedOtherwise);
@@ -133,8 +130,7 @@ public void testBuildConfigurationOtherwiseNegative()
133130
.when(new True())
134131
.otherwise(otherwise);
135132

136-
Rule rule = config.getRules().get(0);
137-
execute(rule);
133+
execute(config);
138134

139135
Assert.assertFalse(performed);
140136
Assert.assertFalse(performedOtherwise);
@@ -152,8 +148,7 @@ public void testBuildConfigurationPerformAndOtherwise() throws Exception
152148
.perform(operation)
153149
.otherwise(otherwise);
154150

155-
Rule rule = config.getRules().get(0);
156-
execute(rule);
151+
execute(config);
157152

158153
Assert.assertTrue(performed);
159154
Assert.assertFalse(performedOtherwise);
@@ -171,8 +166,7 @@ public void testBuildConfigurationPerformAndOtherwiseNegative() throws Exception
171166
.perform(operation)
172167
.otherwise(otherwise);
173168

174-
Rule rule = config.getRules().get(0);
175-
execute(rule);
169+
execute(config);
176170

177171
Assert.assertFalse(performed);
178172
Assert.assertTrue(performedOtherwise);
@@ -181,13 +175,19 @@ public void testBuildConfigurationPerformAndOtherwiseNegative() throws Exception
181175
@Test
182176
public void testSubsetAPI() throws Exception
183177
{
184-
ConfigurationBuilder.begin()
178+
Configuration config = ConfigurationBuilder.begin()
185179

186180
.addRule()
187-
.when(Path.matches("/admin"))
188-
.perform(Subset.when(JAASRoles.required("admin"))
189-
.otherwise(SendError.code(401)))
190-
.otherwise(SendStatus.code(201));
181+
.when(new False())
182+
.otherwise(Subset.when(ConfigurationBuilder.begin()
183+
.addRule()
184+
.when(new True())
185+
.perform(operation)));
186+
187+
execute(config);
188+
189+
Assert.assertTrue(performed);
190+
Assert.assertFalse(performedOtherwise);
191191
}
192192

193193
@Test

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
public interface CompositeCondition extends Condition
2727
{
2828
/**
29-
* Return all {@link Condition} objects contained within this {@link CompositeCondition}.
29+
* Returns all {@link Condition} objects contained within this {@link CompositeCondition}; otherwise, returns an
30+
* empty {@link List}.
3031
*/
3132
public List<Condition> getConditions();
3233
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919

2020
/**
2121
* An {@link Operation} that aggregates one or more operations.
22-
*
22+
*
2323
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
24-
*
24+
*
2525
*/
2626
public interface CompositeOperation extends Operation
2727
{
2828
/**
29-
* Return all {@link Operation} objects contained within this {@link CompositeOperation}.
29+
* Returns all {@link Operation} objects contained within this {@link CompositeOperation}; otherwise, returns an
30+
* empty {@link List}.
3031
*/
3132
public List<Operation> getOperations();
3233
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
/**
2323
* An intermediate stage {@link Rule} configuration.
24-
*
24+
*
2525
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2626
*/
2727
public class ConfigurationRuleBuilder extends ConfigurationBuilder implements
@@ -89,7 +89,10 @@ public ConfigurationRuleBuilder perform(final Operation operation)
8989
@Override
9090
public ConfigurationRuleBuilder otherwise(final Operation operation)
9191
{
92-
rule.otherwise(operation);
92+
wrapped.addRule()
93+
.when(Not.any(rule))
94+
.perform(operation);
95+
9396
return this;
9497
}
9598

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ private void enrichParameters(ParameterEnricher enricher)
178178

179179
// enrich conditions
180180
Visitor<Condition> conditionVisitor = new ConditionParameterEnricher(parameter, enricher);
181-
new ConditionVisit(ruleBuilder.getCondition()).accept(conditionVisitor);
181+
new ConditionVisit(ruleBuilder).accept(conditionVisitor);
182182

183183
// enrich operations
184184
Visitor<Operation> operationVisitor = new OperationParameterEnricher(parameter, enricher);
185-
new OperationVisit(ruleBuilder.getOperation()).accept(operationVisitor);
185+
new OperationVisit(ruleBuilder).accept(operationVisitor);
186186

187187
}
188188

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2222
*/
23-
public interface Rule extends Condition, Operation, Otherwise
23+
public interface Rule extends Condition, Operation
2424
{
2525
/**
2626
* Get the ID for this {@link Rule}

0 commit comments

Comments
 (0)