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; 018 019 import java.io.Serializable; 020 021 import org.apache.commons.functor.BinaryPredicate; 022 import org.apache.commons.functor.UnaryPredicate; 023 import org.apache.commons.functor.adapter.IgnoreLeftPredicate; 024 import org.apache.commons.functor.adapter.IgnoreRightPredicate; 025 026 /** 027 * {@link #test Tests} 028 * <code>false</code> iff its argument 029 * is <code>null</code>. 030 * 031 * @param <T> the argument type. 032 * @version $Revision: 1160401 $ $Date: 2011-08-22 21:47:27 +0200 (Mon, 22 Aug 2011) $ 033 * @author Rodney Waldhoff 034 */ 035 public final class IsNotNull<T> implements UnaryPredicate<T>, Serializable { 036 037 // static attributes 038 // ------------------------------------------------------------------------ 039 /** 040 * Basic IsNotNull instance. 041 */ 042 public static final IsNotNull<Object> INSTANCE = IsNotNull.<Object>instance(); 043 044 /** 045 * Left-handed BinaryPredicate. 046 */ 047 public static final BinaryPredicate<Object, Object> LEFT = IsNotNull.<Object>left(); 048 049 /** 050 * Right-handed BinaryPredicate. 051 */ 052 public static final BinaryPredicate<Object, Object> RIGHT = IsNotNull.<Object>right(); 053 054 /** 055 * serialVersionUID declaration. 056 */ 057 private static final long serialVersionUID = -6856387958371590330L; 058 059 // constructor 060 // ------------------------------------------------------------------------ 061 /** 062 * Create a new IsNotNull. 063 */ 064 public IsNotNull() { 065 } 066 067 // predicate interface 068 // ------------------------------------------------------------------------ 069 /** 070 * {@inheritDoc} 071 */ 072 public boolean test(Object obj) { 073 return (null != obj); 074 } 075 076 /** 077 * {@inheritDoc} 078 */ 079 public boolean equals(Object that) { 080 return that instanceof IsNotNull<?>; 081 } 082 083 /** 084 * {@inheritDoc} 085 */ 086 public int hashCode() { 087 return "IsNotNull".hashCode(); 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 public String toString() { 094 return "IsNotNull"; 095 } 096 097 // static methods 098 // ------------------------------------------------------------------------ 099 /** 100 * Get an IsNotNull instance. 101 * @param <T> the predicate argument type. 102 * @return IsNotNull 103 */ 104 public static <T> IsNotNull<T> instance() { 105 return new IsNotNull<T>(); 106 } 107 108 /** 109 * Get a BinaryPredicate that matches if the left argument is not null. 110 * @param <A> the left {@code BinaryPredicate} argument type. 111 * @return BinaryPredicate<A, Object> 112 */ 113 public static <A> BinaryPredicate<A, Object> left() { 114 return IgnoreRightPredicate.adapt(new IsNotNull<A>()); 115 } 116 117 /** 118 * Get a BinaryPredicate that matches if the right argument is null. 119 * @param <A> the right {@code BinaryPredicate} argument type. 120 * @return BinaryPredicate<Object, A> 121 */ 122 public static <A> BinaryPredicate<Object, A> right() { 123 return IgnoreLeftPredicate.adapt(new IsNotNull<A>()); 124 } 125 126 }