Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BinaryOr |
|
| 1.5555555555555556;1.556 |
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 | * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $ | |
35 | * @author Rodney Waldhoff | |
36 | */ | |
37 | public final class BinaryOr<L, R> extends BaseBinaryPredicateList<L, R> { | |
38 | ||
39 | /** | |
40 | * serialVersionUID declaration. | |
41 | */ | |
42 | private static final long serialVersionUID = -831325131082968810L; | |
43 | ||
44 | // constructor | |
45 | // ------------------------------------------------------------------------ | |
46 | /** | |
47 | * Create a new BinaryOr. | |
48 | */ | |
49 | public BinaryOr() { | |
50 | 8 | super(); |
51 | 8 | } |
52 | ||
53 | /** | |
54 | * Create a new BinaryOr instance. | |
55 | * | |
56 | * @param predicates | |
57 | */ | |
58 | public BinaryOr(BinaryPredicate<? super L, ? super R>... predicates) { | |
59 | 52 | super(predicates); |
60 | 52 | } |
61 | ||
62 | /** | |
63 | * Create a new BinaryOr instance. | |
64 | * | |
65 | * @param predicates | |
66 | */ | |
67 | public BinaryOr(Iterable<BinaryPredicate<? super L, ? super R>> predicates) { | |
68 | 0 | super(predicates); |
69 | 0 | } |
70 | ||
71 | // modifiers | |
72 | // ------------------------------------------------------------------------ | |
73 | /** | |
74 | * Fluently add a BinaryPredicate. | |
75 | * @param p BinaryPredicate to add | |
76 | * @return this | |
77 | */ | |
78 | public BinaryOr<L, R> or(BinaryPredicate<? super L, ? super R> p) { | |
79 | 124 | super.addBinaryPredicate(p); |
80 | 124 | return this; |
81 | } | |
82 | ||
83 | // predicate interface | |
84 | // ------------------------------------------------------------------------ | |
85 | /** | |
86 | * {@inheritDoc} | |
87 | */ | |
88 | public boolean test(L a, R b) { | |
89 | 134 | for (BinaryPredicate<? super L, ? super R> p : getBinaryPredicateList()) { |
90 | 406 | if (p.test(a, b)) { |
91 | 76 | return true; |
92 | } | |
93 | } | |
94 | 58 | return false; |
95 | } | |
96 | ||
97 | /** | |
98 | * {@inheritDoc} | |
99 | */ | |
100 | public boolean equals(Object that) { | |
101 | 142 | return that == this || (that instanceof BinaryOr<?, ?> && equals((BinaryOr<?, ?>) that)); |
102 | } | |
103 | ||
104 | /** | |
105 | * Learn whether another BinaryOr is equal to this. | |
106 | * @param that BinaryOr to test | |
107 | * @return boolean | |
108 | */ | |
109 | public boolean equals(BinaryOr<?, ?> that) { | |
110 | 118 | return getBinaryPredicateListEquals(that); |
111 | } | |
112 | ||
113 | /** | |
114 | * {@inheritDoc} | |
115 | */ | |
116 | public int hashCode() { | |
117 | 192 | return "BinaryOr".hashCode() ^ getBinaryPredicateListHashCode(); |
118 | } | |
119 | ||
120 | /** | |
121 | * {@inheritDoc} | |
122 | */ | |
123 | public String toString() { | |
124 | 184 | return "BinaryOr<" + getBinaryPredicateListToString() + ">"; |
125 | } | |
126 | ||
127 | } |