scolebourne 2003/09/20 04:26:33 Modified: collections/src/java/org/apache/commons/collections MapUtils.java Log: Make MapUtils threadsafe and remove synchronized keyword. Change is partially backwards incompatible: - protected method is removed - two threads outputting a Map may now overlap Revision Changes Path 1.35 +35 -35 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.34 retrieving revision 1.35 diff -u -r1.34 -r1.35 --- MapUtils.java 17 Sep 2003 19:59:45 -0000 1.34 +++ MapUtils.java 20 Sep 2003 11:26:32 -0000 1.35 @@ -82,7 +82,7 @@ import org.apache.commons.collections.decorators.TypedSortedMap; /** - * A helper class for using [EMAIL PROTECTED] Map Map} instances. + * Provides useful utility methods for [EMAIL PROTECTED] Map Map} instances. * <p> * It contains various typesafe methods * as well as other useful features like deep copying. @@ -129,9 +129,9 @@ * This is not provided in the JDK. */ public static final SortedMap EMPTY_SORTED_MAP = Collections.unmodifiableSortedMap(new TreeMap()); - - private static int indentDepth = 0; // must be synchronized - + /** + * String used to indent the verbose and debug Map prints. + */ private static final String INDENT_STRING = " "; /** @@ -142,7 +142,6 @@ // Type safe getters //------------------------------------------------------------------------- - /** * Synonym for [EMAIL PROTECTED] Map#get(Object)}. * @@ -410,7 +409,6 @@ // Type safe getters with default values //------------------------------------------------------------------------- - /** * Looks up the given key in the given map, converting null into the * given default value. @@ -633,7 +631,6 @@ // Conversion methods //------------------------------------------------------------------------- - /** * Gets a new Properties object initialised with the values from a Map. * A null input will return an empty properties object. @@ -676,13 +673,15 @@ // Printing methods //------------------------------------------------------------------------- - /** * Prints the given map with nice line breaks. * <p> * This method prints a nicely formatted String describing the Map. * Each map entry will be printed with key and value. * When the value is a Map, recursive behaviour occurs. + * <p> + * This method is NOT thread-safe in any special way. You must manually + * synchronize on either this class or the stream as required. * * @param out the stream to print to, must not be null * @param label The label to be used, may be <code>null</code>. @@ -692,12 +691,11 @@ * If <code>null</code>, the text 'null' is output. * @throws NullPointerException if the stream is <code>null</code> */ - public static synchronized void verbosePrint( + public static void verbosePrint( final PrintStream out, final Object label, final Map map) { - indentDepth = 0; verbosePrintInternal(out, label, map, new ArrayStack(), false); } @@ -707,6 +705,9 @@ * This method prints a nicely formatted String describing the Map. * Each map entry will be printed with key, value and value classname. * When the value is a Map, recursive behaviour occurs. + * <p> + * This method is NOT thread-safe in any special way. You must manually + * synchronize on either this class or the stream as required. * * @param out the stream to print to, must not be null * @param label The label to be used, may be <code>null</code>. @@ -716,31 +717,20 @@ * If <code>null</code>, the text 'null' is output. * @throws NullPointerException if the stream is <code>null</code> */ - public static synchronized void debugPrint( + public static void debugPrint( final PrintStream out, final Object label, final Map map) { - indentDepth = 0; verbosePrintInternal(out, label, map, new ArrayStack(), true); } // Implementation methods //------------------------------------------------------------------------- - - /** - * Writes indentation to the given stream. - * - * @param out the stream to indent - */ - protected static void printIndent(final PrintStream out) { - for (int i = 0; i < indentDepth; i++) { - out.print(INDENT_STRING); - } - } - /** * Logs the given exception to <code>System.out</code>. + * <p> + * This method exists as Jakarta Collections does not depend on logging. * * @param ex the exception to log */ @@ -768,18 +758,17 @@ * @param lineage a stack consisting of any maps in which the previous * argument is contained. This is checked to avoid infinite recursion when * printing the output - * - * @param debug flag indicating whether type names should be output. + * @param debug flag indicating whether type names should be output. * @throws NullPointerException if the stream is <code>null</code> */ - private static void verbosePrintInternal( // externally synchronized + private static void verbosePrintInternal( final PrintStream out, final Object label, final Map map, final ArrayStack lineage, final boolean debug) { - printIndent(out); + printIndent(out, lineage.size()); if (map == null) { if (label != null) { @@ -794,10 +783,9 @@ out.println(" = "); } - printIndent(out); + printIndent(out, lineage.size()); out.println("{"); - indentDepth++; lineage.push(map); for (Iterator it = map.entrySet().iterator(); it.hasNext();) { @@ -812,7 +800,7 @@ lineage, debug); } else { - printIndent(out); + printIndent(out, lineage.size()); out.print(childKey); out.print(" = "); @@ -838,12 +826,22 @@ } lineage.pop(); - indentDepth--; - printIndent(out); + printIndent(out, lineage.size()); out.println(debug ? "} " + map.getClass().getName() : "}"); } + /** + * Writes indentation to the given stream. + * + * @param out the stream to indent + */ + private static void printIndent(final PrintStream out, final int indent) { + for (int i = 0; i < indent; i++) { + out.print(INDENT_STRING); + } + } + // Misc //----------------------------------------------------------------------- /** @@ -891,6 +889,7 @@ } } + // Map decorators //----------------------------------------------------------------------- /** * Returns a synchronized map backed by the given map. @@ -1065,6 +1064,7 @@ return LazyMap.decorate(map, transformerFactory); } + // SortedMap decorators //----------------------------------------------------------------------- /** * Returns a synchronized sorted map backed by the given sorted map.
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]