1 24 package org.ofbiz.base.util; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import java.util.TreeMap ; 35 36 import javax.mail.Address ; 37 38 import javolution.util.FastList; 39 import javolution.util.FastMap; 40 41 import org.ofbiz.base.util.collections.MapComparator; 42 43 51 public class UtilMisc { 52 53 public static final String module = UtilMisc.class.getName(); 54 55 60 public static Iterator toIterator(Collection col) { 61 if (col == null) 62 return null; 63 else 64 return col.iterator(); 65 } 66 67 71 public static Map toMap(String name1, Object value1) { 72 return new UtilMisc.SimpleMap(name1, value1); 73 74 77 } 78 79 83 public static Map toMap(String name1, Object value1, String name2, Object value2) { 84 return new UtilMisc.SimpleMap(name1, value1, name2, value2); 85 86 90 } 91 92 96 public static Map toMap(String name1, Object value1, String name2, Object value2, String name3, Object value3) { 97 return new UtilMisc.SimpleMap(name1, value1, name2, value2, name3, value3); 98 99 104 } 105 106 110 public static Map toMap(String name1, Object value1, String name2, Object value2, String name3, 111 Object value3, String name4, Object value4) { 112 return new UtilMisc.SimpleMap(name1, value1, name2, value2, name3, value3, name4, value4); 113 114 120 } 121 122 126 public static Map toMap(String name1, Object value1, String name2, Object value2, String name3, Object value3, 127 String name4, Object value4, String name5, Object value5) { 128 Map fields = FastMap.newInstance(); 129 130 fields.put(name1, value1); 131 fields.put(name2, value2); 132 fields.put(name3, value3); 133 fields.put(name4, value4); 134 fields.put(name5, value5); 135 return fields; 136 } 137 138 142 public static Map toMap(String name1, Object value1, String name2, Object value2, String name3, Object value3, 143 String name4, Object value4, String name5, Object value5, String name6, Object value6) { 144 Map fields = FastMap.newInstance(); 145 146 fields.put(name1, value1); 147 fields.put(name2, value2); 148 fields.put(name3, value3); 149 fields.put(name4, value4); 150 fields.put(name5, value5); 151 fields.put(name6, value6); 152 return fields; 153 } 154 155 159 public static Map toMap(Object [] data) { 160 if (data == null) { 161 return null; 162 } 163 if (data.length % 2 == 1) { 164 throw new IllegalArgumentException ("You must pass an even sized array to the toMap method"); 165 } 166 Map map = FastMap.newInstance(); 167 for (int i = 0; i < data.length; ) { 168 map.put(data[i++], data[i++]); 169 } 170 return map; 171 } 172 173 public static String printMap(Map theMap) { 174 StringBuffer theBuf = new StringBuffer (); 175 Iterator entryIter = theMap.entrySet().iterator(); 176 while (entryIter.hasNext()) { 177 Map.Entry entry = (Map.Entry ) entryIter.next(); 178 theBuf.append(entry.getKey()); 179 theBuf.append(" --> "); 180 theBuf.append(entry.getValue()); 181 theBuf.append("\n"); 182 } 183 return theBuf.toString(); 184 } 185 186 192 public static List sortMaps(List listOfMaps, List sortKeys) { 193 if (listOfMaps == null || sortKeys == null) 194 return null; 195 List toSort = FastList.newInstance(); 196 toSort.addAll(listOfMaps); 197 try { 198 MapComparator mc = new MapComparator(sortKeys); 199 Collections.sort(toSort, mc); 200 } catch (Exception e) { 201 Debug.logError(e, "Problems sorting list of maps; returning null.", module); 202 return null; 203 } 204 return toSort; 205 } 206 207 public static Object removeFirst(List lst) { 208 return lst.remove(0); 209 } 210 211 215 public static List toList(Object obj1) { 216 List list = new ArrayList (1); 217 218 list.add(obj1); 219 return list; 220 } 221 222 226 public static List toList(Object obj1, Object obj2) { 227 List list = new ArrayList (2); 228 229 list.add(obj1); 230 list.add(obj2); 231 return list; 232 } 233 234 238 public static List toList(Object obj1, Object obj2, Object obj3) { 239 List list = new ArrayList (3); 240 241 list.add(obj1); 242 list.add(obj2); 243 list.add(obj3); 244 return list; 245 } 246 247 251 public static List toList(Object obj1, Object obj2, Object obj3, Object obj4) { 252 List list = new ArrayList (4); 253 254 list.add(obj1); 255 list.add(obj2); 256 list.add(obj3); 257 list.add(obj4); 258 return list; 259 } 260 261 265 public static List toList(Object obj1, Object obj2, Object obj3, Object obj4, Object obj5) { 266 List list = new ArrayList (5); 267 268 list.add(obj1); 269 list.add(obj2); 270 list.add(obj3); 271 list.add(obj4); 272 list.add(obj5); 273 return list; 274 } 275 276 280 public static List toList(Object obj1, Object obj2, Object obj3, Object obj4, Object obj5, Object obj6) { 281 List list = new ArrayList (6); 282 283 list.add(obj1); 284 list.add(obj2); 285 list.add(obj3); 286 list.add(obj4); 287 list.add(obj5); 288 list.add(obj6); 289 return list; 290 } 291 292 public static List toList(Collection collection) { 293 if (collection == null) return null; 294 if (collection instanceof List ) { 295 return (List ) collection; 296 } else { 297 return new ArrayList (collection); 298 } 299 } 300 301 public static List toListArray(Object [] data) { 302 if (data == null) { 303 return null; 304 } 305 List list = new ArrayList (data.length); 306 for (int i = 0; i < data.length; i++) { 307 list.add(data[i]); 308 } 309 return list; 310 } 311 312 public static long toLong(Object value) { 313 if (value != null) { 314 if (value instanceof Long ) { 315 return ((Long ) value).longValue(); 316 } else if (value instanceof String ) { 317 return Long.parseLong((String ) value); 318 } 319 } 320 return 0; 321 } 322 323 328 public static double toDouble(Object value) { 329 if (value != null) { 330 if (value instanceof Double ) { 331 return ((Double ) value).doubleValue(); 332 } else if (value instanceof String ) { 333 return Double.parseDouble((String ) value); 334 } 335 } 336 return 0.0; 337 } 338 339 345 public static void addToDoubleInMap(Map theMap, Object key, Double value) { 346 Double curValue = (Double ) theMap.get(key); 347 if (curValue != null) { 348 theMap.put(key, new Double (curValue.doubleValue() + value.doubleValue())); 349 } else { 350 theMap.put(key, value); 351 } 352 } 353 354 359 public static Locale parseLocale(String localeString) { 360 if (localeString == null || localeString.length() == 0) { 361 return null; 362 } 363 364 Locale locale = null; 365 if (localeString.length() == 2) { 366 locale = new Locale (localeString); 368 } else if (localeString.length() == 5) { 369 String language = localeString.substring(0, 2); 371 String country = localeString.substring(3, 5); 372 locale = new Locale (language, country); 373 } else if (localeString.length() > 6) { 374 String language = localeString.substring(0, 2); 376 String country = localeString.substring(3, 5); 377 String extension = localeString.substring(6); 378 locale = new Locale (language, country, extension); 379 } else { 380 Debug.logWarning("Do not know what to do with the localeString [" + localeString + "], should be length 2, 5, or greater than 6, returning null", module); 381 } 382 383 return locale; 384 } 385 386 389 public static Locale ensureLocale(Object localeObject) { 390 if (localeObject != null && localeObject instanceof String ) { 391 localeObject = UtilMisc.parseLocale((String ) localeObject); 392 } 393 if (localeObject != null && localeObject instanceof Locale ) { 394 return (Locale ) localeObject; 395 } 396 return Locale.getDefault(); 397 } 398 399 public static List availableLocaleList = null; 400 401 public static List availableLocales() { 402 if (availableLocaleList == null) { 403 synchronized(UtilMisc.class) { 404 if (availableLocaleList == null) { 405 TreeMap localeMap = new TreeMap (); 406 Locale [] locales = Locale.getAvailableLocales(); 407 for (int i = 0; i < locales.length; i++) { 408 localeMap.put(locales[i].getDisplayName(), locales[i]); 409 } 410 availableLocaleList = new LinkedList (localeMap.values()); 411 } 412 } 413 } 414 return availableLocaleList; 415 } 416 417 418 protected static class SimpleMap implements Map , java.io.Serializable { 419 protected Map realMapIfNeeded = null; 420 421 String [] names; 422 Object [] values; 423 424 public SimpleMap() { 425 names = new String [0]; 426 values = new Object [0]; 427 } 428 429 public SimpleMap(String name1, Object value1) { 430 names = new String [1]; 431 values = new Object [1]; 432 this.names[0] = name1; 433 this.values[0] = value1; 434 } 435 436 public SimpleMap(String name1, Object value1, String name2, Object value2) { 437 names = new String [2]; 438 values = new Object [2]; 439 this.names[0] = name1; 440 this.values[0] = value1; 441 this.names[1] = name2; 442 this.values[1] = value2; 443 } 444 445 public SimpleMap(String name1, Object value1, String name2, Object value2, String name3, Object value3) { 446 names = new String [3]; 447 values = new Object [3]; 448 this.names[0] = name1; 449 this.values[0] = value1; 450 this.names[1] = name2; 451 this.values[1] = value2; 452 this.names[2] = name3; 453 this.values[2] = value3; 454 } 455 456 public SimpleMap(String name1, Object value1, String name2, Object value2, String name3, Object value3, String name4, Object value4) { 457 names = new String [4]; 458 values = new Object [4]; 459 this.names[0] = name1; 460 this.values[0] = value1; 461 this.names[1] = name2; 462 this.values[1] = value2; 463 this.names[2] = name3; 464 this.values[2] = value3; 465 this.names[3] = name4; 466 this.values[3] = value4; 467 } 468 469 protected void makeRealMap() { 470 realMapIfNeeded = FastMap.newInstance(); 471 for (int i = 0; i < names.length; i++) { 472 realMapIfNeeded.put(names[i], values[i]); 473 } 474 this.names = null; 475 this.values = null; 476 } 477 478 public void clear() { 479 if (realMapIfNeeded != null) { 480 realMapIfNeeded.clear(); 481 } else { 482 realMapIfNeeded = FastMap.newInstance(); 483 names = null; 484 values = null; 485 } 486 } 487 488 public boolean containsKey(Object obj) { 489 if (realMapIfNeeded != null) { 490 return realMapIfNeeded.containsKey(obj); 491 } else { 492 for (int i = 0; i < names.length; i++) { 493 if (obj == null && names[i] == null) return true; 494 if (names[i] != null && names[i].equals(obj)) return true; 495 } 496 return false; 497 } 498 } 499 500 public boolean containsValue(Object obj) { 501 if (realMapIfNeeded != null) { 502 return realMapIfNeeded.containsValue(obj); 503 } else { 504 for (int i = 0; i < names.length; i++) { 505 if (obj == null && values[i] == null) return true; 506 if (values[i] != null && values[i].equals(obj)) return true; 507 } 508 return false; 509 } 510 } 511 512 public java.util.Set entrySet() { 513 if (realMapIfNeeded != null) { 514 return realMapIfNeeded.entrySet(); 515 } else { 516 this.makeRealMap(); 517 return realMapIfNeeded.entrySet(); 518 } 519 } 520 521 public Object get(Object obj) { 522 if (realMapIfNeeded != null) { 523 return realMapIfNeeded.get(obj); 524 } else { 525 for (int i = 0; i < names.length; i++) { 526 if (obj == null && names[i] == null) return values[i]; 527 if (names[i] != null && names[i].equals(obj)) return values[i]; 528 } 529 return null; 530 } 531 } 532 533 public boolean isEmpty() { 534 if (realMapIfNeeded != null) { 535 return realMapIfNeeded.isEmpty(); 536 } else { 537 if (this.names.length == 0) return true; 538 return false; 539 } 540 } 541 542 public java.util.Set keySet() { 543 if (realMapIfNeeded != null) { 544 return realMapIfNeeded.keySet(); 545 } else { 546 this.makeRealMap(); 547 return realMapIfNeeded.keySet(); 548 } 549 } 550 551 public Object put(Object obj, Object obj1) { 552 if (realMapIfNeeded != null) { 553 return realMapIfNeeded.put(obj, obj1); 554 } else { 555 this.makeRealMap(); 556 return realMapIfNeeded.put(obj, obj1); 557 } 558 } 559 560 public void putAll(java.util.Map map) { 561 if (realMapIfNeeded != null) { 562 realMapIfNeeded.putAll(map); 563 } else { 564 this.makeRealMap(); 565 realMapIfNeeded.putAll(map); 566 } 567 } 568 569 public Object remove(Object obj) { 570 if (realMapIfNeeded != null) { 571 return realMapIfNeeded.remove(obj); 572 } else { 573 this.makeRealMap(); 574 return realMapIfNeeded.remove(obj); 575 } 576 } 577 578 public int size() { 579 if (realMapIfNeeded != null) { 580 return realMapIfNeeded.size(); 581 } else { 582 return this.names.length; 583 } 584 } 585 586 public java.util.Collection values() { 587 if (realMapIfNeeded != null) { 588 return realMapIfNeeded.values(); 589 } else { 590 this.makeRealMap(); 591 return realMapIfNeeded.values(); 592 } 593 } 594 595 public String toString() { 596 if (realMapIfNeeded != null) { 597 return realMapIfNeeded.toString(); 598 } else { 599 StringBuffer outString = new StringBuffer ("{"); 600 for (int i = 0; i < names.length; i++) { 601 if (i > 0) outString.append(','); 602 outString.append('{'); 603 outString.append(names[i]); 604 outString.append(','); 605 outString.append(values[i]); 606 outString.append('}'); 607 } 608 outString.append('}'); 609 return outString.toString(); 610 } 611 } 612 613 public int hashCode() { 614 if (realMapIfNeeded != null) { 615 return realMapIfNeeded.hashCode(); 616 } else { 617 int hashCode = 0; 618 for (int i = 0; i < names.length; i++) { 619 int tempNum = (names[i] == null ? 0 : names[i].hashCode()) ^ 621 (values[i] == null ? 0 : values[i].hashCode()); 622 hashCode += tempNum; 623 } 624 return hashCode; 625 } 626 } 627 628 public boolean equals(Object obj) { 629 if (realMapIfNeeded != null) { 630 return realMapIfNeeded.equals(obj); 631 } else { 632 Map mapObj = (Map ) obj; 633 634 if (mapObj.size() != names.length) return false; 636 637 for (int i = 0; i < names.length; i++) { 639 if (!mapObj.containsKey(names[i])) return false; 641 642 Object mapValue = mapObj.get(names[i]); 644 if (mapValue == null) { 645 if (values[i] != null) return false; 646 } else { 647 if (!mapValue.equals(values[i])) return false; 648 } 649 } 650 651 return true; 652 } 653 } 654 } 655 } 656 | Popular Tags |