Skip to content

Commit c6693cc

Browse files
committed
Enforce properly structured rules with ConfigurationBuilder.
1 parent c78cadd commit c6693cc

12 files changed

Lines changed: 388 additions & 20 deletions

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.ocpsoft.rewrite.config.Rule;
77
import org.ocpsoft.rewrite.config.RuleBuilder;
88
import org.ocpsoft.rewrite.context.ContextBase;
9+
import org.ocpsoft.rewrite.context.EvaluationContext;
10+
import org.ocpsoft.rewrite.event.Rewrite;
911

1012
public class ClassContextImpl extends ContextBase implements ClassContext
1113
{
@@ -31,7 +33,27 @@ public void setBaseRule(Rule rule)
3133
public RuleBuilder getRuleBuilder()
3234
{
3335
if (configurationRuleBuilder == null) {
34-
configurationRuleBuilder = config.addRule();
36+
configurationRuleBuilder = config.addRule(new Rule() {
37+
@Override
38+
public void otherwise(Rewrite event, EvaluationContext context)
39+
{}
40+
41+
@Override
42+
public void perform(Rewrite event, EvaluationContext context)
43+
{}
44+
45+
@Override
46+
public boolean evaluate(Rewrite event, EvaluationContext context)
47+
{
48+
return true;
49+
}
50+
51+
@Override
52+
public String getId()
53+
{
54+
return null;
55+
}
56+
});
3557
}
3658
return configurationRuleBuilder.getRuleBuilder();
3759
}

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,71 @@ public void testBuildConfigurationPerformAndOtherwiseNegative() throws Exception
173173
Assert.assertTrue(performedOtherwise);
174174
}
175175

176+
@Test
177+
public void testFluentAPI() throws Exception
178+
{
179+
ConfigurationBuilder.begin()
180+
.addRule()
181+
.perform(operation).addRule();
182+
183+
ConfigurationBuilder.begin()
184+
.addRule()
185+
.perform(operation)
186+
.withPriority(0).addRule();
187+
188+
ConfigurationBuilder.begin()
189+
.addRule()
190+
.perform(operation)
191+
.withId("id")
192+
.withPriority(0).addRule();
193+
194+
ConfigurationBuilder.begin()
195+
.addRule()
196+
.perform(operation)
197+
.otherwise(otherwise)
198+
.withId("id")
199+
.withPriority(0).addRule();
200+
201+
ConfigurationBuilder.begin()
202+
.addRule()
203+
.when(new False())
204+
.perform(operation)
205+
.otherwise(otherwise)
206+
.withId("id")
207+
.withPriority(0).addRule();
208+
209+
ConfigurationBuilder.begin()
210+
.addRule()
211+
.when(new False())
212+
.perform(operation)
213+
.otherwise(otherwise)
214+
.withPriority(0)
215+
.withId("id").addRule();
216+
217+
ConfigurationBuilder.begin()
218+
.addRule()
219+
.when(new False())
220+
.otherwise(otherwise)
221+
.withPriority(0)
222+
.withId("id").addRule();
223+
224+
ConfigurationBuilder.begin()
225+
.addRule()
226+
.when(new False())
227+
.otherwise(otherwise)
228+
.withId("id")
229+
.withPriority(0).addRule();
230+
231+
ConfigurationBuilder.begin()
232+
.addRule()
233+
.when(new False())
234+
.otherwise(otherwise)
235+
.withPriority(0).addRule();
236+
237+
ConfigurationBuilder.begin()
238+
.addRule()
239+
.when(new False())
240+
.otherwise(otherwise).addRule();
241+
}
242+
176243
}

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,10 +20,10 @@
2020

2121
/**
2222
* A fluent builder for defining {@link Configuration} objects.
23-
*
23+
*
2424
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2525
*/
26-
public class ConfigurationBuilder implements Configuration
26+
public class ConfigurationBuilder implements Configuration, ConfigurationBuilderRoot
2727
{
2828
private final List<Rule> rules = new ArrayList<Rule>();
2929

@@ -44,19 +44,15 @@ public static ConfigurationBuilder begin()
4444
return new ConfigurationBuilder();
4545
}
4646

47-
/**
48-
* Define a new fluent {@link Rule}
49-
*/
50-
public ConfigurationRuleBuilder addRule()
47+
@Override
48+
public ConfigurationRuleBuilderCustom addRule()
5149
{
5250
RuleBuilder rule = RuleBuilder.define();
5351
rules.add(rule);
5452
return new ConfigurationRuleBuilder(this, rule);
5553
}
5654

