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