Fix debugPrint(Map) methods to handle more than just String keys
bug 20740, from Max Rydahl Anderson git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131112 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
421a30912d
commit
b4c1122160
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.25 2003/06/20 07:50:21 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/MapUtils.java,v 1.26 2003/08/20 21:03:16 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -105,7 +105,7 @@ import org.apache.commons.collections.decorators.TypedSortedMap;
|
|||
* </ul>
|
||||
*
|
||||
* @since Commons Collections 1.0
|
||||
* @version $Revision: 1.25 $ $Date: 2003/06/20 07:50:21 $
|
||||
* @version $Revision: 1.26 $ $Date: 2003/08/20 21:03:16 $
|
||||
*
|
||||
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
|
||||
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
|
||||
|
@ -115,6 +115,7 @@ import org.apache.commons.collections.decorators.TypedSortedMap;
|
|||
* @author Matthew Hawthorne
|
||||
* @author Arun Mammen Thomas
|
||||
* @author Janek Bogucki
|
||||
* @author Max Rydahl Andersen
|
||||
*/
|
||||
public class MapUtils {
|
||||
|
||||
|
@ -679,69 +680,34 @@ public class MapUtils {
|
|||
* 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
|
||||
|
@ -767,6 +733,59 @@ public class MapUtils {
|
|||
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
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMapUtils.java,v 1.5 2003/04/26 14:27:46 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMapUtils.java,v 1.6 2003/08/20 21:03:16 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -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,14 +69,14 @@ import java.util.Set;
|
|||
|
||||
import junit.framework.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for MapUtils.
|
||||
*
|
||||
* @version $Revision: 1.5 $ $Date: 2003/04/26 14:27:46 $
|
||||
* @version $Revision: 1.6 $ $Date: 2003/08/20 21:03:16 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
* @author Arun Mammen Thomas
|
||||
* @author Max Rydahl Andersen
|
||||
*/
|
||||
public class TestMapUtils extends BulkTest {
|
||||
|
||||
|
@ -310,4 +312,24 @@ public class TestMapUtils extends BulkTest {
|
|||
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue