001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.functor.core.composite;
018    
019    import org.apache.commons.functor.Predicate;
020    
021    /**
022     * {@link #test Tests} <code>true</code> iff
023     * at least one of its children test <code>true</code>.
024     * Note that by this definition, the "or" of
025     * an empty collection of predicates tests <code>false</code>.
026     * <p>
027     * Note that although this class implements
028     * {@link java.io.Serializable Serializable}, a given instance will
029     * only be truly <code>Serializable</code> if all the
030     * underlying functors are.  Attempts to serialize
031     * an instance whose delegates are not all
032     * <code>Serializable</code> will result in an exception.
033     * </p>
034     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
035     * @author Rodney Waldhoff
036     */
037    public final class Or extends BasePredicateList {
038    
039        /**
040         * serialVersionUID declaration.
041         */
042        private static final long serialVersionUID = -1636233158061690073L;
043    
044        // constructor
045        // ------------------------------------------------------------------------
046        /**
047         * Create a new Or.
048         */
049        public Or() {
050            super();
051        }
052    
053        /**
054         * Create a new Or instance.
055         *
056         * @param predicates
057         */
058        public Or(Iterable<Predicate> predicates) {
059            super(predicates);
060        }
061    
062        /**
063         * Create a new Or instance.
064         *
065         * @param predicates
066         */
067        public Or(Predicate... predicates) {
068            super(predicates);
069        }
070    
071        /**
072         * Fluently add a Predicate.
073         * @param p Predicate to add
074         * @return this
075         */
076        public Or or(Predicate p) {
077            super.addPredicate(p);
078            return this;
079        }
080    
081        // predicate interface
082        // ------------------------------------------------------------------------
083        /**
084         * {@inheritDoc}
085         */
086        public boolean test() {
087            for (Predicate p : getPredicateList()) {
088                if (p.test()) {
089                    return true;
090                }
091            }
092            return false;
093        }
094    
095        /**
096         * {@inheritDoc}
097         */
098        public boolean equals(Object that) {
099            return that == this || (that instanceof Or && equals((Or) that));
100        }
101    
102        /**
103         * Learn whether another Or is equal to this.
104         * @param that Or to test
105         * @return boolean
106         */
107        public boolean equals(Or that) {
108            return getPredicateListEquals(that);
109        }
110    
111        /**
112         * {@inheritDoc}
113         */
114        public int hashCode() {
115            return "Or".hashCode() ^ getPredicateListHashCode();
116        }
117    
118        /**
119         * {@inheritDoc}
120         */
121        public String toString() {
122            return "Or<" + getPredicateListToString() + ">";
123        }
124    
125    }