You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/08/20 23:03:16 UTC
cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections MapUtils.java
scolebourne 2003/08/20 14:03:16
Modified: collections/src/test/org/apache/commons/collections
TestMapUtils.java
collections/src/java/org/apache/commons/collections
MapUtils.java
Log:
Fix debugPrint(Map) methods to handle more than just String keys
bug 20740, from Max Rydahl Anderson
Revision Changes Path
1.6 +25 -3 jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java
Index: TestMapUtils.java
===================================================================
RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMapUtils.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestMapUtils.java 26 Apr 2003 14:27:46 -0000 1.5
+++ TestMapUtils.java 20 Aug 2003 21:03:16 -0000 1.6
@@ -57,6 +57,8 @@
*/
package org.apache.commons.collections;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
@@ -67,7 +69,6 @@
import junit.framework.Test;
-
/**
* Tests for MapUtils.
*
@@ -75,6 +76,7 @@
*
* @author Stephen Colebourne
* @author Arun Mammen Thomas
+ * @author Max Rydahl Andersen
*/
public class TestMapUtils extends BulkTest {
@@ -309,5 +311,25 @@
final Map out = MapUtils.toMap(b);
assertTrue( in.equals(out));
+ }
+
+ public void testDebugAndVerbosePrintCasting() {
+ final Map inner = new HashMap(2, 1);
+ inner.put( new Integer(2) , "B" );
+ inner.put( new Integer(3) , "C" );
+
+ final Map outer = new HashMap(2, 1);
+ outer.put( new Integer(0) , inner );
+ outer.put( new Integer(1) , "A");
+
+
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
+ final PrintStream outPrint = new PrintStream(out);
+
+ try {
+ MapUtils.debugPrint(outPrint, "Print Map", outer);
+ } catch (final ClassCastException e) {
+ fail("No Casting should be occurring!");
+ }
}
}
1.26 +72 -53 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.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- MapUtils.java 20 Jun 2003 07:50:21 -0000 1.25
+++ MapUtils.java 20 Aug 2003 21:03:16 -0000 1.26
@@ -115,6 +115,7 @@
* @author Matthew Hawthorne
* @author Arun Mammen Thomas
* @author Janek Bogucki
+ * @author Max Rydahl Andersen
*/
public class MapUtils {
@@ -679,69 +680,34 @@
* Prints the given map with nice line breaks.
*
* @param out the stream to print to
- * @param key the key that maps to the map in some other map
+ * @param label the label to be applied to the output generated. This
+ * may well be the key associated with this map within a
+ * surrounding map in which this is nested.
* @param map the map to print
*/
- public static synchronized void verbosePrint( PrintStream out, Object key, Map map ) {
- debugPrintIndent( out );
- out.println( key + " = " );
+ public static synchronized void verbosePrint(
+ final PrintStream out,
+ final Object label,
+ final Map map) {
- debugPrintIndent( out );
- out.println( "{" );
- ++debugIndent;
-
- for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
- Map.Entry entry = (Map.Entry) iter.next();
- String childKey = (String) entry.getKey();
- Object childValue = entry.getValue();
- if ( childValue instanceof Map ) {
- verbosePrint( out, childKey, (Map) childValue );
- }
- else {
- debugPrintIndent( out );
- out.println( childKey + " = " + childValue);
- }
- }
- --debugIndent;
- debugPrintIndent( out );
- out.println( "}" );
+ verbosePrintInternal(out, label, map, false);
}
/**
* Prints the given map with nice line breaks.
*
* @param out the stream to print to
- * @param key the key that maps to the map in some other map
+ * @param label the label to be applied to the output generated. This
+ * may well be the key associated with this map within a
+ * surrounding map in which this is nested.
* @param map the map to print
*/
- public static synchronized void debugPrint( PrintStream out, Object key, Map map ) {
- debugPrintIndent( out );
- out.println( key + " = " );
+ public static synchronized void debugPrint(
+ final PrintStream out,
+ final Object label,
+ final Map map) {
- debugPrintIndent( out );
- out.println( "{" );
- ++debugIndent;
-
- for ( Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
- Map.Entry entry = (Map.Entry) iter.next();
- String childKey = (String) entry.getKey();
- Object childValue = entry.getValue();
- if ( childValue instanceof Map ) {
- verbosePrint( out, childKey, (Map) childValue );
- }
- else {
- debugPrintIndent( out );
-
- String typeName = ( childValue != null )
- ? childValue.getClass().getName()
- : null;
-
- out.println( childKey + " = " + childValue + " class: " + typeName );
- }
- }
- --debugIndent;
- debugPrintIndent( out );
- out.println( "}" );
+ verbosePrintInternal(out, label, map, true);
}
// Implementation methods
@@ -765,6 +731,59 @@
*/
protected static void logInfo(Exception ex) {
System.out.println("INFO: Exception: " + ex);
+ }
+
+ /**
+ * Implementation providing functionality for {@link #debugPrint} and for
+ * {@link #verbosePrint}. This prints the given map with nice line breaks.
+ * If the debug flag is true, it additionally prints the type of the object
+ * value. .
+ *
+ * @param out the stream to print to
+ * @param label the label to be applied to the output generated. This
+ * may well be the key associated with this map within a
+ * surrounding map in which this is nested.
+ * @param map the map to print
+ * @param debug flag indicating whether type names should be output.
+ */
+ private static void verbosePrintInternal(
+ final PrintStream out,
+ final Object label,
+ final Map map,
+ final boolean debug) {
+
+ debugPrintIndent(out);
+ out.println(label + " = ");
+
+ debugPrintIndent(out);
+ out.println("{");
+ ++debugIndent;
+
+ for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
+ Map.Entry entry = (Map.Entry) iter.next();
+ Object childKey = entry.getKey();
+ Object childValue = entry.getValue();
+ if (childValue instanceof Map) {
+ verbosePrintInternal(out, childKey, (Map) childValue, false);
+ } else {
+ debugPrintIndent(out);
+
+ if (debug) {
+ String typeName =
+ (childValue != null)
+ ? childValue.getClass().getName()
+ : null;
+
+ out.println(
+ childKey + " = " + childValue + " class: " + typeName);
+ } else {
+ out.println(childKey + " = " + childValue);
+ }
+ }
+ }
+ --debugIndent;
+ debugPrintIndent(out);
+ out.println("}");
}
// Misc