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.LinkedHashMap; 026import java.util.List; 027import java.util.Map; 028import java.util.Set; 029 030import org.w3c.dom.NodeList; 031 032/** 033 * A number of helper methods for working with collections 034 * 035 * @version 036 */ 037public final class CollectionHelper { 038 039 /** 040 * Utility classes should not have a public constructor. 041 */ 042 private CollectionHelper() { 043 } 044 045 /** 046 * Returns the size of the collection if it can be determined to be a collection 047 * 048 * @param value the collection 049 * @return the size, or <tt>null</tt> if not a collection 050 */ 051 public static Integer size(Object value) { 052 if (value != null) { 053 if (value instanceof Collection) { 054 Collection<?> collection = (Collection<?>) value; 055 return collection.size(); 056 } else if (value instanceof Map) { 057 Map<?, ?> map = (Map<?, ?>) value; 058 return map.size(); 059 } else if (value instanceof Object[]) { 060 Object[] array = (Object[]) value; 061 return array.length; 062 } else if (value.getClass().isArray()) { 063 return Array.getLength(value); 064 } else if (value instanceof NodeList) { 065 NodeList nodeList = (NodeList) value; 066 return nodeList.getLength(); 067 } 068 } 069 return null; 070 } 071 072 /** 073 * Sets the value of the entry in the map for the given key, though if the 074 * map already contains a value for the given key then the value is appended 075 * to a list of values. 076 * 077 * @param map the map to add the entry to 078 * @param key the key in the map 079 * @param value the value to put in the map 080 */ 081 @SuppressWarnings("unchecked") 082 public static void appendValue(Map<String, Object> map, String key, Object value) { 083 Object oldValue = map.get(key); 084 if (oldValue != null) { 085 List<Object> list; 086 if (oldValue instanceof List) { 087 list = (List<Object>) oldValue; 088 } else { 089 list = new ArrayList<Object>(); 090 list.add(oldValue); 091 // replace old entry with list 092 map.remove(key); 093 map.put(key, list); 094 } 095 list.add(value); 096 } else { 097 map.put(key, value); 098 } 099 } 100 101 public static <T> Set<T> createSetContaining(T... contents) { 102 Set<T> contentsAsSet = new HashSet<T>(); 103 contentsAsSet.addAll(Arrays.asList(contents)); 104 return contentsAsSet; 105 } 106 107 public static String collectionAsCommaDelimitedString(String[] col) { 108 if (col == null || col.length == 0) { 109 return ""; 110 } 111 return collectionAsCommaDelimitedString(Arrays.asList(col)); 112 } 113 114 public static String collectionAsCommaDelimitedString(Collection<?> col) { 115 if (col == null || col.isEmpty()) { 116 return ""; 117 } 118 119 StringBuilder sb = new StringBuilder(); 120 Iterator<?> it = col.iterator(); 121 while (it.hasNext()) { 122 sb.append(it.next().toString()); 123 if (it.hasNext()) { 124 sb.append(","); 125 } 126 } 127 128 return sb.toString(); 129 } 130 131 /** 132 * Traverses the given map recursively and flattern the keys by combining them with the optional separator. 133 * 134 * @param map the map 135 * @param separator optional separator to use in key name, for example a hyphen or dot. 136 * @return the map with flattern keys 137 */ 138 public static Map<String, Object> flatternKeysInMap(Map<String, Object> map, String separator) { 139 Map<String, Object> answer = new LinkedHashMap<>(); 140 doFlatternKeysInMap(map, "", ObjectHelper.isNotEmpty(separator) ? separator : "", answer); 141 return answer; 142 } 143 144 private static void doFlatternKeysInMap(Map<String, Object> source, String prefix, String separator, Map<String, Object> target) { 145 for (Map.Entry<String, Object> entry : source.entrySet()) { 146 String key = entry.getKey(); 147 Object value = entry.getValue(); 148 String newKey = prefix.isEmpty() ? key : prefix + separator + key; 149 150 if (value instanceof Map) { 151 Map map = (Map) value; 152 doFlatternKeysInMap(map, newKey, separator, target); 153 } else { 154 target.put(newKey, value); 155 } 156 } 157 } 158}