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.UnaryPredicate;
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 UnaryOr<A> extends BaseUnaryPredicateList<A> {
038
039 /**
040 * serialVersionUID declaration.
041 */
042 private static final long serialVersionUID = -4072936447932983645L;
043
044 // constructor
045 // ------------------------------------------------------------------------
046 /**
047 * Create a new UnaryOr.
048 */
049 public UnaryOr() {
050 super();
051 }
052
053 /**
054 * Create a new UnaryOr instance.
055 *
056 * @param predicates
057 */
058 public UnaryOr(Iterable<UnaryPredicate<? super A>> predicates) {
059 super(predicates);
060 }
061
062 /**
063 * Create a new UnaryOr instance.
064 *
065 * @param predicates
066 */
067 public UnaryOr(UnaryPredicate<? super A>... predicates) {
068 super(predicates);
069 }
070
071 // modifiers
072 // ------------------------------------------------------------------------
073 /**
074 * Fluently add a Predicate.
075 * @param p Predicate to add
076 * @return this
077 */
078 public UnaryOr<A> or(UnaryPredicate<? super A> p) {
079 super.addUnaryPredicate(p);
080 return this;
081 }
082
083 // predicate interface
084 // ------------------------------------------------------------------------
085 /**
086 * {@inheritDoc}
087 */
088 public boolean test(A a) {
089 for (UnaryPredicate<? super A> p : getUnaryPredicateList()) {
090 if (p.test(a)) {
091 return true;
092 }
093 }
094 return false;
095 }
096
097 /**
098 * {@inheritDoc}
099 */
100 public boolean equals(Object that) {
101 return that == this || (that instanceof UnaryOr<?> && equals((UnaryOr<?>) that));
102 }
103
104 /**
105 * Learn whether another UnaryOr is equal to this.
106 * @param that UnaryOr to test
107 * @return boolean
108 */
109 public boolean equals(UnaryOr<?> that) {
110 return getUnaryPredicateListEquals(that);
111 }
112
113 /**
114 * {@inheritDoc}
115 */
116 public int hashCode() {
117 return "UnaryOr".hashCode() ^ getUnaryPredicateListHashCode();
118 }
119
120 /**
121 * {@inheritDoc}
122 */
123 public String toString() {
124 return "UnaryOr<" + getUnaryPredicateListToString() + ">";
125 }
126
127 }