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    
023    import org.apache.commons.functor.UnaryFunction;
024    
025    /**
026     * Returns the size of the specified Collection, or the length
027     * of the specified array or String.
028     * @version $Revision: 1166334 $ $Date: 2011-09-07 21:37:49 +0200 (Wed, 07 Sep 2011) $
029     * @author Rodney Waldhoff
030     */
031    public final class Size<A> implements UnaryFunction<A, Integer>, Serializable {
032    
033        /**
034         * serialVersionUID declaration.
035         */
036        private static final long serialVersionUID = -12374650738412129L;
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            if (obj instanceof Collection<?>) {
051                return evaluate((Collection<?>) obj);
052            }
053            if (obj instanceof String) {
054                return evaluate((String) obj);
055            }
056            if (null != obj && obj.getClass().isArray()) {
057                return evaluateArray(obj);
058            }
059            if (null == obj) {
060                throw new IllegalArgumentException("Argument must not be null");
061            }
062            throw new IllegalArgumentException("Expected Collection, String or Array, found " + obj);
063        }
064    
065        /**
066         * {@inheritDoc}
067         */
068        public boolean equals(Object that) {
069            return that instanceof Size<?>;
070        }
071    
072        /**
073         * {@inheritDoc}
074         */
075        public int hashCode() {
076            return "Size".hashCode();
077        }
078    
079        /**
080         * {@inheritDoc}
081         */
082        public String toString() {
083            return "Size()";
084        }
085    
086        /**
087         * Get a Size instance.
088         * @return Size
089         */
090        public static Size<Object> instance() {
091            return INSTANCE;
092        }
093    
094        /**
095         * Evaluate a Collection.
096         * @param col to evaluate
097         * @return Integer
098         */
099        private int evaluate(Collection<?> col) {
100            return col.size();
101        }
102    
103        /**
104         * Evaluate a String.
105         * @param str to evaluate
106         * @return Integer
107         */
108        private int evaluate(String str) {
109            return str.length();
110        }
111    
112        /**
113         * Evaluate an array.
114         * @param array to evaluate
115         * @return Integer
116         */
117        private int evaluateArray(Object array) {
118            return Array.getLength(array);
119        }
120    
121    }