diff --git a/src/zutil/Dumpable.java b/src/zutil/Dumpable.java deleted file mode 100644 index 8e93c0b..0000000 --- a/src/zutil/Dumpable.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2015 Ziver Koc - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package zutil; - -/** - * If an class implements this interface and runs it through - * MultiPrintStream.dump() then all the values in the object - * will be written out. - * - * @author Ziver - * - */ -public interface Dumpable { - -} diff --git a/src/zutil/io/MultiPrintStream.java b/src/zutil/io/MultiPrintStream.java old mode 100644 new mode 100755 index 4f8f0cf..1acaa28 --- a/src/zutil/io/MultiPrintStream.java +++ b/src/zutil/io/MultiPrintStream.java @@ -24,11 +24,13 @@ package zutil.io; -import zutil.Dumpable; + +import zutil.ClassUtil; import java.io.*; import java.lang.reflect.Array; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -180,51 +182,60 @@ public class MultiPrintStream extends PrintStream { } /** - * Dumps the content of: - *
- Array content - *
- Map content (HashMap etc.) - *
- List content (ArrayList, LinkedList etc.) - *
- InputStream content (Prints out until the end of the stream) - *
- Reader content (Prints out until the end of the reader) - *
- Instance variables of a Object + * Same as {@link #dump(Object, int)} but prints to this OutputStream. * - * @param o is the Object to dump + * @param o is the Object to dump */ - public void dump( Object o ){ - println(dumpToString( o )); + public void dump(Object o){ + println(dumpToString( o, 1)); } + /** + * Same as {@link #dump(Object, int)} but prints to this OutputStream. + * + * @param o is the Object to dump + * @param depth sets the object dump depth, the object recursion depth + */ + public void dump(Object o, int depth){ + println(dumpToString( o, depth)); + } + /** - * Dumps the content of: - *
- Array content - *
- Map content (HashMap etc.) - *
- List content (ArrayList, LinkedList etc.) - *
- InputStream content (Prints out until the end of the stream) - *
- Reader content (Prints out until the end of the reader) - *
- Instance variables of a Object - * - * @param o is the Object to dump + * Same as {@link #dump(Object, int)} + * + * @param o is the Object to dump * @return a String with all the printed data */ - public String dumpToString( Object o) { - return dumpToString(o, ""); + public static String dumpToString(Object o) { + return dumpToString(o, 1); } + + /** + * Dumps the content of: + *
- Array content + *
- Map content (HashMap etc.) + *
- List content (ArrayList, LinkedList etc.) + *
- InputStream content (Prints out until the end of the stream) + *
- Reader content (Prints out until the end of the reader) + *
- Instance variables of a Object + * + * @param o is the Object to dump + * @param depth sets the object dump depth, the object recursion depth + */ + public static String dumpToString(Object o, int depth) { + return dumpToString(o, "", depth); + } /** - * Dumps the content of: - *
- Array content - *
- Map content (HashMap etc.) - *
- List content (ArrayList, LinkedList etc.) - *
- InputStream content (Prints out until the end of the stream) - *
- Reader content (Prints out until the end of the reader) - *
- Instance variables of a Object - * + * See {@link #dumpToString(Object)} + * * @param o is the Object to dump * @param head is the string that will be put in front of every line + * @param depth sets the object dump depth, the object recursion depth * @return A String with all the printed data */ - private String dumpToString( Object o , String head) { + private static String dumpToString(Object o , String head, int depth) { if(o == null) return "NULL"; StringBuffer buffer = new StringBuffer(); @@ -238,7 +249,8 @@ public class MultiPrintStream extends PrintStream { Object value = Array.get(o,i); buffer.append("\n"); buffer.append(nextHead); - buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) ); + buffer.append( (dumbCapable(value, depth-1) ? + dumpToString(value, nextHead, depth-1) : value) ); if ( i+1 it = ((Collection)o).iterator(); - buffer.append( "{" ); + buffer.append( "[" ); while(it.hasNext()){ Object value = it.next(); buffer.append("\n"); buffer.append(nextHead); - buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) ); + buffer.append( (dumbCapable(value, depth-1) ? + dumpToString(value, nextHead, depth-1) : value) ); if(it.hasNext()) buffer.append( "," ); } buffer.append( "\n" ); buffer.append(head); - buffer.append( "}" ); + buffer.append( "]" ); } // Prints out a Map else if(o instanceof Map){ @@ -273,7 +286,8 @@ public class MultiPrintStream extends PrintStream { buffer.append(nextHead); buffer.append( key ); buffer.append( "=>" ); - buffer.append( (dumbCapable(value) ? dumpToString(value, nextHead) : value) ); + buffer.append( (dumbCapable(value, depth-1) ? + dumpToString(value, nextHead, depth-1) : value) ); if(it.hasNext()) buffer.append( "," ); } @@ -293,7 +307,7 @@ public class MultiPrintStream extends PrintStream { } in.close(); } catch (IOException e) { - e.printStackTrace(this); + e.printStackTrace(); } buffer.append( "\n" ); buffer.append(head); @@ -311,7 +325,7 @@ public class MultiPrintStream extends PrintStream { } in.close(); } catch (IOException e) { - e.printStackTrace(this); + e.printStackTrace(); } buffer.append( "\n" ); buffer.append(head); @@ -323,17 +337,20 @@ public class MultiPrintStream extends PrintStream { while ( oClass != null ) { Field[] fields = oClass.getDeclaredFields(); for ( int i=0; i