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 import org.apache.commons.lang3.Validate; 23 24 /** 25 * {@link #test Tests} to the logical inverse 26 * of some other predicate. 27 * <p> 28 * Note that although this class implements 29 * {@link Serializable}, a given instance will 30 * only be truly <code>Serializable</code> if the 31 * underlying functor is. Attempts to serialize 32 * an instance whose delegate is not 33 * <code>Serializable</code> will result in an exception. 34 * </p> 35 * @version $Revision: 1365329 $ $Date: 2012-07-24 19:34:23 -0300 (Tue, 24 Jul 2012) $ 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 /** 46 * The adapted predicate has to be negated. 47 */ 48 private final Predicate predicate; 49 50 // constructor 51 // ------------------------------------------------------------------------ 52 /** 53 * Create a new Not. 54 * @param predicate Predicate to negate 55 */ 56 public Not(Predicate predicate) { 57 this.predicate = Validate.notNull(predicate, "Predicate argument was null"); 58 } 59 60 // predicate interface 61 // ------------------------------------------------------------------------ 62 /** 63 * {@inheritDoc} 64 */ 65 public boolean test() { 66 return !(predicate.test()); 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 public boolean equals(Object that) { 74 return that == this || (that instanceof Not && equals((Not) that)); 75 } 76 77 /** 78 * Learn whether another Not is equal to this. 79 * @param that the Not to test 80 * @return boolean 81 */ 82 public boolean equals(Not that) { 83 return null != that && predicate.equals(that.predicate); 84 } 85 86 /** 87 * {@inheritDoc} 88 */ 89 @Override 90 public int hashCode() { 91 int hash = "Not".hashCode(); 92 hash ^= predicate.hashCode(); 93 return hash; 94 } 95 96 /** 97 * {@inheritDoc} 98 */ 99 @Override 100 public String toString() { 101 return "Not<" + predicate + ">"; 102 } 103 104 // static 105 // ------------------------------------------------------------------------ 106 /** 107 * Get a Not instance for <code>that</code>. 108 * @param that Predicate to negate 109 * @return Not 110 */ 111 public static Predicate not(Predicate that) { 112 return null == that ? null : new Not(that); 113 } 114 }