1 16 package org.apache.commons.collections; 17 18 import java.io.PrintStream ; 19 import java.text.NumberFormat ; 20 import java.text.ParseException ; 21 import java.util.Collections ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.ResourceBundle ; 28 import java.util.SortedMap ; 29 import java.util.TreeMap ; 30 31 import org.apache.commons.collections.map.FixedSizeMap; 32 import org.apache.commons.collections.map.FixedSizeSortedMap; 33 import org.apache.commons.collections.map.LazyMap; 34 import org.apache.commons.collections.map.LazySortedMap; 35 import org.apache.commons.collections.map.ListOrderedMap; 36 import org.apache.commons.collections.map.PredicatedMap; 37 import org.apache.commons.collections.map.PredicatedSortedMap; 38 import org.apache.commons.collections.map.TransformedMap; 39 import org.apache.commons.collections.map.TransformedSortedMap; 40 import org.apache.commons.collections.map.TypedMap; 41 import org.apache.commons.collections.map.TypedSortedMap; 42 import org.apache.commons.collections.map.UnmodifiableMap; 43 import org.apache.commons.collections.map.UnmodifiableSortedMap; 44 45 83 public class MapUtils { 84 85 89 public static final Map EMPTY_MAP = UnmodifiableMap.decorate(new HashMap (1)); 90 94 public static final SortedMap EMPTY_SORTED_MAP = UnmodifiableSortedMap.decorate(new TreeMap ()); 95 98 private static final String INDENT_STRING = " "; 99 100 103 public MapUtils() { 104 } 105 106 115 public static Object getObject(final Map map, final Object key) { 116 if (map != null) { 117 return map.get(key); 118 } 119 return null; 120 } 121 122 131 public static String getString(final Map map, final Object key) { 132 if (map != null) { 133 Object answer = map.get(key); 134 if (answer != null) { 135 return answer.toString(); 136 } 137 } 138 return null; 139 } 140 141 155 public static Boolean getBoolean(final Map map, final Object key) { 156 if (map != null) { 157 Object answer = map.get(key); 158 if (answer != null) { 159 if (answer instanceof Boolean ) { 160 return (Boolean ) answer; 161 162 } else if (answer instanceof String ) { 163 return new Boolean ((String ) answer); 164 165 } else if (answer instanceof Number ) { 166 Number n = (Number ) answer; 167 return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE; 168 } 169 } 170 } 171 return null; 172 } 173 174 187 public static Number getNumber(final Map map, final Object key) { 188 if (map != null) { 189 Object answer = map.get(key); 190 if (answer != null) { 191 if (answer instanceof Number ) { 192 return (Number ) answer; 193 194 } else if (answer instanceof String ) { 195 try { 196 String text = (String ) answer; 197 return NumberFormat.getInstance().parse(text); 198 199 } catch (ParseException e) { 200 logInfo(e); 201 } 202 } 203 } 204 } 205 return null; 206 } 207 208 217 public static Byte getByte(final Map map, final Object key) { 218 Number answer = getNumber(map, key); 219 if (answer == null) { 220 return null; 221 } else if (answer instanceof Byte ) { 222 return (Byte ) answer; 223 } 224 return new Byte (answer.byteValue()); 225 } 226 227 236 public static Short getShort(final Map map, final Object key) { 237 Number answer = getNumber(map, key); 238 if (answer == null) { 239 return null; 240 } else if (answer instanceof Short ) { 241 return (Short ) answer; 242 } 243 return new Short (answer.shortValue()); 244 } 245 246 255 public static Integer getInteger(final Map map, final Object key) { 256 Number answer = getNumber(map, key); 257 if (answer == null) { 258 return null; 259 } else if (answer instanceof Integer ) { 260 return (Integer ) answer; 261 } 262 return new Integer (answer.intValue()); 263 } 264 265 274 public static Long getLong(final Map map, final Object key) { 275 Number answer = getNumber(map, key); 276 if (answer == null) { 277 return null; 278 } else if (answer instanceof Long ) { 279 return (Long ) answer; 280 } 281 return new Long (answer.longValue()); 282 } 283 284 293 public static Float getFloat(final Map map, final Object key) { 294 Number answer = getNumber(map, key); 295 if (answer == null) { 296 return null; 297 } else if (answer instanceof Float ) { 298 return (Float ) answer; 299 } 300 return new Float (answer.floatValue()); 301 } 302 303 312 public static Double getDouble(final Map map, final Object key) { 313 Number answer = getNumber(map, key); 314 if (answer == null) { 315 return null; 316 } else if (answer instanceof Double ) { 317 return (Double ) answer; 318 } 319 return new Double (answer.doubleValue()); 320 } 321 322 332 public static Map getMap(final Map map, final Object key) { 333 if (map != null) { 334 Object answer = map.get(key); 335 if (answer != null && answer instanceof Map ) { 336 return (Map ) answer; 337 } 338 } 339 return null; 340 } 341 342 354 public static Object getObject( Map map, Object key, Object defaultValue ) { 355 if ( map != null ) { 356 Object answer = map.get( key ); 357 if ( answer != null ) { 358 return answer; 359 } 360 } 361 return defaultValue; 362 } 363 364 376 public static String getString( Map map, Object key, String defaultValue ) { 377 String answer = getString( map, key ); 378 if ( answer == null ) { 379 answer = defaultValue; 380 } 381 return answer; 382 } 383 384 396 public static Boolean getBoolean( Map map, Object key, Boolean defaultValue ) { 397 Boolean answer = getBoolean( map, key ); 398 if ( answer == null ) { 399 answer = defaultValue; 400 } 401 return answer; 402 } 403 404 416 public static Number getNumber( Map map, Object key, Number defaultValue ) { 417 Number answer = getNumber( map, key ); 418 if ( answer == null ) { 419 answer = defaultValue; 420 } 421 return answer; 422 } 423 424 436 public static Byte getByte( Map map, Object key, Byte defaultValue ) { 437 Byte answer = getByte( map, key ); 438 if ( answer == null ) { 439 answer = defaultValue; 440 } 441 return answer; 442 } 443 444 456 public static Short getShort( Map map, Object key, Short defaultValue ) { 457 Short answer = getShort( map, key ); 458 if ( answer == null ) { 459 answer = defaultValue; 460 } 461 return answer; 462 } 463 464 476 public static Integer getInteger( Map map, Object key, Integer defaultValue ) { 477 Integer answer = getInteger( map, key ); 478 if ( answer == null ) { 479 answer = defaultValue; 480 } 481 return answer; 482 } 483 484 496 public static Long getLong( Map map, Object key, Long defaultValue ) { 497 Long answer = getLong( map, key ); 498 if ( answer == null ) { 499 answer = defaultValue; 500 } 501 return answer; 502 } 503 504 516 public static Float getFloat( Map map, Object key, Float defaultValue ) { 517 Float answer = getFloat( map, key ); 518 if ( answer == null ) { 519 answer = defaultValue; 520 } 521 return answer; 522 } 523 524 536 public static Double getDouble( Map map, Object key, Double defaultValue ) { 537 Double answer = getDouble( map, key ); 538 if ( answer == null ) { 539 answer = defaultValue; 540 } 541 return answer; 542 } 543 544 556 public static Map getMap( Map map, Object key, Map defaultValue ) { 557 Map answer = getMap( map, key ); 558 if ( answer == null ) { 559 answer = defaultValue; 560 } 561 return answer; 562 } 563 564 565 581 public static boolean getBooleanValue(final Map map, final Object key) { 582 Boolean booleanObject = getBoolean(map, key); 583 if (booleanObject == null) { 584 return false; 585 } 586 return booleanObject.booleanValue(); 587 } 588 589 598 public static byte getByteValue(final Map map, final Object key) { 599 Byte byteObject = getByte(map, key); 600 if (byteObject == null) { 601 return 0; 602 } 603 return byteObject.byteValue(); 604 } 605 606 615 public static short getShortValue(final Map map, final Object key) { 616 Short shortObject = getShort(map, key); 617 if (shortObject == null) { 618 return 0; 619 } 620 return shortObject.shortValue(); 621 } 622 623 632 public static int getIntValue(final Map map, final Object key) { 633 Integer integerObject = getInteger(map, key); 634 if (integerObject == null) { 635 return 0; 636 } 637 return integerObject.intValue(); 638 } 639 640 649 public static long getLongValue(final Map map, final Object key) { 650 Long longObject = getLong(map, key); 651 if (longObject == null) { 652 return 0L; 653 } 654 return longObject.longValue(); 655 } 656 657 666 public static float getFloatValue(final Map map, final Object key) { 667 Float floatObject = getFloat(map, key); 668 if (floatObject == null) { 669 return 0f; 670 } 671 return floatObject.floatValue(); 672 } 673 674 683 public static double getDoubleValue(final Map map, final Object key) { 684 Double doubleObject = getDouble(map, key); 685 if (doubleObject == null) { 686 return 0d; 687 } 688 return doubleObject.doubleValue(); 689 } 690 691 710 public static boolean getBooleanValue(final Map map, final Object key, boolean defaultValue) { 711 Boolean booleanObject = getBoolean(map, key); 712 if (booleanObject == null) { 713 return defaultValue; 714 } 715 return booleanObject.booleanValue(); 716 } 717 718 730 public static byte getByteValue(final Map map, final Object key, byte defaultValue) { 731 Byte byteObject = getByte(map, key); 732 if (byteObject == null) { 733 return defaultValue; 734 } 735 return byteObject.byteValue(); 736 } 737 738 750 public static short getShortValue(final Map map, final Object key, short defaultValue) { 751 Short shortObject = getShort(map, key); 752 if (shortObject == null) { 753 return defaultValue; 754 } 755 return shortObject.shortValue(); 756 } 757 758 770 public static int getIntValue(final Map map, final Object key, int defaultValue) { 771 Integer integerObject = getInteger(map, key); 772 if (integerObject == null) { 773 return defaultValue; 774 } 775 return integerObject.intValue(); 776 } 777 778 790 public static long getLongValue(final Map map, final Object key, long defaultValue) { 791 Long longObject = getLong(map, key); 792 if (longObject == null) { 793 return defaultValue; 794 } 795 return longObject.longValue(); 796 } 797 798 810 public static float getFloatValue(final Map map, final Object key, float defaultValue) { 811 Float floatObject = getFloat(map, key); 812 if (floatObject == null) { 813 return defaultValue; 814 } 815 return floatObject.floatValue(); 816 } 817 818 830 public static double getDoubleValue(final Map map, final Object key, double defaultValue) { 831 Double doubleObject = getDouble(map, key); 832 if (doubleObject == null) { 833 return defaultValue; 834 } 835 return doubleObject.doubleValue(); 836 } 837 838 847 public static Properties toProperties(final Map map) { 848 Properties answer = new Properties (); 849 if (map != null) { 850 for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { 851 Map.Entry entry = (Map.Entry ) iter.next(); 852 Object key = entry.getKey(); 853 Object value = entry.getValue(); 854 answer.put(key, value); 855 } 856 } 857 return answer; 858 } 859 860 867 public static Map toMap(final ResourceBundle resourceBundle) { 868 Enumeration enumeration = resourceBundle.getKeys(); 869 Map map = new HashMap (); 870 871 while (enumeration.hasMoreElements()) { 872 String key = (String ) enumeration.nextElement(); 873 Object value = resourceBundle.getObject(key); 874 map.put(key, value); 875 } 876 877 return map; 878 } 879 880 900 public static void verbosePrint( 901 final PrintStream out, 902 final Object label, 903 final Map map) { 904 905 verbosePrintInternal(out, label, map, new ArrayStack(), false); 906 } 907 908 926 public static void debugPrint( 927 final PrintStream out, 928 final Object label, 929 final Map map) { 930 931 verbosePrintInternal(out, label, map, new ArrayStack(), true); 932 } 933 934 943 protected static void logInfo(final Exception ex) { 944 System.out.println("INFO: Exception: " + ex); 945 } 946 947 970 private static void verbosePrintInternal( 971 final PrintStream out, 972 final Object label, 973 final Map map, 974 final ArrayStack lineage, 975 final boolean debug) { 976 977 printIndent(out, lineage.size()); 978 979 if (map == null) { 980 if (label != null) { 981 out.print(label); 982 out.print(" = "); 983 } 984 out.println("null"); 985 return; 986 } 987 if (label != null) { 988 out.print(label); 989 out.println(" = "); 990 } 991 992 printIndent(out, lineage.size()); 993 out.println("{"); 994 995 lineage.push(map); 996 997 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 998 Map.Entry entry = (Map.Entry ) it.next(); 999 Object childKey = entry.getKey(); 1000 Object childValue = entry.getValue(); 1001 if (childValue instanceof Map && !lineage.contains(childValue)) { 1002 verbosePrintInternal( 1003 out, 1004 (childKey == null ? "null" : childKey), 1005 (Map ) childValue, 1006 lineage, 1007 debug); 1008 } else { 1009 printIndent(out, lineage.size()); 1010 out.print(childKey); 1011 out.print(" = "); 1012 1013 final int lineageIndex = lineage.indexOf(childValue); 1014 if (lineageIndex == -1) { 1015 out.print(childValue); 1016 } else if (lineage.size() - 1 == lineageIndex) { 1017 out.print("(this Map)"); 1018 } else { 1019 out.print( 1020 "(ancestor[" 1021 + (lineage.size() - 1 - lineageIndex - 1) 1022 + "] Map)"); 1023 } 1024 1025 if (debug && childValue != null) { 1026 out.print(' '); 1027 out.println(childValue.getClass().getName()); 1028 } else { 1029 out.println(); 1030 } 1031 } 1032 } 1033 1034 lineage.pop(); 1035 1036 printIndent(out, lineage.size()); 1037 out.println(debug ? "} " + map.getClass().getName() : "}"); 1038 } 1039 1040 1045 private static void printIndent(final PrintStream out, final int indent) { 1046 for (int i = 0; i < indent; i++) { 1047 out.print(INDENT_STRING); 1048 } 1049 } 1050 1051 1067 public static Map invertMap(Map map) { 1068 Map out = new HashMap (map.size()); 1069 for (Iterator it = map.entrySet().iterator(); it.hasNext();) { 1070 Map.Entry entry = (Map.Entry ) it.next(); 1071 out.put(entry.getValue(), entry.getKey()); 1072 } 1073 return out; 1074 } 1075 1076 1090 public static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException { 1091 if (value == null) { 1092 map.put ( key, "" ); 1093 } else { 1094 map.put ( key, value ); 1095 } 1096 } 1097 1098 1123 public static Map synchronizedMap(Map map) { 1124 return Collections.synchronizedMap(map); 1125 } 1126 1127 1136 public static Map unmodifiableMap(Map map) { 1137 return UnmodifiableMap.decorate(map); 1138 } 1139 1140 1155 public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred) { 1156 return PredicatedMap.decorate(map, keyPred, valuePred); 1157 } 1158 1159 1170 public static Map typedMap(Map map, Class keyType, Class valueType) { 1171 return TypedMap.decorate(map, keyType, valueType); 1172 } 1173 1174 1187 public static Map transformedMap(Map map, Transformer keyTransformer, Transformer valueTransformer) { 1188 return TransformedMap.decorate(map, keyTransformer, valueTransformer); 1189 } 1190 1191 1201 public static Map fixedSizeMap(Map map) { 1202 return FixedSizeMap.decorate(map); 1203 } 1204 1205 1233 public static Map lazyMap(Map map, Factory factory) { 1234 return LazyMap.decorate(map, factory); 1235 } 1236 1237 1272 public static Map lazyMap(Map map, Transformer transformerFactory) { 1273 return LazyMap.decorate(map, transformerFactory); 1274 } 1275 1276 1287 public static Map orderedMap(Map map) { 1288 return ListOrderedMap.decorate(map); 1289 } 1290 1291 1316 public static Map synchronizedSortedMap(SortedMap map) { 1317 return Collections.synchronizedSortedMap(map); 1318 } 1319 1320 1329 public static Map unmodifiableSortedMap(SortedMap map) { 1330 return UnmodifiableSortedMap.decorate(map); 1331 } 1332 1333 1348 public static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred) { 1349 return PredicatedSortedMap.decorate(map, keyPred, valuePred); 1350 } 1351 1352 1362 public static SortedMap typedSortedMap(SortedMap map, Class keyType, Class valueType) { 1363 return TypedSortedMap.decorate(map, keyType, valueType); 1364 } 1365 1366 1379 public static SortedMap transformedSortedMap(SortedMap map, Transformer keyTransformer, Transformer valueTransformer) { 1380 return TransformedSortedMap.decorate(map, keyTransformer, valueTransformer); 1381 } 1382 1383 1393 public static SortedMap fixedSizeSortedMap(SortedMap map) { 1394 return FixedSizeSortedMap.decorate(map); 1395 } 1396 1397 1426 public static SortedMap lazySortedMap(SortedMap map, Factory factory) { 1427 return LazySortedMap.decorate(map, factory); 1428 } 1429 1430 1465 public static SortedMap lazySortedMap(SortedMap map, Transformer transformerFactory) { 1466 return LazySortedMap.decorate(map, transformerFactory); 1467 } 1468 1469} 1470 | Popular Tags |