Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Not |
|
| 2.142857142857143;2.143 |
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 java.io.Serializable; | |
20 | ||
21 | import org.apache.commons.functor.Predicate; | |
22 | ||
23 | /** | |
24 | * {@link #test Tests} to the logical inverse | |
25 | * of some other predicate. | |
26 | * <p> | |
27 | * Note that although this class implements | |
28 | * {@link Serializable}, a given instance will | |
29 | * only be truly <code>Serializable</code> if the | |
30 | * underlying functor is. Attempts to serialize | |
31 | * an instance whose delegate is not | |
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 Not implements Predicate, Serializable { | |
38 | ||
39 | /** | |
40 | * serialVersionUID declaration. | |
41 | */ | |
42 | private static final long serialVersionUID = 8268713706856765874L; | |
43 | // attributes | |
44 | // ------------------------------------------------------------------------ | |
45 | private final Predicate predicate; | |
46 | ||
47 | // constructor | |
48 | // ------------------------------------------------------------------------ | |
49 | /** | |
50 | * Create a new Not. | |
51 | * @param predicate Predicate to negate | |
52 | */ | |
53 | 24 | public Not(Predicate predicate) { |
54 | 24 | if (predicate == null) { |
55 | 0 | throw new IllegalArgumentException("Predicate argument was null"); |
56 | } | |
57 | 24 | this.predicate = predicate; |
58 | 24 | } |
59 | ||
60 | // predicate interface | |
61 | // ------------------------------------------------------------------------ | |
62 | /** | |
63 | * {@inheritDoc} | |
64 | */ | |
65 | public boolean test() { | |
66 | 28 | return !(predicate.test()); |
67 | } | |
68 | ||
69 | /** | |
70 | * {@inheritDoc} | |
71 | */ | |
72 | public boolean equals(Object that) { | |
73 | 26 | return that == this || (that instanceof Not && equals((Not) that)); |
74 | } | |
75 | ||
76 | /** | |
77 | * Learn whether another Not is equal to this. | |
78 | * @param that the Not to test | |
79 | * @return boolean | |
80 | */ | |
81 | public boolean equals(Not that) { | |
82 | 18 | return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate)); |
83 | } | |
84 | ||
85 | /** | |
86 | * {@inheritDoc} | |
87 | */ | |
88 | public int hashCode() { | |
89 | 28 | int hash = "Not".hashCode(); |
90 | 28 | if (null != predicate) { |
91 | 28 | hash ^= predicate.hashCode(); |
92 | } | |
93 | 28 | return hash; |
94 | } | |
95 | ||
96 | /** | |
97 | * {@inheritDoc} | |
98 | */ | |
99 | public String toString() { | |
100 | 20 | return "Not<" + predicate + ">"; |
101 | } | |
102 | ||
103 | // static | |
104 | // ------------------------------------------------------------------------ | |
105 | /** | |
106 | * Get a Not instance for <code>that</code>. | |
107 | * @param that Predicate to negate | |
108 | * @return Not | |
109 | */ | |
110 | public static Predicate not(Predicate that) { | |
111 | 6 | return null == that ? null : new Not(that); |
112 | } | |
113 | } |