|
| 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 | +} |
0 commit comments