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.collection;
018
019import java.lang.reflect.Array;
020import java.util.Collection;
021import java.util.Map;
022
023import org.apache.commons.functor.Predicate;
024import org.apache.commons.lang3.Validate;
025
026/**
027 * A {@link Predicate} that checks to see if the specified object is empty.
028 *
029 * @param <A> the predicate argument type.
030 * @version $Revision: 1538565 $ $Date: 2013-11-04 12:39:54 +0100 (Mo, 04 Nov 2013) $
031 */
032public final class IsEmpty<A> implements Predicate<A> {
033
034    // class variables
035    // ------------------------------------------------------------------------
036
037    /**
038     * Basic IsEmpty instance.
039     */
040    public static final IsEmpty<Object> INSTANCE = new IsEmpty<Object>();
041
042    // constructor
043    // ------------------------------------------------------------------------
044    /**
045     * Create a new IsEmpty.
046     */
047    public IsEmpty() {
048    }
049
050    // instance methods
051    // ------------------------------------------------------------------------
052    /**
053     * {@inheritDoc}
054     */
055    public boolean test(A obj) {
056        Validate.notNull(obj, "Argument must not be null");
057        if (obj instanceof Collection<?>) {
058            return testCollection((Collection<?>) obj);
059        }
060        if (obj instanceof Map<?, ?>) {
061            return testMap((Map<?, ?>) obj);
062        }
063        if (obj instanceof String) {
064            return testString((String) obj);
065        }
066        if (obj.getClass().isArray()) {
067            return testArray(obj);
068        }
069        throw new IllegalArgumentException("Expected Collection, Map, String or Array, found " + obj.getClass());
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public boolean equals(Object that) {
077        return that instanceof IsEmpty<?>;
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public int hashCode() {
085        return "IsEmpty".hashCode();
086    }
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public String toString() {
093        return "IsEmpty()";
094    }
095
096    /**
097     * Test a collection.
098     * @param col to test
099     * @return boolean
100     */
101    private boolean testCollection(Collection<?> col) {
102        return col.isEmpty();
103    }
104
105    /**
106     * Test a map.
107     * @param map to test
108     * @return boolean
109     */
110    private boolean testMap(Map<?, ?> map) {
111        return map.isEmpty();
112    }
113
114    /**
115     * Test a string.
116     * @param str to test
117     * @return boolean
118     */
119    private boolean testString(String str) {
120        return 0 == str.length();
121    }
122
123    /**
124     * Test an array.
125     * @param array to test
126     * @return boolean
127     */
128    private boolean testArray(Object array) {
129        return 0 == Array.getLength(array);
130    }
131
132    // static
133    // ------------------------------------------------------------------------
134    /**
135     * Get an IsEmpty instance.
136     *
137     * @param <A> the predicate argument type.
138     * @return IsEmpty
139     */
140    public static <A> IsEmpty<A> instance() {
141        return new IsEmpty<A>();
142    }
143
144}