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; 18 19 import java.io.Serializable; 20 21 import org.apache.commons.functor.BinaryPredicate; 22 import org.apache.commons.functor.UnaryPredicate; 23 import org.apache.commons.functor.adapter.RightBoundPredicate; 24 25 /** 26 * {@link #test Tests} 27 * <code>true</code> iff its arguments are 28 * not {@link Object#equals equal} or both 29 * <code>null</code>. 30 * <p> 31 * This relation is symmetric but irreflexive 32 * and not transitive. 33 * </p> 34 * @param <L> the left argument type. 35 * @param <R> the right argument type. 36 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 37 */ 38 public final class IsNotEqual<L, R> implements BinaryPredicate<L, R>, Serializable { 39 // static attributes 40 // ------------------------------------------------------------------------ 41 /** 42 * Basic IsNotEqual<Object, Object> instance. 43 */ 44 public static final IsNotEqual<Object, Object> INSTANCE = IsNotEqual.<Object, Object>instance(); 45 /** 46 * serialVersionUID declaration. 47 */ 48 private static final long serialVersionUID = -7303588338955281317L; 49 50 // constructor 51 // ------------------------------------------------------------------------ 52 /** 53 * Create a new IsNotEqual. 54 */ 55 public IsNotEqual() { 56 } 57 58 // predicate interface 59 // ------------------------------------------------------------------------ 60 /** 61 * {@inheritDoc} 62 */ 63 public boolean test(L left, R right) { 64 return (null == left ? null != right : !left.equals(right)); 65 } 66 67 /** 68 * {@inheritDoc} 69 */ 70 @Override 71 public boolean equals(Object that) { 72 return that instanceof IsNotEqual<?, ?>; 73 } 74 75 /** 76 * {@inheritDoc} 77 */ 78 @Override 79 public int hashCode() { 80 return "IsNotEqual".hashCode(); 81 } 82 83 /** 84 * {@inheritDoc} 85 */ 86 @Override 87 public String toString() { 88 return "IsNotEqual"; 89 } 90 91 // static methods 92 // ------------------------------------------------------------------------ 93 94 /** 95 * Get an IsNotEqual instance. 96 * @param <L> the left argument type. 97 * @param <R> the right argument type. 98 * @return IsNotEqual<L, R> 99 */ 100 public static <L, R> IsNotEqual<L, R> instance() { 101 return new IsNotEqual<L, R>(); 102 } 103 104 /** 105 * Get an IsNotEqual UnaryPredicate. 106 * @param <L> the left argument type. 107 * @param <R> the right argument type. 108 * @param object bound comparison object 109 * @return UnaryPredicate<L> 110 */ 111 public static <L, R> UnaryPredicate<L> to(R object) { 112 return new RightBoundPredicate<L>(new IsNotEqual<L, R>(), object); 113 } 114 }