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.Collections; 024import java.util.HashMap; 025import java.util.HashSet; 026import java.util.Iterator; 027import java.util.LinkedHashMap; 028import java.util.List; 029import java.util.Map; 030import java.util.Properties; 031import java.util.Set; 032import java.util.function.Supplier; 033 034import org.w3c.dom.NodeList; 035 036/** 037 * A number of helper methods for working with collections 038 */ 039public final class CollectionHelper { 040 041 /** 042 * Utility classes should not have a public constructor. 043 */ 044 private CollectionHelper() { 045 } 046 047 /** 048 * Returns the size of the collection if it can be determined to be a collection 049 * 050 * @param value the collection 051 * @return the size, or <tt>null</tt> if not a collection 052 */ 053 public static Integer size(Object value) { 054 if (value != null) { 055 if (value instanceof Collection) { 056 Collection<?> collection = (Collection<?>) value; 057 return collection.size(); 058 } else if (value instanceof Map) { 059 Map<?, ?> map = (Map<?, ?>) value; 060 return map.size(); 061 } else if (value instanceof Object[]) { 062 Object[] array = (Object[]) value; 063 return array.length; 064 } else if (value.getClass().isArray()) { 065 return Array.getLength(value); 066 } else if (value instanceof NodeList) { 067 NodeList nodeList = (NodeList) value; 068 return nodeList.getLength(); 069 } 070 } 071 return null; 072 } 073 074 /** 075 * Sets the value of the entry in the map for the given key, though if the 076 * map already contains a value for the given key then the value is appended 077 * to a list of values. 078 * 079 * @param map the map to add the entry to 080 * @param key the key in the map 081 * @param value the value to put in the map 082 */ 083 @SuppressWarnings("unchecked") 084 public static void appendValue(Map<String, Object> map, String key, Object value) { 085 Object oldValue = map.get(key); 086 if (oldValue != null) { 087 List<Object> list; 088 if (oldValue instanceof List) { 089 list = (List<Object>) oldValue; 090 } else { 091 list = new ArrayList<>(); 092 list.add(oldValue); 093 // replace old entry with list 094 map.remove(key); 095 map.put(key, list); 096 } 097 list.add(value); 098 } else { 099 map.put(key, value); 100 } 101 } 102 103 public static <T> Set<T> createSetContaining(T... contents) { 104 return new HashSet<>(Arrays.asList(contents)); 105 } 106 107 public static String collectionAsCommaDelimitedString(Collection<?> col) { 108 if (col == null || col.isEmpty()) { 109 return ""; 110 } 111 112 StringBuilder sb = new StringBuilder(); 113 Iterator<?> it = col.iterator(); 114 while (it.hasNext()) { 115 sb.append(it.next().toString()); 116 if (it.hasNext()) { 117 sb.append(","); 118 } 119 } 120 121 return sb.toString(); 122 } 123 124 /** 125 * Traverses the given map recursively and flattern the keys by combining them with the optional separator. 126 * 127 * @param map the map 128 * @param separator optional separator to use in key name, for example a hyphen or dot. 129 * @return the map with flattern keys 130 */ 131 public static Map<String, Object> flattenKeysInMap(Map<String, Object> map, String separator) { 132 Map<String, Object> answer = new LinkedHashMap<>(); 133 doFlattenKeysInMap(map, "", ObjectHelper.isNotEmpty(separator) ? separator : "", answer); 134 return answer; 135 } 136 137 private static void doFlattenKeysInMap(Map<String, Object> source, String prefix, String separator, Map<String, Object> target) { 138 for (Map.Entry<String, Object> entry : source.entrySet()) { 139 String key = entry.getKey(); 140 Object value = entry.getValue(); 141 String newKey = prefix.isEmpty() ? key : prefix + separator + key; 142 143 if (value instanceof Map) { 144 Map map = (Map) value; 145 doFlattenKeysInMap(map, newKey, separator, target); 146 } else { 147 target.put(newKey, value); 148 } 149 } 150 } 151 152 /** 153 * Build an unmodifiable map on top of a given map. Note tha thew given map is 154 * copied if not null. 155 * 156 * @param map a map 157 * @return an unmodifiable map. 158 */ 159 public static <K, V> Map<K, V> unmodifiableMap(Map<K, V> map) { 160 return map == null 161 ? Collections.emptyMap() 162 : Collections.unmodifiableMap(new HashMap<>(map)); 163 } 164 165 166 /** 167 * Build a map from varargs. 168 */ 169 public static <K, V> Map<K, V> mapOf(Supplier<Map<K, V>> creator, K key, V value, Object... keyVals) { 170 Map<K, V> map = creator.get(); 171 map.put(key, value); 172 173 for (int i = 0; i < keyVals.length; i += 2) { 174 map.put( 175 (K) keyVals[i], 176 (V) keyVals[i + 1] 177 ); 178 } 179 180 return map; 181 } 182 183 184 /** 185 * Build an immutable map from varargs. 186 */ 187 public static <K, V> Map<K, V> immutableMapOf(Supplier<Map<K, V>> creator, K key, V value, Object... keyVals) { 188 return Collections.unmodifiableMap( 189 mapOf(creator, key, value, keyVals) 190 ); 191 } 192 193 /** 194 * Build a map from varargs. 195 */ 196 public static <K, V> Map<K, V> mapOf(K key, V value, Object... keyVals) { 197 return mapOf(HashMap::new, key, value, keyVals); 198 } 199 200 /** 201 * Build an immutable map from varargs. 202 */ 203 public static <K, V> Map<K, V> immutableMapOf(K key, V value, Object... keyVals) { 204 return Collections.unmodifiableMap( 205 mapOf(HashMap::new, key, value, keyVals) 206 ); 207 } 208 209 /** 210 * Build a {@link java.util.Properties} from varargs. 211 */ 212 public static Properties propertiesOf(String key, String value, String... keyVals) { 213 Properties properties = new Properties(); 214 properties.setProperty(key, value); 215 216 for (int i = 0; i < keyVals.length; i += 2) { 217 properties.setProperty( 218 keyVals[i], 219 keyVals[i + 1] 220 ); 221 } 222 223 return properties; 224 } 225}