57-
/**
58-
* Add a pre-defined {@link Rule}.
59-
*/
55+
@Override
6056
public ConfigurationRuleBuilder addRule(final Rule rule)
6157
{
6258
RuleBuilder wrapped = RuleBuilder.wrap(rule);
@@ -69,5 +65,5 @@ public String toString()
6965
{
7066
return "ConfigurationBuilder [rules=" + rules + "]";
7167
}
72-
68+
7369
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2013 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.config;
17+
18+
/**
19+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
20+
*/
21+
public interface ConfigurationBuilderRoot extends Configuration
22+
{
23+
/**
24+
* Define a new fluent {@link Rule}
25+
*/
26+
public ConfigurationRuleBuilderCustom addRule();
27+
28+
/**
29+
* Add a pre-defined {@link Rule}
30+
*/
31+
public ConfigurationRuleBuilder addRule(final Rule rule);
32+
}

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2+
* Copyright 2013 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,18 @@
1818
import java.util.List;
1919

2020
/**
21-
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
21+
* An intermediate stage {@link Rule} configuration.
2222
*
23+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2324
*/
24-
public class ConfigurationRuleBuilder extends ConfigurationBuilder
25+
public class ConfigurationRuleBuilder extends ConfigurationBuilder implements
26+
ConfigurationRuleBuilderCustom,
27+
ConfigurationRuleBuilderWhen,
28+
ConfigurationRuleBuilderPerform,
29+
ConfigurationRuleBuilderOtherwise,
30+
ConfigurationRuleBuilderWithId,
31+
ConfigurationRuleBuilderWithPriority,
32+
ConfigurationRuleBuilderWithPriorityAndId
2533
{
2634
private final ConfigurationBuilder wrapped;
2735
private final RuleBuilder rule;
@@ -39,14 +47,15 @@ public ConfigurationRuleBuilder addRule(final Rule rule)
3947
}
4048

4149
@Override
42-
public ConfigurationRuleBuilder addRule()
50+
public ConfigurationRuleBuilderCustom addRule()
4351
{
4452
return wrapped.addRule();
4553
}
4654

4755
/**
4856
* Set the {@link Condition} of this {@link Rule} instance.
4957
*/
58+
@Override
5059
public ConfigurationRuleBuilder when(final Condition condition)
5160
{
5261
rule.when(condition);
@@ -56,6 +65,7 @@ public ConfigurationRuleBuilder when(final Condition condition)
5665
/**
5766
* Perform the given {@link Operation} when the conditions set in this {@link Rule} are met.
5867
*/
68+
@Override
5969
public ConfigurationRuleBuilder perform(final Operation operation)
6070
{
6171
rule.perform(operation);
@@ -65,6 +75,7 @@ public ConfigurationRuleBuilder perform(final Operation operation)
6575
/**
6676
* Perform the given {@link Operation} when the conditions set in this {@link Rule} fail to be met.
6777
*/
78+
@Override
6879
public ConfigurationRuleBuilder otherwise(final Operation operation)
6980
{
7081
rule.otherwise(operation);
@@ -76,23 +87,25 @@ public ConfigurationRuleBuilder otherwise(final Operation operation)
7687
* {@link ConfigurationProvider} from which this rule was returned, then relocate this rule to its new priority
7788
* position in the compiled rule set.
7889
*/
90+
@Override
7991
public ConfigurationRuleBuilder withPriority(int priority)
8092
{
8193
rule.withPriority(priority);
8294
return this;
8395
}
8496

8597
/**
86-
* Set the ID for the current rule.
98+
* Set the ID for the current {@link Rule}. This may be used in logging and for rule lookup purposes.
8799
*/
100+
@Override
88101
public ConfigurationRuleBuilder withId(String id)
89102
{
90103
rule.withId(id);
91104
return this;
92105
}
93106

94107
/**
95-
* Provides access to the rule builder for the current rule.
108+
* Provides access to the {@link RuleBuilder} for the current {@link Rule}.
96109
*/
97110
public RuleBuilder getRuleBuilder()
98111
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2013 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.config;
17+
18+
/**
19+
* An intermediate stage {@link Rule} configuration.
20+
*
21+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
22+
*/
23+
public interface ConfigurationRuleBuilderCustom
24+
{
25+
/**
26+
* Set the {@link Condition} of this {@link Rule} instance.
27+
*/
28+
ConfigurationRuleBuilderWhen when(Condition condition);
29+
30+
/**
31+
* Perform the given {@link Operation} when the conditions set in this {@link Rule} are met.
32+
*/
33+
ConfigurationRuleBuilderPerform perform(Operation condition);
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2013 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.config;
17+
18+
/**
19+
* An intermediate stage {@link Rule} configuration.
20+
*
21+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
22+
*/
23+
public interface ConfigurationRuleBuilderOtherwise extends ConfigurationBuilderRoot
24+
{
25+
26+
/**
27+
* Set the ID for the current {@link Rule}. This may be used in logging and for rule lookup purposes.
28+
*/
29+
ConfigurationRuleBuilderWithId withId(String id);
30+
31+
/**
32+
* Set the priority of this {@link Rule} instance. If {@link #priority()} differs from the priority of the
33+
* {@link ConfigurationProvider} from which this rule was returned, then relocate this rule to its new priority
34+
* position in the compiled rule set.
35+
*/
36+
ConfigurationRuleBuilderWithPriority withPriority(int priority);
37+
}

0 commit comments

Comments
 (0)