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 java.io.Serializable;
020
021 import org.apache.commons.functor.UnaryPredicate;
022
023 /**
024 * {@link #test Tests} to the logical inverse
025 * of some other predicate.
026 * <p>
027 * Note that although this class implements
028 * {@link Serializable}, a given instance will
029 * only be truly <code>Serializable</code> if the
030 * underlying functor is. Attempts to serialize
031 * an instance whose delegate is not
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 UnaryNot<A> implements UnaryPredicate<A>, Serializable {
038 /**
039 * serialVersionUID declaration.
040 */
041 private static final long serialVersionUID = -97785102566566058L;
042 // attributes
043 // ------------------------------------------------------------------------
044 private final UnaryPredicate<? super A> predicate;
045
046 // constructor
047 // ------------------------------------------------------------------------
048 /**
049 * Create a new UnaryNot.
050 * @param predicate UnaryPredicate to negate
051 */
052 public UnaryNot(UnaryPredicate<? super A> predicate) {
053 if (predicate == null) {
054 throw new IllegalArgumentException("UnaryPredicate argument was null");
055 }
056 this.predicate = predicate;
057 }
058
059 // predicate interface
060 // ------------------------------------------------------------------------
061 /**
062 * {@inheritDoc}
063 */
064 public boolean test(A obj) {
065 return !(predicate.test(obj));
066 }
067
068 /**
069 * {@inheritDoc}
070 */
071 public boolean equals(Object that) {
072 return that == this || (that instanceof UnaryNot<?> && equals((UnaryNot<?>) that));
073 }
074
075 /**
076 * Learn whether another UnaryNot is equal to this.
077 * @param that UnaryNot to test
078 * @return boolean
079 */
080 public boolean equals(UnaryNot<?> that) {
081 return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
082 }
083
084 /**
085 * {@inheritDoc}
086 */
087 public int hashCode() {
088 int hash = "UnaryNot".hashCode();
089 if (null != predicate) {
090 hash ^= predicate.hashCode();
091 }
092 return hash;
093 }
094
095 /**
096 * {@inheritDoc}
097 */
098 public String toString() {
099 return "UnaryNot<" + predicate + ">";
100 }
101
102 // static
103 // ------------------------------------------------------------------------
104 /**
105 * Invert a UnaryPredicate.
106 * @param pred UnaryPredicate to invert
107 * @return UnaryPredicate<A
108 */
109 public static <A> UnaryPredicate<A> not(UnaryPredicate<? super A> pred) {
110 return null == pred ? null : new UnaryNot<A>(pred);
111 }
112
113 }