Skip to content

Commit 755a894

Browse files
committed
Added visitor support for conditions
1 parent 03a2468 commit 755a894

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2011 <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+
* This class allows {@link Visitor}s to walk through a {@link Condition} tree which may contain
20+
* {@link CompositeCondition} elements.
21+
*
22+
* @author Christian Kaltepoth
23+
*/
24+
public class ConditionVisit
25+
{
26+
27+
private final Condition root;
28+
29+
/**
30+
* Initialize class with the supplied root {@link Condition}
31+
*/
32+
public ConditionVisit(Condition root)
33+
{
34+
this.root = root;
35+
}
36+
37+
/**
38+
* Submit the supplied visitor and apply it to all conditions in the tree
39+
*/
40+
public void accept(Visitor<Condition> visitor)
41+
{
42+
visit(root, visitor);
43+
}
44+
45+
/**
46+
* Method to call the visitor for the supplied condition and recursively calls itself, if the condition is a
47+
* {@link CompositeCondition}.
48+
*/
49+
private void visit(Condition condition, Visitor<Condition> visitor)
50+
{
51+
52+
// visit the condition itself
53+
visitor.visit(condition);
54+
55+
// recursive call for all children if a CompositeCondition
56+
if (condition instanceof CompositeCondition) {
57+
for (Condition child : ((CompositeCondition) condition).getConditions()) {
58+
visit(child, visitor);
59+
}
60+
}
61+
}
62+
63+
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,15 @@ else if (!(operation instanceof DefaultOperationBuilder))
145145

146146
return (DefaultOperationBuilder) operation;
147147
}
148+
149+
/**
150+
* This method will call the supplied visitor for all conditions attached to the rule builder.
151+
*
152+
* @param visitor visitor to process
153+
*/
154+
public void accept(Visitor<Condition> visitor)
155+
{
156+
new ConditionVisit(condition).accept(visitor);
157+
}
158+
148159
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2011 <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+
* General purpose visitor interface
20+
*
21+
* @author Christian Kaltepoth
22+
*/
23+
public interface Visitor<E>
24+
{
25+
26+
/**
27+
* Visit the supplied object
28+
*/
29+
void visit(E e);
30+
31+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2011 <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+
import static org.junit.Assert.assertEquals;
19+
20+
import java.util.HashSet;
21+
import java.util.Set;
22+
23+
import org.junit.Test;
24+
25+
public class ConditionVisitTest
26+
{
27+
28+
@Test
29+
public void testConditionVisit()
30+
{
31+
32+
// setup condition tree
33+
Condition condition1 = new True();
34+
Condition condition2 = new True();
35+
Condition condition3 = new True();
36+
Condition root = And.all(condition1, Or.any(condition2, condition3));
37+
38+
// set of visited conditions for verification
39+
final Set<Condition> visited = new HashSet<Condition>();
40+
41+
// visit the tree
42+
new ConditionVisit(root).accept(new Visitor<Condition>() {
43+
@Override
44+
public void visit(Condition e)
45+
{
46+
visited.add(e);
47+
}
48+
});
49+
50+
// all 5 conditions should have been visited (3x True, 1x And, 1x Or)
51+
assertEquals(5, visited.size());
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)