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 * @param <A> the predicate argument type.
027 * @version $Revision: 1537602 $ $Date: 2013-10-31 20:53:09 +0100 (Do, 31 Okt 2013) $
028 */
029public final class Or<A> extends BasePredicateList<A> {
030
031    // constructor
032    // ------------------------------------------------------------------------
033    /**
034     * Create a new Or.
035     */
036    public Or() {
037        super();
038    }
039
040    /**
041     * Create a new Or instance.
042     *
043     * @param predicates the predicates to put in or.
044     */
045    public Or(Iterable<Predicate<? super A>> predicates) {
046        super(predicates);
047    }
048
049    /**
050     * Create a new Or instance.
051     *
052     * @param predicates the predicates to put in or.
053     */
054    public Or(Predicate<? super A>... predicates) {
055        super(predicates);
056    }
057
058    // modifiers
059    // ------------------------------------------------------------------------
060    /**
061     * Fluently add a Predicate.
062     * @param p Predicate to add
063     * @return this
064     */
065    public Or<A> or(Predicate<? super A> p) {
066        super.addPredicate(p);
067        return this;
068    }
069
070    // predicate interface
071    // ------------------------------------------------------------------------
072    /**
073     * {@inheritDoc}
074     */
075    public boolean test(A a) {
076        for (Predicate<? super A> p : getPredicateList()) {
077            if (p.test(a)) {
078                return true;
079            }
080        }
081        return false;
082    }
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public boolean equals(Object obj) {
089        if (obj == this) {
090            return true;
091        }
092        if (!(obj instanceof Or<?>)) {
093            return false;
094        }
095        Or<?> that = (Or<?>) obj;
096        return getPredicateListEquals(that);
097    }
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public int hashCode() {
104        return "Or".hashCode() ^ getPredicateListHashCode();
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    @Override
111    public String toString() {
112        return "Or<" + getPredicateListToString() + ">";
113    }
114
115}