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.camel.util;
018    
019    import java.lang.reflect.Array;
020    import java.util.ArrayList;
021    import java.util.Collection;
022    import java.util.List;
023    import java.util.Map;
024    
025    import org.w3c.dom.NodeList;
026    
027    /**
028     * A number of helper methods for working with collections
029     *
030     * @version $Revision: 36321 $
031     */
032    public final class CollectionHelper {
033    
034        /**
035         * Utility classes should not have a public constructor.
036         */
037        private CollectionHelper() {
038        }
039    
040        /**
041         * Returns the size of the collection if it can be determined to be a collection
042         */
043        public static Integer size(Object value) {
044            if (value != null) {
045                if (value instanceof Collection) {
046                    Collection collection = (Collection)value;
047                    return collection.size();
048                } else if (value instanceof Map) {
049                    Map map = (Map)value;
050                    return map.size();
051                } else if (value instanceof Object[]) {
052                    Object[] array = (Object[])value;
053                    return array.length;
054                } else if (value.getClass().isArray()) {
055                    return Array.getLength(value);
056                } else if (value instanceof NodeList) {
057                    NodeList nodeList = (NodeList)value;
058                    return nodeList.getLength();
059                }
060            }
061            return null;
062        }
063    
064        /**
065         * Sets the value of the entry in the map for the given key, though if the
066         * map already contains a value for the given key then the value is appended
067         * to a list of values.
068         *
069         * @param map the map to add the entry to
070         * @param key the key in the map
071         * @param value the value to put in the map
072         */
073        public static void appendValue(Map map, Object key, Object value) {
074    
075            Object oldValue = map.get(key);
076            if (oldValue != null) {
077                List list;
078                if (oldValue instanceof List) {
079                    list = (List)oldValue;
080                } else {
081                    list = new ArrayList();
082                    list.add(oldValue);
083                }
084                list.add(value);
085            } else {
086                map.put(key, value);
087            }
088        }
089    }