Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 2064 invoked from network); 26 Apr 2003 14:24:52 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 26 Apr 2003 14:24:52 -0000 Received: (qmail 26536 invoked by uid 97); 26 Apr 2003 14:26:51 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@nagoya.betaversion.org Received: (qmail 26529 invoked from network); 26 Apr 2003 14:26:51 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 26 Apr 2003 14:26:51 -0000 Received: (qmail 1736 invoked by uid 500); 26 Apr 2003 14:24:49 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 1724 invoked by uid 500); 26 Apr 2003 14:24:49 -0000 Received: (qmail 1720 invoked from network); 26 Apr 2003 14:24:49 -0000 Received: from icarus.apache.org (208.185.179.13) by daedalus.apache.org with SMTP; 26 Apr 2003 14:24:49 -0000 Received: (qmail 21125 invoked by uid 1529); 26 Apr 2003 14:24:48 -0000 Date: 26 Apr 2003 14:24:48 -0000 Message-ID: <20030426142448.21124.qmail@icarus.apache.org> From: scolebourne@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections MapUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N scolebourne 2003/04/26 07:24:48 Modified: collections/src/java/org/apache/commons/collections MapUtils.java Log: Add invertMap(Map) method Add toMap(ResourceBundle) method from Arun Mammen Thomas Revision Changes Path 1.21 +72 -29 jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java Index: MapUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/MapUtils.java,v retrieving revision 1.20 retrieving revision 1.21 diff -u -r1.20 -r1.21 --- MapUtils.java 7 Apr 2003 16:57:33 -0000 1.20 +++ MapUtils.java 26 Apr 2003 14:24:48 -0000 1.21 @@ -1,13 +1,10 @@ /* * $Header$ - * $Revision$ - * $Date$ - * * ==================================================================== * * The Apache Software License, Version 1.1 * - * Copyright (c) 1999-2002 The Apache Software Foundation. All rights + * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -65,19 +62,22 @@ import java.text.ParseException; import java.util.Collections; import java.util.Comparator; +import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; +import java.util.ResourceBundle; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; + /** - * A helper class for using {@link Map Map} instances.

- * + * A helper class for using {@link Map Map} instances. + *

* It contains various typesafe methods - * as well as other useful features like deep copying.

- * + * as well as other useful features like deep copying. + *

* It also provides the following decorators: * *

    @@ -91,13 +91,16 @@ *
  • {@link #typedSortedMap(Map, Class, Class)} *
* - * @since 1.0 + * @since Commons Collections 1.0 + * @version $Revision$ $Date$ + * * @author James Strachan * @author Nissim Karpenstein * @author Kasper Nielsen * @author Paul Jack * @author Stephen Colebourne * @author Matthew Hawthorne + * @author Arun Mammen Thomas */ public class MapUtils { @@ -634,6 +637,25 @@ return answer; } + /** + * Creates a new HashMap using data copied from a ResourceBundle. + * + * @param resourceBundle the resource bundle to convert + * @return the hashmap containing the data + */ + public static Map toMap(ResourceBundle resourceBundle) { + Enumeration enum = resourceBundle.getKeys(); + Map map = new HashMap(); + + while (enum.hasMoreElements()) { + String key = (String) enum.nextElement(); + Object value = resourceBundle.getObject(key); + map.put(key, value); + } + + return map; + } + // Printing methods //------------------------------------------------------------------------- @@ -710,27 +732,49 @@ //------------------------------------------------------------------------- /** - * Writes indentation to the given stream. + * Writes indentation to the given stream. * - * @param out the stream to indent + * @param out the stream to indent */ - protected static void debugPrintIndent( PrintStream out ) { - for ( int i = 0; i < debugIndent; i++ ) { - out.print( " " ); + protected static void debugPrintIndent(PrintStream out) { + for (int i = 0; i < debugIndent; i++) { + out.print(" "); } } /** - * Logs the given exception to System.out. + * Logs the given exception to System.out. * - * @param e the exception to log + * @param ex the exception to log */ - protected static void logInfo(Exception e) { - // mapX: should probably use log4j here instead... - System.out.println( "INFO: Exception: " + e ); + protected static void logInfo(Exception ex) { + System.out.println("INFO: Exception: " + ex); } - + // Misc + //----------------------------------------------------------------------- + /** + * Inverts the supplied map returning a new HashMap such that the keys of + * the input are swapped with the values. + *

+ * This operation assumes that the inverse mapping is well defined. + * If the input map had multiple entries with the same value mapped to + * different keys, the returned map will map one of those keys to the + * value, but the exact key which will be mapped is undefined. + * + * @see DoubleOrderedMap + * @param map the map to invert + * @return a new HashMap containing the inverted data + */ + public static Map invertMap(Map map) { + Map out = new HashMap(map.size()); + for (Iterator it = map.entrySet().iterator(); it.hasNext();) { + Map.Entry entry = (Map.Entry) it.next(); + out.put(entry.getValue(), entry.getKey()); + } + return out; + } + /** * Nice method for adding data to a map in such a way * as to not get NPE's. The point being that if the @@ -739,16 +783,15 @@ * essentially treat put("Not Null", null ) == put("Not Null", "") * We will still throw a NPE if the key is null cause that should * never happen. + * + * @param map the map to add to + * @param key the key + * @param value the value */ - public static final void safeAddToMap(Map map, Object key, Object value) - throws NullPointerException - { - if (value == null) - { + public static void safeAddToMap(Map map, Object key, Object value) throws NullPointerException { + if (value == null) { map.put ( key, "" ); - } - else - { + } else { map.put ( key, value ); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-dev-help@jakarta.apache.org