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.camel.util;
018
019import java.lang.reflect.Array;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.Iterator;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import org.w3c.dom.NodeList;
030
031/**
032 * A number of helper methods for working with collections
033 *
034 * @version 
035 */
036public final class CollectionHelper {
037
038    /**
039     * Utility classes should not have a public constructor.
040     */
041    private CollectionHelper() {
042    }
043
044    /**
045     * Returns the size of the collection if it can be determined to be a collection
046     *
047     * @param value the collection
048     * @return the size, or <tt>null</tt> if not a collection
049     */
050    public static Integer size(Object value) {
051        if (value != null) {
052            if (value instanceof Collection) {
053                Collection<?> collection = (Collection<?>)value;
054                return collection.size();
055            } else if (value instanceof Map) {
056                Map<?, ?> map = (Map<?, ?>)value;
057                return map.size();
058            } else if (value instanceof Object[]) {
059                Object[] array = (Object[])value;
060                return array.length;
061            } else if (value.getClass().isArray()) {
062                return Array.getLength(value);
063            } else if (value instanceof NodeList) {
064                NodeList nodeList = (NodeList)value;
065                return nodeList.getLength();
066            }
067        }
068        return null;
069    }
070
071    /**
072     * Sets the value of the entry in the map for the given key, though if the
073     * map already contains a value for the given key then the value is appended
074     * to a list of values.
075     *
076     * @param map the map to add the entry to
077     * @param key the key in the map
078     * @param value the value to put in the map
079     */
080    @SuppressWarnings("unchecked")
081    public static void appendValue(Map<String, Object> map, String key, Object value) {
082        Object oldValue = map.get(key);
083        if (oldValue != null) {
084            List<Object> list;
085            if (oldValue instanceof List) {
086                list = (List<Object>)oldValue;
087            } else {
088                list = new ArrayList<Object>();
089                list.add(oldValue);
090                // replace old entry with list
091                map.remove(key);
092                map.put(key, list);
093            }
094            list.add(value);
095        } else {
096            map.put(key, value);
097        }
098    }
099
100    public static <T> Set<T> createSetContaining(T... contents) {
101        Set<T> contentsAsSet = new HashSet<T>();
102        contentsAsSet.addAll(Arrays.asList(contents));
103        return contentsAsSet;
104    }
105
106    public static String collectionAsCommaDelimitedString(String[] col) {
107        if (col == null || col.length == 0) {
108            return "";
109        }
110        return collectionAsCommaDelimitedString(Arrays.asList(col));
111    }
112
113    public static String collectionAsCommaDelimitedString(Collection<?> col) {
114        if (col == null || col.isEmpty()) {
115            return "";
116        }
117
118        StringBuilder sb = new StringBuilder();
119        Iterator<?> it = col.iterator();
120        while (it.hasNext()) {
121            sb.append(it.next().toString());
122            if (it.hasNext()) {
123                sb.append(",");
124            }
125        }
126
127        return sb.toString();
128    }
129}