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.Arrays;
022    import java.util.Collection;
023    import java.util.HashSet;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import org.w3c.dom.NodeList;
030    
031    /**
032     * A number of helper methods for working with collections
033     *
034     * @version $Revision: 785479 $
035     */
036    public 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 map, Object key, Object value) {
082            Object oldValue = map.get(key);
083            if (oldValue != null) {
084                List list;
085                if (oldValue instanceof List) {
086                    list = (List)oldValue;
087                } else {
088                    list = new ArrayList();
089                    list.add(oldValue);
090                }
091                list.add(value);
092            } else {
093                map.put(key, value);
094            }
095        }
096    
097        /**
098         * Filters the given list to skip instanceof filter objects.
099         * 
100         * @param list  the list
101         * @param filters  objects to skip
102         * @return a new list without the filtered objects
103         */
104        @SuppressWarnings("unchecked")
105        public static List filterList(List list, Object... filters) {
106            List answer = new ArrayList();
107            for (Object o : list) {
108                for (Object filter : filters) {
109                    if (!o.getClass().isInstance(filter)) {
110                        answer.add(o);
111                    }
112                }
113            }
114            return answer;
115        }
116    
117        public static <T> Set<T> createSetContaining(T... contents) {
118            Set<T> contentsAsSet = new HashSet<T>();
119            contentsAsSet.addAll(Arrays.asList(contents));
120            return contentsAsSet;
121        }
122    
123        public static String collectionAsCommaDelimitedString(String[] col) {
124            if (col == null || col.length == 0) {
125                return "";
126            }
127            return collectionAsCommaDelimitedString(Arrays.asList(col));
128        }
129    
130        public static String collectionAsCommaDelimitedString(Collection col) {
131            if (col == null || col.isEmpty()) {
132                return "";
133            }
134    
135            StringBuilder sb = new StringBuilder();
136            Iterator it = col.iterator();
137            while (it.hasNext()) {
138                sb.append(it.next());
139                if (it.hasNext()) {
140                    sb.append(",");
141                }
142            }
143    
144            return sb.toString();
145        }
146        
147    }