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