Skip to content

Commit f654d88

Browse files
committed
Added RuleVisit API
1 parent a14957a commit f654d88

3 files changed

Lines changed: 93 additions & 3 deletions

File tree

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+
import java.util.List;
19+
20+
/**
21+
* Defines a composite {@link Rule}, which can return other {@link Rule} instances contained within.
22+
*
23+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
24+
*/
25+
public interface CompositeRule extends Rule
26+
{
27+
/**
28+
* Returns a list of {@link Rule} instances contained by this {@link CompositeRule}.
29+
*/
30+
public List<Rule> getRules();
31+
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ public void accept(Visitor<Condition> visitor)
5050
*/
5151
private void visit(Condition condition, Visitor<Condition> visitor)
5252
{
53-
54-
// visit the condition itself
5553
visitor.visit(condition);
5654

57-
// recursive call for all children if a CompositeCondition
5855
if (condition instanceof CompositeCondition) {
5956
for (Condition child : ((CompositeCondition) condition).getConditions()) {
6057
visit(child, visitor);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 org.ocpsoft.rewrite.util.Visitor;
19+
20+
/**
21+
* This class allows {@link Visitor} instances to walk through a {@link Rule} tree which may contain
22+
* {@link CompositeCondition} or {@link CompositeOperation} elements containing other rules.
23+
*
24+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
25+
*/
26+
public class RuleVisit
27+
{
28+
29+
private final Rule root;
30+
31+
/**
32+
* Initialize class with the supplied root {@link Rule}
33+
*/
34+
public RuleVisit(Rule root)
35+
{
36+
this.root = root;
37+
}
38+
39+
/**
40+
* Submit the supplied {@link Visitor} and apply it to all {@link Rule} instances in the tree
41+
*/
42+
public void accept(Visitor<Rule> visitor)
43+
{
44+
visit(root, visitor);
45+
}
46+
47+
/**
48+
* Method to call the {@link Visitor} for the supplied {@link Rule} and recursively calls itself, if the {@link Rule}
49+
* is a {@link CompositeRule}.
50+
*/
51+
private void visit(Rule rule, Visitor<Rule> visitor)
52+
{
53+
visitor.visit(rule);
54+
55+
if (rule instanceof CompositeRule) {
56+
for (Rule child : ((CompositeRule) rule).getRules()) {
57+
visit(child, visitor);
58+
}
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)