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