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: 1927 $ 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 * @param value the collection 044 * @return the size, or <tt>null</tt> if not a collection 045 */ 046 public static Integer size(Object value) { 047 if (value != null) { 048 if (value instanceof Collection) { 049 Collection collection = (Collection)value; 050 return collection.size(); 051 } else if (value instanceof Map) { 052 Map map = (Map)value; 053 return map.size(); 054 } else if (value instanceof Object[]) { 055 Object[] array = (Object[])value; 056 return array.length; 057 } else if (value.getClass().isArray()) { 058 return Array.getLength(value); 059 } else if (value instanceof NodeList) { 060 NodeList nodeList = (NodeList)value; 061 return nodeList.getLength(); 062 } 063 } 064 return null; 065 } 066 067 /** 068 * Sets the value of the entry in the map for the given key, though if the 069 * map already contains a value for the given key then the value is appended 070 * to a list of values. 071 * 072 * @param map the map to add the entry to 073 * @param key the key in the map 074 * @param value the value to put in the map 075 */ 076 public static void appendValue(Map map, Object key, Object value) { 077 Object oldValue = map.get(key); 078 if (oldValue != null) { 079 List list; 080 if (oldValue instanceof List) { 081 list = (List)oldValue; 082 } else { 083 list = new ArrayList(); 084 list.add(oldValue); 085 } 086 list.add(value); 087 } else { 088 map.put(key, value); 089 } 090 } 091 092 /** 093 * Filters the given list to skip instanceof filter objects. 094 * 095 * @param list the list 096 * @param filters objects to skip 097 * @return a new list without the filtered objects 098 */ 099 public static List filterList(List list, Object... filters) { 100 List answer = new ArrayList(); 101 for (Object o : list) { 102 for (Object filter : filters) { 103 if (!o.getClass().isInstance(filter)) { 104 answer.add(o); 105 } 106 } 107 } 108 return answer; 109 } 110 }