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