001    /*****************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *****************************************************************************/
008    
009    package org.picocontainer.gems.constraints;
010    
011    import org.picocontainer.ComponentAdapter;
012    import org.picocontainer.PicoVisitor;
013    
014    /**
015     * Aggregates multiple constraints together using boolean AND logic.
016     * Constraints are short-circuited as in java.
017     *
018     * @author Nick Sieger
019     * @version 1.1
020     */
021    public class And extends AbstractConstraint {
022        private Constraint[] children;
023    
024        public And(Constraint c1, Constraint c2) {
025            children = new Constraint[2];
026            children[0] = c1;
027            children[1] = c2;
028        }
029    
030        public And(Constraint c1, Constraint c2, Constraint c3) {
031            children = new Constraint[3];
032            children[0] = c1;
033            children[1] = c2;
034            children[2] = c3;
035        }
036    
037        public And(Constraint[] cc) {
038            children = new Constraint[cc.length];
039            System.arraycopy(cc, 0, children, 0, cc.length);
040        }
041    
042        public boolean evaluate(ComponentAdapter adapter) {
043            for (int i = 0; i < children.length; i++) {
044                if (!children[i].evaluate(adapter)) {
045                    return false;
046                }
047            }
048            return true;
049        }
050        
051        public void accept(PicoVisitor visitor) {
052            super.accept(visitor);
053            for (int i = 0; i < children.length; i++) {
054                children[i].accept(visitor);
055            }
056        }
057    }