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; 021 022import org.apache.commons.functor.Function; 023import org.apache.commons.lang3.Validate; 024 025/** 026 * Returns the size of the specified Collection, or the length 027 * of the specified array or String. 028 * 029 * @param <A> the function argument type. 030 * @version $Revision: 1537901 $ $Date: 2013-11-01 12:30:19 +0100 (Fr, 01 Nov 2013) $ 031 */ 032public final class Size<A> implements Function<A, Integer> { 033 034 /** 035 * A static {@code Size} instance reference. 036 */ 037 private static final Size<Object> INSTANCE = new Size<Object>(); 038 039 // constructor 040 // ------------------------------------------------------------------------ 041 /** 042 * Create a new Size. 043 */ 044 public Size() { } 045 046 /** 047 * {@inheritDoc} 048 */ 049 public Integer evaluate(Object obj) { 050 Validate.notNull(obj, "Argument must not be null"); 051 if (obj instanceof Collection<?>) { 052 return evaluate((Collection<?>) obj); 053 } 054 if (obj instanceof String) { 055 return evaluate((String) obj); 056 } 057 if (obj.getClass().isArray()) { 058 return evaluateArray(obj); 059 } 060 throw new IllegalArgumentException("Expected Collection, String or Array, found " + obj); 061 } 062 063 /** 064 * {@inheritDoc} 065 */ 066 @Override 067 public boolean equals(Object that) { 068 return that instanceof Size<?>; 069 } 070 071 /** 072 * {@inheritDoc} 073 */ 074 @Override 075 public int hashCode() { 076 return "Size".hashCode(); 077 } 078 079 /** 080 * {@inheritDoc} 081 */ 082 @Override 083 public String toString() { 084 return "Size()"; 085 } 086 087 /** 088 * Get a Size instance. 089 * @return Size 090 */ 091 public static Size<Object> instance() { 092 return INSTANCE; 093 } 094 095 /** 096 * Evaluate a Collection. 097 * @param col to evaluate 098 * @return Integer 099 */ 100 private Integer evaluate(Collection<?> col) { 101 return Integer.valueOf(col.size()); 102 } 103 104 /** 105 * Evaluate a String. 106 * @param str to evaluate 107 * @return Integer 108 */ 109 private Integer evaluate(String str) { 110 return Integer.valueOf(str.length()); 111 } 112 113 /** 114 * Evaluate an array. 115 * @param array to evaluate 116 * @return Integer 117 */ 118 private Integer evaluateArray(Object array) { 119 return Integer.valueOf(Array.getLength(array)); 120 } 121 122}