1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.functor.core.composite;
18
19 import org.apache.commons.functor.BinaryPredicate;
20
21 /**
22 * {@link #test Tests} <code>true</code> iff
23 * at least one of its children test <code>true</code>.
24 * Note that by this definition, the "or" of
25 * an empty collection of predicates tests <code>false</code>.
26 * <p>
27 * Note that although this class implements
28 * {@link java.io.Serializable Serializable}, a given instance will
29 * only be truly <code>Serializable</code> if all the
30 * underlying functors are. Attempts to serialize
31 * an instance whose delegates are not all
32 * <code>Serializable</code> will result in an exception.
33 * </p>
34 * @param <L> the left argument type.
35 * @param <R> the right argument type.
36 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
37 */
38 public final class BinaryOr<L, R> extends BaseBinaryPredicateList<L, R> {
39
40 /**
41 * serialVersionUID declaration.
42 */
43 private static final long serialVersionUID = -831325131082968810L;
44
45 // constructor
46 // ------------------------------------------------------------------------
47 /**
48 * Create a new BinaryOr.
49 */
50 public BinaryOr() {
51 super();
52 }
53
54 /**
55 * Create a new BinaryOr instance.
56 *
57 * @param predicates the predicates to add
58 */
59 public BinaryOr(BinaryPredicate<? super L, ? super R>... predicates) {
60 super(predicates);
61 }
62
63 /**
64 * Create a new BinaryOr instance.
65 *
66 * @param predicates the predicates to add
67 */
68 public BinaryOr(Iterable<BinaryPredicate<? super L, ? super R>> predicates) {
69 super(predicates);
70 }
71
72 // modifiers
73 // ------------------------------------------------------------------------
74 /**
75 * Fluently add a BinaryPredicate.
76 * @param p BinaryPredicate to add
77 * @return this
78 */
79 public BinaryOr<L, R> or(BinaryPredicate<? super L, ? super R> p) {
80 super.addBinaryPredicate(p);
81 return this;
82 }
83
84 // predicate interface
85 // ------------------------------------------------------------------------
86 /**
87 * {@inheritDoc}
88 */
89 public boolean test(L a, R b) {
90 for (BinaryPredicate<? super L, ? super R> p : getBinaryPredicateList()) {
91 if (p.test(a, b)) {
92 return true;
93 }
94 }
95 return false;
96 }
97
98 /**
99 * {@inheritDoc}
100 */
101 @Override
102 public boolean equals(Object that) {
103 return that == this || (that instanceof BinaryOr<?, ?> && equals((BinaryOr<?, ?>) that));
104 }
105
106 /**
107 * Learn whether another BinaryOr is equal to this.
108 * @param that BinaryOr to test
109 * @return boolean
110 */
111 public boolean equals(BinaryOr<?, ?> that) {
112 return getBinaryPredicateListEquals(that);
113 }
114
115 /**
116 * {@inheritDoc}
117 */
118 @Override
119 public int hashCode() {
120 return "BinaryOr".hashCode() ^ getBinaryPredicateListHashCode();
121 }
122
123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public String toString() {
128 return "BinaryOr<" + getBinaryPredicateListToString() + ">";
129 }
130
131 }