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