1 10 11 package org.mule.util; 12 13 import java.util.Arrays ; 14 import java.util.Collection ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import org.apache.commons.lang.SystemUtils; 20 21 public class MapUtils extends org.apache.commons.collections.MapUtils 23 { 24 25 29 public static Map mapWithKeysAndValues(Class mapClass, Object [] keys, Object [] values) 30 { 31 Collection keyCollection = (keys != null ? Arrays.asList(keys) : Collections.EMPTY_LIST); 32 Collection valuesCollection = (values != null ? Arrays.asList(values) : Collections.EMPTY_LIST); 33 return mapWithKeysAndValues(mapClass, keyCollection.iterator(), valuesCollection.iterator()); 34 } 35 36 40 public static Map mapWithKeysAndValues(Class mapClass, Collection keys, Collection values) 41 { 42 keys = (keys != null ? keys : Collections.EMPTY_LIST); 43 values = (values != null ? values : Collections.EMPTY_LIST); 44 return mapWithKeysAndValues(mapClass, keys.iterator(), values.iterator()); 45 } 46 47 56 public static Map mapWithKeysAndValues(Class mapClass, Iterator keys, Iterator values) 57 { 58 Map m = null; 59 60 if (mapClass == null) 61 { 62 throw new IllegalArgumentException ("Map class must not be null!"); 63 } 64 65 try 66 { 67 m = (Map )mapClass.newInstance(); 68 } 69 catch (Exception ex) 70 { 71 throw new RuntimeException (ex); 72 } 73 74 if (keys != null && values != null) 75 { 76 while (keys.hasNext() && values.hasNext()) 77 { 78 m.put(keys.next(), values.next()); 79 } 80 } 81 82 return m; 83 } 84 85 93 public static String toString(Map props, boolean newline) 94 { 95 if (props == null || props.isEmpty()) 96 { 97 return "{}"; 98 } 99 100 StringBuffer buf = new StringBuffer (props.size() * 32); 101 buf.append('{'); 102 103 if (newline) 104 { 105 buf.append(SystemUtils.LINE_SEPARATOR); 106 } 107 108 Object [] entries = props.entrySet().toArray(); 109 int i; 110 111 for (i = 0; i < entries.length - 1; i++) 112 { 113 Map.Entry property = (Map.Entry )entries[i]; 114 buf.append(property.getKey()); 115 buf.append('='); 116 buf.append(PropertiesUtils.maskedPropertyValue(property)); 117 118 if (newline) 119 { 120 buf.append(SystemUtils.LINE_SEPARATOR); 121 } 122 else 123 { 124 buf.append(',').append(' '); 125 } 126 } 127 128 Map.Entry lastProperty = (Map.Entry )entries[i]; 130 buf.append(lastProperty.getKey().toString()); 131 buf.append('='); 132 buf.append(PropertiesUtils.maskedPropertyValue(lastProperty)); 133 134 if (newline) 135 { 136 buf.append(SystemUtils.LINE_SEPARATOR); 137 } 138 139 buf.append('}'); 140 return buf.toString(); 141 } 142 143 } 144 | Popular Tags |