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 */
017package org.apache.commons.functor.core.composite;
018
019import 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: 1345136 $ $Date: 2012-06-01 08:47:06 -0400 (Fri, 01 Jun 2012) $
035 */
036public final class Or extends BasePredicateList {
037
038    /**
039     * serialVersionUID declaration.
040     */
041    private static final long serialVersionUID = -1636233158061690073L;
042
043    // constructor
044    // ------------------------------------------------------------------------
045    /**
046     * Create a new Or.
047     */
048    public Or() {
049        super();
050    }
051
052    /**
053     * Create a new Or instance.
054     *
055     * @param predicates predicates have to be put in or condition.
056     */
057    public Or(Iterable<Predicate> predicates) {
058        super(predicates);
059    }
060
061    /**
062     * Create a new Or instance.
063     *
064     * @param predicates predicates have to be put in or condition.
065     */
066    public Or(Predicate... predicates) {
067        super(predicates);
068    }
069
070    /**
071     * Fluently add a Predicate.
072     * @param p Predicate to add
073     * @return this
074     */
075    public Or or(Predicate p) {
076        super.addPredicate(p);
077        return this;
078    }
079
080    // predicate interface
081    // ------------------------------------------------------------------------
082    /**
083     * {@inheritDoc}
084     */
085    public boolean test() {
086        for (Predicate p : getPredicateList()) {
087            if (p.test()) {
088                return true;
089            }
090        }
091        return false;
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    @Override
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    @Override
115    public int hashCode() {
116        return "Or".hashCode() ^ getPredicateListHashCode();
117    }
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public String toString() {
124        return "Or<" + getPredicateListToString() + ">";
125    }
126
127}