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     */
020    @SuppressWarnings("serial")
021    public final class And extends AbstractConstraint {
022            private final Constraint[] children;
023    
024        public And(final Constraint c1, final Constraint c2) {
025            children = new Constraint[2];
026            children[0] = c1;
027            children[1] = c2;
028        }
029    
030        public And(final Constraint c1, final Constraint c2, final Constraint c3) {
031            children = new Constraint[3];
032            children[0] = c1;
033            children[1] = c2;
034            children[2] = c3;
035        }
036    
037        public And(final Constraint[] cc) {
038            children = new Constraint[cc.length];
039            System.arraycopy(cc, 0, children, 0, cc.length);
040        }
041    
042        @Override
043            public boolean evaluate(final ComponentAdapter adapter) {
044            for (Constraint aChildren : children) {
045                if (!aChildren.evaluate(adapter)) {
046                    return false;
047                }
048            }
049            return true;
050        }
051        
052        @Override
053            public void accept(final PicoVisitor visitor) {
054            super.accept(visitor);
055            for (Constraint aChildren : children) {
056                aChildren.accept(visitor);
057            }
058        }
059    }