001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.rules.reporting;
017    
018    import java.util.Iterator;
019    
020    import org.springframework.core.ReflectiveVisitorHelper;
021    import org.springframework.rules.constraint.Constraint;
022    import org.springframework.rules.constraint.And;
023    import org.springframework.rules.constraint.Or;
024    import org.springframework.rules.constraint.property.CompoundPropertyConstraint;
025    import org.springframework.rules.constraint.property.ParameterizedPropertyConstraint;
026    import org.springframework.rules.constraint.property.PropertiesConstraint;
027    import org.springframework.util.Assert;
028    
029    /**
030     * @author Keith Donald
031     */
032    public class SummingVisitor {
033    
034            private ReflectiveVisitorHelper visitorSupport = new ReflectiveVisitorHelper();
035    
036            private int sum;
037    
038            private Constraint constraint;
039    
040            public SummingVisitor(Constraint constraint) {
041                    Assert.notNull(constraint, "constraint is required");
042                    this.constraint = constraint;
043            }
044    
045            public int sum() {
046                    visitorSupport.invokeVisit(this, constraint);
047                    return sum;
048            }
049    
050            void visit(CompoundPropertyConstraint rule) {
051                    visitorSupport.invokeVisit(this, rule.getPredicate());
052            }
053    
054            void visit(PropertiesConstraint e) {
055                    sum++;
056            }
057    
058            void visit(ParameterizedPropertyConstraint e) {
059                    sum++;
060            }
061    
062            void visit(And and) {
063                    Iterator it = and.iterator();
064                    while (it.hasNext()) {
065                            Constraint p = (Constraint) it.next();
066                            visitorSupport.invokeVisit(this, p);
067                    }
068            }
069    
070            void visit(Or or) {
071                    Iterator it = or.iterator();
072                    while (it.hasNext()) {
073                            Constraint p = (Constraint) it.next();
074                            visitorSupport.invokeVisit(this, p);
075                    }
076            }
077    
078            void visit(Constraint constraint) {
079                    sum++;
080            }
081    
082    }