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