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