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.BinaryPredicate; 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 BinaryNot<L, R> implements BinaryPredicate<L, R>, Serializable { 038 /** 039 * serialVersionUID declaration. 040 */ 041 private static final long serialVersionUID = -3488974286912054737L; 042 // attributes 043 // ------------------------------------------------------------------------ 044 private final BinaryPredicate<? super L, ? super R> predicate; 045 046 // constructor 047 // ------------------------------------------------------------------------ 048 /** 049 * Create a new BinaryNot. 050 * @param predicate BinaryPredicate to negate 051 */ 052 public BinaryNot(BinaryPredicate<? super L, ? super R> predicate) { 053 if (predicate == null) { 054 throw new IllegalArgumentException("BinaryPredicate argument was null"); 055 } 056 this.predicate = predicate; 057 } 058 059 // predicate interface 060 // ------------------------------------------------------------------------ 061 /** 062 * {@inheritDoc} 063 */ 064 public boolean test(L left, R right) { 065 return !(predicate.test(left, right)); 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 public boolean equals(Object that) { 072 return that == this || (that instanceof BinaryNot<?, ?> && equals((BinaryNot<?, ?>) that)); 073 } 074 075 /** 076 * Learn whether another BinaryNot is equal to this. 077 * @param that BinaryNot to test 078 * @return boolean 079 */ 080 public boolean equals(BinaryNot<?, ?> 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 = "BinaryNot".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 "BinaryNot<" + predicate + ">"; 100 } 101 102 // static 103 // ------------------------------------------------------------------------ 104 /** 105 * Negate a BinaryPredicate. 106 * @param that BinaryPredicate to negate 107 * @return BinaryPredicate 108 */ 109 public static <L, R> BinaryPredicate<L, R> not(BinaryPredicate<? super L, ? super R> that) { 110 return null == that ? null : new BinaryNot<L, R>(that); 111 } 112 113 }