├── .vs └── slnx.sqlite ├── .gradle ├── buildOutputCleanup │ ├── built.bin │ ├── cache.properties.lock │ └── cache.properties ├── 3.5 │ ├── file-changes │ │ └── last-build.bin │ └── taskHistory │ │ ├── fileHashes.bin │ │ ├── taskHistory.bin │ │ ├── taskHistory.lock │ │ └── fileSnapshots.bin └── 4.0.2 │ ├── fileChanges │ └── last-build.bin │ ├── fileHashes │ ├── fileHashes.bin │ └── fileHashes.lock │ └── taskHistory │ ├── taskHistory.bin │ ├── taskHistory.lock │ └── fileSnapshots.bin ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── bin └── io │ └── github │ └── woodenbell │ └── pprint │ ├── Util.class │ ├── ObjectPrint.class │ ├── SimplePrint.class │ ├── CollectionPrint.class │ ├── PrettyPrintable.class │ ├── Util$TableFormat.class │ └── test │ ├── ObjectTest$1.class │ ├── ObjectTest$2.class │ ├── ObjectTest$3.class │ ├── ObjectTest.class │ ├── PPTestSuite.class │ ├── SimpleTest.class │ ├── CollectionTest.class │ ├── ObjectTest$Person.class │ ├── ObjectTest$DataStruct.class │ └── ObjectTest$DataStructType.class ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.buildship.core.prefs ├── src ├── test │ └── java │ │ └── io │ │ └── github │ │ └── woodenbell │ │ └── pprint │ │ └── test │ │ ├── PPTestSuite.java │ │ ├── SimpleTest.java │ │ ├── CollectionTest.java │ │ └── ObjectTest.java └── main │ └── java │ └── io │ └── github │ └── woodenbell │ └── pprint │ ├── PrettyPrintable.java │ ├── SimplePrint.java │ ├── Util.java │ ├── ObjectPrint.java │ └── CollectionPrint.java ├── .gitattributes ├── .classpath ├── settings.gradle ├── .project ├── .gitignore ├── gradlew.bat ├── gradlew ├── LICENSE └── README.md /.vs/slnx.sqlite: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gradle/buildOutputCleanup/built.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gradle/3.5/file-changes/last-build.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gradle/4.0.2/fileChanges/last-build.bin: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gradle/buildOutputCleanup/cache.properties.lock: -------------------------------------------------------------------------------- 1 |  -------------------------------------------------------------------------------- /.gradle/buildOutputCleanup/cache.properties: -------------------------------------------------------------------------------- 1 | #Fri Sep 22 19:11:17 BRT 2017 2 | gradle.version=3.5 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gradle/3.5/taskHistory/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/3.5/taskHistory/fileHashes.bin -------------------------------------------------------------------------------- /.gradle/3.5/taskHistory/taskHistory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/3.5/taskHistory/taskHistory.bin -------------------------------------------------------------------------------- /.gradle/3.5/taskHistory/taskHistory.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/3.5/taskHistory/taskHistory.lock -------------------------------------------------------------------------------- /.gradle/4.0.2/fileHashes/fileHashes.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/4.0.2/fileHashes/fileHashes.bin -------------------------------------------------------------------------------- /.gradle/4.0.2/fileHashes/fileHashes.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/4.0.2/fileHashes/fileHashes.lock -------------------------------------------------------------------------------- /.gradle/3.5/taskHistory/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/3.5/taskHistory/fileSnapshots.bin -------------------------------------------------------------------------------- /.gradle/4.0.2/taskHistory/taskHistory.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/4.0.2/taskHistory/taskHistory.bin -------------------------------------------------------------------------------- /.gradle/4.0.2/taskHistory/taskHistory.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/4.0.2/taskHistory/taskHistory.lock -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/Util.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/Util.class -------------------------------------------------------------------------------- /.gradle/4.0.2/taskHistory/fileSnapshots.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/.gradle/4.0.2/taskHistory/fileSnapshots.bin -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding//src/main/java/io/github/woodenbell/pprint/Util.java=UTF-8 3 | -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/ObjectPrint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/ObjectPrint.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/SimplePrint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/SimplePrint.class -------------------------------------------------------------------------------- /.settings/org.eclipse.buildship.core.prefs: -------------------------------------------------------------------------------- 1 | connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) 2 | connection.project.dir= 3 | eclipse.preferences.version=1 4 | -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/CollectionPrint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/CollectionPrint.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/PrettyPrintable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/PrettyPrintable.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/Util$TableFormat.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/Util$TableFormat.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest$1.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest$2.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest$3.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/PPTestSuite.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/PPTestSuite.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/SimpleTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/SimpleTest.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/CollectionTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/CollectionTest.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest$Person.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest$Person.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest$DataStruct.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest$DataStruct.class -------------------------------------------------------------------------------- /bin/io/github/woodenbell/pprint/test/ObjectTest$DataStructType.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woodenbell/PrettyPrint/HEAD/bin/io/github/woodenbell/pprint/test/ObjectTest$DataStructType.class -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 11 15:15:32 BRT 2017 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-bin.zip 7 | -------------------------------------------------------------------------------- /src/test/java/io/github/woodenbell/pprint/test/PPTestSuite.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint.test; 2 | 3 | import org.junit.runner.RunWith; 4 | import org.junit.runners.Suite; 5 | 6 | @RunWith(Suite.class) 7 | 8 | @Suite.SuiteClasses({ ObjectTest.class, CollectionTest.class, SimpleTest.class}) 9 | 10 | public class PPTestSuite { 11 | 12 | } 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This settings file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * In a single project build this file can be empty or even removed. 6 | * 7 | * Detailed information about configuring a multi-project build in Gradle can be found 8 | * in the user guide at https://docs.gradle.org/3.5/userguide/multi_project_builds.html 9 | */ 10 | 11 | /* 12 | // To declare projects as part of a multi-project build use the 'include' method 13 | include 'shared' 14 | include 'api' 15 | include 'services:webservice' 16 | */ 17 | 18 | rootProject.name = 'PrettyPrint' 19 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | PrettyPrint 4 | Project PrettyPrint created by Buildship. 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.buildship.core.gradleprojectbuilder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.buildship.core.gradleprojectnature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /src/main/java/io/github/woodenbell/pprint/PrettyPrintable.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint; 2 | 3 | /** 4 | * Interface to be implemented by objects that support pprint. 5 | * 6 | * @author Gabriel C. 7 | * 8 | * @see io.github.woodenbell.pprint.ObjectPrint 9 | */ 10 | 11 | public interface PrettyPrintable { 12 | 13 | /** 14 | * Used to tell if the object supports recursive printing. 15 | * 16 | * @return If the object supports recursive printing. 17 | * @see io.github.woodenbell.pprint.ObjectPrint#pprintRecursive(PrettyPrintable) 18 | */ 19 | 20 | public boolean ppIsRecursive(); 21 | 22 | /** 23 | * Used to tell if the object uses keys or number indexes when enumerated is 24 | * used. 25 | * 26 | * @return If the object uses keys when pretty printing. 27 | */ 28 | 29 | public boolean ppHasKeys(); 30 | 31 | /** 32 | * Method to return the object printing keys. 33 | * 34 | * @return The keys of object's elements. 35 | */ 36 | 37 | public Object[] ppGetKeys(); 38 | 39 | /** 40 | * Method to return the object printing values. 41 | * 42 | * @return The keys of object's values. 43 | */ 44 | 45 | public Object[] ppGetValues(); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/io/github/woodenbell/pprint/SimplePrint.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.Queue; 6 | import java.util.Set; 7 | 8 | /** 9 | * Contains pprint methods that print in a simple format, similar to Python 10 | * representation of data structures. 11 | * 12 | * @author Gabriel C. 13 | * @since 1.1 14 | */ 15 | 16 | public class SimplePrint { 17 | 18 | /** 19 | * Prints an object array in a simple format. 20 | * 21 | * @param p 22 | * The object array. 23 | */ 24 | 25 | public static void pprint(Object[] p) { 26 | 27 | if (p == null) 28 | return; 29 | 30 | System.out.println("[" + Util.toListWithCommas(p) + "]"); 31 | } 32 | 33 | /** 34 | * Prints a List collection in a simple format. 35 | * 36 | * @param p 37 | * The List object. 38 | * 39 | * @see java.util.List 40 | */ 41 | 42 | public static void pprint(List p) { 43 | 44 | if (p == null) 45 | return; 46 | 47 | System.out.println("[" + Util.toListWithCommas(p.toArray()) + "]"); 48 | 49 | } 50 | 51 | /** 52 | * Prints a Set collection in a simple format. 53 | * 54 | * @param p 55 | * The Set object. 56 | * @see java.util.Set 57 | */ 58 | 59 | public static void pprint(Set p) { 60 | 61 | if (p == null) 62 | return; 63 | 64 | System.out.println("{" + Util.toListWithCommas(p.toArray()) + "}"); 65 | } 66 | 67 | /** 68 | * Prints a Queue collection in a simple format. 69 | * 70 | * @param p 71 | * The Queue object. 72 | * @see java.util.Queue 73 | */ 74 | 75 | public static void pprint(Queue p) { 76 | 77 | if (p == null) 78 | return; 79 | 80 | System.out.println("[" + Util.toListWithCommas(p.toArray()) + "]"); 81 | } 82 | 83 | /** 84 | * Prints a Map collection in a simple format. 85 | * 86 | * @param p 87 | * The map object. 88 | * @see java.util.Map 89 | */ 90 | 91 | public static void pprint(Map p) { 92 | 93 | if (p == null) 94 | return; 95 | 96 | System.out.println("{\n" + Util.toKeyValListWithCommas(p.keySet().toArray(), p.values().toArray()) + "\n}"); 97 | 98 | } 99 | 100 | } 101 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/test/java/io/github/woodenbell/pprint/test/SimpleTest.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint.test; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.HashSet; 6 | 7 | import org.junit.Test; 8 | 9 | import io.github.woodenbell.pprint.SimplePrint; 10 | 11 | 12 | public class SimpleTest { 13 | 14 | @Test 15 | public void arrayTest() { 16 | 17 | System.out.println("- Testing object array simple pprint"); 18 | System.out.println(); 19 | 20 | Object[] numbers = new Integer[] {2, 5, 6, 9}; 21 | SimplePrint.pprint(numbers); 22 | 23 | System.out.println(); 24 | System.out.println(); 25 | 26 | Object[] names = new String[] {"wow", "much", null, "array"}; 27 | SimplePrint.pprint(names); 28 | 29 | System.out.println(); 30 | System.out.println(); 31 | } 32 | 33 | @Test 34 | public void listTest() { 35 | 36 | System.out.println("- Testing List simple pprint"); 37 | System.out.println(); 38 | 39 | ArrayList words = new ArrayList<>(); 40 | words.add("world"); 41 | words.add("cup"); 42 | words.add("head"); 43 | 44 | SimplePrint.pprint(words); 45 | 46 | System.out.println(); 47 | System.out.println(); 48 | 49 | ArrayList money = new ArrayList<>(); 50 | money.add(37.8); 51 | money.add(9.43); 52 | money.add(null); 53 | money.add(6.0); 54 | 55 | SimplePrint.pprint(money); 56 | 57 | System.out.println(); 58 | System.out.println(); 59 | } 60 | 61 | 62 | @Test 63 | public void setTest() { 64 | 65 | System.out.println("- Testing Set simple pprint"); 66 | System.out.println(); 67 | 68 | HashSet words = new HashSet<>(); 69 | words.add("really"); 70 | words.add("more"); 71 | words.add("words"); 72 | 73 | SimplePrint.pprint(words); 74 | 75 | System.out.println(); 76 | System.out.println(); 77 | 78 | HashSet money = new HashSet<>(); 79 | money.add(2.8); 80 | money.add(0.0); 81 | money.add(null); 82 | money.add(3.0); 83 | 84 | SimplePrint.pprint(money); 85 | 86 | System.out.println(); 87 | System.out.println(); 88 | 89 | } 90 | 91 | @Test 92 | public void mapTest() { 93 | 94 | System.out.println("- Testing Map simple pprint"); 95 | System.out.println(); 96 | 97 | HashMap ages = new HashMap<>(); 98 | ages.put("John", 31); 99 | ages.put(null, 0); 100 | ages.put("James", 22); 101 | ages.put("Unknown", null); 102 | ages.put(null, null); 103 | ages.put("Pyro", 20); 104 | 105 | SimplePrint.pprint(ages); 106 | 107 | System.out.println(); 108 | System.out.println(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/io/github/woodenbell/pprint/Util.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint; 2 | 3 | /** 4 | * Utility class for the package. 5 | * 6 | * @author Gabriel C. 7 | */ 8 | 9 | public class Util { 10 | 11 | /** 12 | * Enumeration representing table printing formats. 13 | * 14 | * @author WoodenBell 15 | */ 16 | 17 | public static enum TableFormat { 18 | 19 | /** 20 | * UNDERSCORE TableFormat Top/Bottom border character: _ Right/Left border and 21 | * division character: | 22 | */ 23 | 24 | UNDERSCORE('_', '|'), 25 | 26 | /** 27 | * HYPHEN TableFormat Top/Bottom border character: - Right/Left border and 28 | * division character: | 29 | */ 30 | 31 | HYPHEN('-', '|'), 32 | 33 | /** 34 | * EQUALS TableFormat Top/Bottom border character: = Right/Left border and 35 | * division character: | 36 | */ 37 | 38 | EQUALS('=', '|'); 39 | 40 | char border, division; 41 | 42 | /** 43 | * Creates a table format with given border and division. 44 | * 45 | * @param border 46 | * The character to be used as top and bottom border. 47 | * @param division 48 | * The character to be used as division to the elements. 49 | */ 50 | 51 | TableFormat(char border, char division) { 52 | this.border = border; 53 | this.division = division; 54 | } 55 | } 56 | 57 | /** 58 | * Generates padding for table printing. 59 | * 60 | * @param c 61 | * The character used in padding. 62 | * @param times 63 | * The padding size, or the number of times the character will be 64 | * multiplied. 65 | * @return A string containing the padding made with the character. 66 | */ 67 | 68 | protected static String makePadding(char c, int times) { 69 | StringBuilder sb = new StringBuilder(); 70 | for (int i = 1; i <= times; i++) 71 | sb.append(c); 72 | return sb.toString(); 73 | } 74 | 75 | /** 76 | * Creates a comma separated list. 77 | * @param l The array that will be used to create the list string. 78 | * @return The list string. 79 | * @since 1.1 80 | */ 81 | 82 | protected static String toListWithCommas(Object[] l) { 83 | 84 | StringBuilder sb = new StringBuilder(); 85 | 86 | for (Object o : l) { 87 | 88 | if (o == null) { 89 | sb.append("null, "); 90 | } else if(o instanceof String) { 91 | sb.append("\"" + o.toString() + "\", "); 92 | } else { 93 | sb.append(o.toString() + ", "); 94 | } 95 | 96 | } 97 | 98 | return sb.substring(0, sb.length() - 2); 99 | 100 | } 101 | 102 | /** 103 | * Creates a key-value pair list represented in string. 104 | * 105 | * @param ks 106 | * The key array that will be used to create the key-value list. 107 | * @param vs 108 | * The value array that will be used to create the key-value list. 109 | * @return The string key-value pair list, separated by newline characters. 110 | * @since 1.1 111 | */ 112 | 113 | protected static String toKeyValListWithCommas(Object[] ks, Object[] vs) { 114 | 115 | StringBuilder sb = new StringBuilder(); 116 | 117 | for (int i = 0; i < ks.length; i++) { 118 | 119 | if (ks[i] == null) { 120 | sb.append("null: "); 121 | } else if (ks[i] instanceof String) { 122 | sb.append("\"" + ks[i].toString() + "\": "); 123 | } else { 124 | sb.append(ks[i].toString() + ": "); 125 | } 126 | 127 | if (vs[i] == null) { 128 | sb.append("null,\n"); 129 | } else if (vs[i] instanceof String) { 130 | sb.append("\"" + vs[i].toString() + "\",\n"); 131 | } else { 132 | sb.append(vs[i].toString() + ",\n"); 133 | } 134 | 135 | } 136 | 137 | return sb.substring(0, sb.length() - 2); 138 | 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /src/test/java/io/github/woodenbell/pprint/test/CollectionTest.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint.test; 2 | 3 | import org.junit.Test; 4 | 5 | import io.github.woodenbell.pprint.CollectionPrint; 6 | import io.github.woodenbell.pprint.Util; 7 | 8 | import java.util.ArrayDeque; 9 | import java.util.ArrayList; 10 | import java.util.HashMap; 11 | import java.util.HashSet; 12 | 13 | public class CollectionTest { 14 | 15 | @Test 16 | public void ListTest() { 17 | 18 | System.out.println("- Testing List pprint"); 19 | System.out.println(); 20 | 21 | ArrayList people = new ArrayList<>(); 22 | people.add("Sherlock Holmes, 37 years old, lives at 221B baker Street"); 23 | people.add("Me, 16 years old, lives in the middle of nothing"); 24 | people.add("Someone else, ? years old"); 25 | people.add("How many people are left?"); 26 | people.add("People filler #1"); 27 | people.add("People filler #2"); 28 | people.add("John Smith"); 29 | people.add("John Smith II"); 30 | people.add("John Smith III"); 31 | people.add("And, again, John Smith IV"); 32 | 33 | CollectionPrint.pprint(people); 34 | 35 | System.out.println(); 36 | System.out.println(); 37 | 38 | CollectionPrint.pprint(people, true); 39 | 40 | System.out.println(); 41 | System.out.println(); 42 | 43 | CollectionPrint.pprint(people, true, true, Util.TableFormat.EQUALS); 44 | System.out.println(); 45 | System.out.println(); 46 | 47 | } 48 | 49 | @Test 50 | public void MapTest() { 51 | 52 | System.out.println("- Testing Map pprint"); 53 | System.out.println(); 54 | 55 | HashMap balance = new HashMap<>(); 56 | 57 | balance.put("John Smith", 345.66); 58 | balance.put("Aaron Daniels", 120.40); 59 | balance.put("Paul Klein", 12.0); 60 | balance.put("Karl Smith", -35.90); 61 | balance.put("Catherine Durant", 200.00); 62 | balance.put("Klein Smith", 145.66); 63 | 64 | CollectionPrint.pprint(balance); 65 | 66 | System.out.println(); 67 | System.out.println(); 68 | 69 | CollectionPrint.pprint(balance, true); 70 | 71 | System.out.println(); 72 | System.out.println(); 73 | 74 | CollectionPrint.pprint(balance, true, true, Util.TableFormat.UNDERSCORE); 75 | 76 | System.out.println(); 77 | System.out.println(); 78 | } 79 | 80 | @Test 81 | public void SetTest() { 82 | 83 | System.out.println("- Testing Set pprint"); 84 | System.out.println(); 85 | 86 | HashSet firstPrimes = new HashSet<>(); 87 | 88 | firstPrimes.add(1); 89 | firstPrimes.add(2); 90 | firstPrimes.add(3); 91 | firstPrimes.add(5); 92 | firstPrimes.add(7); 93 | firstPrimes.add(11); 94 | firstPrimes.add(13); 95 | firstPrimes.add(17); 96 | firstPrimes.add(19); 97 | firstPrimes.add(23); 98 | 99 | CollectionPrint.pprint(firstPrimes); 100 | 101 | System.out.println(); 102 | System.out.println(); 103 | 104 | CollectionPrint.pprint(firstPrimes, true); 105 | 106 | System.out.println(); 107 | System.out.println(); 108 | 109 | CollectionPrint.pprint(firstPrimes, true, true, Util.TableFormat.HYPHEN); 110 | 111 | System.out.println(); 112 | System.out.println(); 113 | } 114 | 115 | @Test 116 | public void QueueTest() { 117 | 118 | System.out.println("- Testing Queue pprint"); 119 | System.out.println(); 120 | 121 | ArrayDeque firstNums = new ArrayDeque<>(); 122 | 123 | firstNums.add(2); 124 | firstNums.add(3); 125 | firstNums.add(4); 126 | firstNums.add(5); 127 | firstNums.add(6); 128 | firstNums.add(7); 129 | firstNums.add(8); 130 | firstNums.add(9); 131 | 132 | firstNums.addFirst(1); 133 | firstNums.addLast(10); 134 | 135 | CollectionPrint.pprint(firstNums); 136 | 137 | System.out.println(); 138 | System.out.println(); 139 | 140 | CollectionPrint.pprint(firstNums, true); 141 | 142 | System.out.println(); 143 | System.out.println(); 144 | 145 | CollectionPrint.pprint(firstNums, true, true); 146 | 147 | System.out.println(); 148 | System.out.println(); 149 | } 150 | 151 | @Test 152 | public void errorTest() { 153 | 154 | System.out.println("- Testing pprint on collections with null elements or values"); 155 | System.out.println(); 156 | 157 | HashMap hm1 = new HashMap<>(); 158 | HashMap hm2 = new HashMap<>(); 159 | ArrayList al = new ArrayList<>(); 160 | 161 | HashSet hs = new HashSet<>(); 162 | 163 | hm1.put("wow", null); 164 | hm1.put("much null", null); 165 | hm1.put("not null", "3"); 166 | 167 | hm2.put(null, "much null"); 168 | hm2.put("not null", "3"); 169 | 170 | al.add(null); 171 | al.add("wow"); 172 | 173 | hs.add("hey"); 174 | hs.add(null); 175 | 176 | CollectionPrint.pprint(hm1, true); 177 | CollectionPrint.pprint(hm2, true); 178 | CollectionPrint.pprint(hm2); 179 | 180 | CollectionPrint.pprint(al); 181 | 182 | CollectionPrint.pprint(hs); 183 | 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save ( ) { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /src/test/java/io/github/woodenbell/pprint/test/ObjectTest.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint.test; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.junit.Test; 7 | 8 | import io.github.woodenbell.pprint.ObjectPrint; 9 | import io.github.woodenbell.pprint.PrettyPrintable; 10 | import io.github.woodenbell.pprint.Util; 11 | 12 | public class ObjectTest { 13 | 14 | private static class Person implements PrettyPrintable { 15 | 16 | String name; 17 | int age; 18 | String address; 19 | 20 | Person(String name, int age, String address) { 21 | 22 | this.name = name; 23 | this.age = age; 24 | this.address = address; 25 | } 26 | 27 | public boolean ppIsRecursive() { 28 | return false; 29 | } 30 | 31 | public boolean ppHasKeys() { 32 | return true; 33 | } 34 | 35 | public Object[] ppGetKeys() { 36 | return new String[] { "Name", "Age", "Address" }; 37 | } 38 | 39 | public Object[] ppGetValues() { 40 | return new Object[] { name, age, address }; 41 | } 42 | 43 | } 44 | 45 | @Test 46 | public void PPObjectTest() { 47 | 48 | System.out.println("- Testing custom object pprint"); 49 | System.out.println(); 50 | 51 | Person sherlock = new Person("Sherlock Holmes", 37, "Baker Street 221B"); 52 | 53 | ObjectPrint.pprint(sherlock); 54 | 55 | System.out.println(); 56 | System.out.println(); 57 | 58 | ObjectPrint.pprint(sherlock, true); 59 | 60 | System.out.println(); 61 | System.out.println(); 62 | 63 | ObjectPrint.pprint(sherlock, true, true); 64 | 65 | System.out.println(); 66 | System.out.println(); 67 | 68 | } 69 | 70 | @Test 71 | public void ObjectArrayTest() { 72 | 73 | System.out.println("- Testing object array pprint"); 74 | System.out.println(); 75 | 76 | Object[] invited = new String[] { "Me", "You", "Everyone else" }; 77 | 78 | ObjectPrint.pprint(invited); 79 | 80 | System.out.println(); 81 | System.out.println(); 82 | 83 | ObjectPrint.pprint(invited, true); 84 | 85 | System.out.println(); 86 | System.out.println(); 87 | 88 | ObjectPrint.pprint(invited, true, true, Util.TableFormat.EQUALS); 89 | 90 | System.out.println(); 91 | System.out.println(); 92 | } 93 | 94 | private static enum DataStructType { 95 | 96 | INTEGER, STRING, DATASTRUCT; 97 | 98 | } 99 | 100 | private static class DataStruct implements PrettyPrintable { 101 | 102 | List values; 103 | List keys; 104 | List types; 105 | 106 | public DataStruct() { 107 | keys = new ArrayList<>(); 108 | values = new ArrayList<>(); 109 | types = new ArrayList<>(); 110 | } 111 | 112 | public void addInt(String key, int n) { 113 | keys.add(key); 114 | values.add(n); 115 | types.add(DataStructType.INTEGER); 116 | } 117 | 118 | public void addString(String key, String str) { 119 | keys.add(key); 120 | values.add(str); 121 | types.add(DataStructType.STRING); 122 | } 123 | 124 | public void addDataStruct(String key, DataStruct ds) { 125 | keys.add(key); 126 | values.add(ds); 127 | types.add(DataStructType.DATASTRUCT); 128 | } 129 | 130 | @SuppressWarnings("unused") 131 | public Integer getInt(String key) { 132 | 133 | int index = -1; 134 | 135 | for (int i = 0; i <= keys.size(); i++) { 136 | if (keys.get(i).equals(key)) 137 | index = i; 138 | } 139 | 140 | if (index == -1) 141 | return null; 142 | 143 | if (types.get(index) == DataStructType.INTEGER) 144 | return (Integer) values.get(index); 145 | else 146 | return null; 147 | } 148 | 149 | @SuppressWarnings("unused") 150 | public String getStr(String key) { 151 | 152 | int index = -1; 153 | 154 | for (int i = 0; i <= keys.size(); i++) { 155 | if (keys.get(i).equals(key)) 156 | index = i; 157 | } 158 | 159 | if (index == -1) 160 | return null; 161 | 162 | if (types.get(index) == DataStructType.STRING) 163 | return (String) values.get(index); 164 | else 165 | return null; 166 | } 167 | 168 | @SuppressWarnings("unused") 169 | public DataStruct getDataStruct(String key) { 170 | 171 | int index = -1; 172 | 173 | for (int i = 0; i <= keys.size(); i++) { 174 | if (keys.get(i).equals(key)) 175 | index = i; 176 | } 177 | 178 | if (index == -1) 179 | return null; 180 | 181 | if (types.get(index) == DataStructType.DATASTRUCT) 182 | return (DataStruct) values.get(index); 183 | else 184 | return null; 185 | } 186 | 187 | public boolean ppIsRecursive() { 188 | return true; 189 | } 190 | 191 | public boolean ppHasKeys() { 192 | return true; 193 | } 194 | 195 | public Object[] ppGetKeys() { 196 | 197 | Object[] k = new Object[keys.size()]; 198 | 199 | for (int i = 0; i < k.length; i++) 200 | k[i] = keys.get(i); 201 | 202 | return k; 203 | } 204 | 205 | public Object[] ppGetValues() { 206 | Object[] v = new Object[values.size()]; 207 | 208 | for (int i = 0; i < v.length; i++) 209 | v[i] = values.get(i); 210 | 211 | return v; 212 | } 213 | 214 | } 215 | 216 | @Test 217 | public void RecursiveObjectTest() { 218 | 219 | System.out.println("- Testing custom object recursive pprint"); 220 | System.out.println(); 221 | 222 | DataStruct people = new DataStruct(); 223 | people.addInt("Total", 5); 224 | people.addString("Janine", "Old friend"); 225 | 226 | DataStruct jobPeople = new DataStruct(); 227 | 228 | jobPeople.addString("Jon", "Boss"); 229 | jobPeople.addInt("Alan", 32); 230 | 231 | DataStruct jobFriends = new DataStruct(); 232 | 233 | jobFriends.addInt("Paul", 40); 234 | jobFriends.addInt("Jake", 31); 235 | 236 | jobPeople.addDataStruct("Friends", jobFriends); 237 | 238 | people.addDataStruct("Job", jobPeople); 239 | 240 | ObjectPrint.pprintRecursive(people); 241 | 242 | System.out.println(); 243 | System.out.println(); 244 | 245 | ObjectPrint.pprintRecursive(people, '='); 246 | 247 | System.out.println(); 248 | System.out.println(); 249 | } 250 | 251 | @Test 252 | public void errorTest() { 253 | 254 | System.out.println("- Testing pprint with null values"); 255 | System.out.println(); 256 | 257 | Object[] objArray = null; 258 | ObjectPrint.pprint(objArray); 259 | 260 | PrettyPrintable customObj = null; 261 | 262 | ObjectPrint.pprint(customObj); 263 | ObjectPrint.pprintRecursive(customObj); 264 | 265 | PrettyPrintable customObj2 = new PrettyPrintable() { 266 | 267 | public boolean ppIsRecursive() { 268 | return false; 269 | } 270 | 271 | public boolean ppHasKeys() { 272 | return true; 273 | } 274 | 275 | public Object[] ppGetKeys() { 276 | return null; 277 | } 278 | 279 | public Object[] ppGetValues() { 280 | return null; 281 | } 282 | 283 | }; 284 | 285 | ObjectPrint.pprint(customObj2); 286 | ObjectPrint.pprintRecursive(customObj2); 287 | 288 | PrettyPrintable customObj3 = new PrettyPrintable() { 289 | 290 | public boolean ppIsRecursive() { 291 | return false; 292 | } 293 | 294 | public boolean ppHasKeys() { 295 | return false; 296 | } 297 | 298 | public Object[] ppGetKeys() { 299 | return null; 300 | } 301 | 302 | public Object[] ppGetValues() { 303 | return new Object[] { null, null }; 304 | } 305 | 306 | }; 307 | 308 | ObjectPrint.pprint(customObj3); 309 | ObjectPrint.pprintRecursive(customObj3); 310 | 311 | Object[] arr = new Object[] { null, null }; 312 | 313 | ObjectPrint.pprint(arr); 314 | 315 | PrettyPrintable customObj4 = new PrettyPrintable() { 316 | 317 | public boolean ppIsRecursive() { 318 | return false; 319 | } 320 | 321 | public boolean ppHasKeys() { 322 | return true; 323 | } 324 | 325 | public Object[] ppGetKeys() { 326 | return new Object[] { null, null }; 327 | } 328 | 329 | public Object[] ppGetValues() { 330 | return new Object[] {}; 331 | } 332 | 333 | }; 334 | 335 | ObjectPrint.pprint(customObj4); 336 | ObjectPrint.pprintRecursive(customObj4); 337 | } 338 | 339 | } 340 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### The Java PrettyPrint library 2 | ![PPrint icon](https://woodenbell.github.io/static/images/pprint_icon.png) 3 | 4 | #### Features of pprint 5 | - Collection printing 6 | - Printing options, like enumeration (or key printing) and table-format 7 | - Custom pprinting for objects! 8 | - Simple pprinting for objects and collections (similar to Python's printing) 9 | - Recursive printing for objects! 10 | 11 | **Examples [here](#examples)** 12 | ___ 13 | 14 | ## Using pprint 15 | PrettyPrint, independent of the object type, is used in the following forms: 16 | 17 | // PPrintClass represents the class corresponding to the print type 18 | // Can be either ObjectPrint or CollectionPrint 19 | 20 | // Prints without enumeration (or keys) 21 | PPrintClass.pprint(someObject); 22 | 23 | // Prints with enumeration (or keys).. 24 | PPrintClass.pprint(someObject, true); 25 | 26 | /* 27 | * Prints with enumeration (or keys) and in a table-format, with default 28 | * table format characters ( '_' and '|' ) 29 | */ 30 | PPrintClass.pprint(someObject, true, true); 31 | 32 | /* 33 | * Prints with enumeration (or keys) and in a table-format, with the equals sign ('=') 34 | * as top and bottom border, and with '_' as division and left/right borders. 35 | */ 36 | PPrintClass.pprint(someObject, true, true, Util.TableFormat.EQUALS); 37 | 38 | //Note that SimplePrint just uses the following format 39 | SimplePrint.pprint(objectOrCOllection); 40 | ___ 41 | ## Simple printing _(since 1.1)_ 42 | Simple printing contains pprint methods that print the object in a simple format (very similar 43 | to _Python_'s string representation format). 44 | 45 | **Object arrays, lists and queues are printed in the following format**: 46 | [_elem1_, _elem2_, _elem3_, _..._] 47 | 48 | **Sets are printed in a similar format, but with curly braces:** 49 | {_elem1_, _elem2_, _elem3_, _..._} 50 | 51 | **Maps, however, are printed with each _key-value pair_ in a different line:** 52 | { 53 | _key1_: _value1_, 54 | _key2_: _value2_, 55 | _key3_: _value3_, 56 | ... 57 | } 58 | 59 | ## Object Printing 60 | To be pretty printed, an object must implement the `PrettyPrintable` interface. It's methods are 61 | described below: 62 | - **ppIsRecursive()** - Must return `true` if the object accepts recursive printing, otherwise `false`. 63 | - **ppHasKeys()** - Must return `true` if the object will use keys when `enumerated` is `true` on `pprint`, also if this methods returns `true` then `ppGetKeys()` cannot return `null`, otherwise the `pprint` method will return immediately. Returning `false` will tell the method to use the index before each value when `enumerated` is `true`. 64 | - **ppGetKeys()** - Must return an array of objects that are going to be used as keys, otherwise 65 | returns `null`. 66 | - **ppGetValues()** - Must return an array of objects containing the fields of the object that are going to be printed. 67 | 68 | **Having implemented all the methods, you can print the object just like shown [above](#using-prettyprint), in the following way:** 69 | 70 | import io.github.woodenbell.pprint.ObjectPrint 71 | 72 | ObjectPrint.pprint(myobj); //Default printing 73 | ObjectPrint.prettyRecursivePrint(myObj); //Recursive printing 74 | 75 | *PrettyPrint* also supports *Object* array printing, that works the same way as *Sets* described below, by using the *pprint* method of the **ObjectPrint** class. 76 | 77 | ### Recursive object print 78 | Recursive print is only supported for objects, that must implement `PrettyPrintable`. 79 | Recursive printing works just like normal printing, except when a field that also implements 80 | `PrettyPrintable` is going to be printed (a field returned by `ppGetValues()`). 81 | The recursive print then goes down one level in depth, jumping one line and padding 4 times the depth with the *padding character* 82 | 83 | **Example:** 84 | Value: 5 85 | Outer: 86 | ----Inner: "padding char -" 87 | Outer2: 88 | ----Inner2: 89 | --------Inner3: "depth=2, 2 * 4 = 8 padding chars" 90 | 91 | You can find an example of recursive *prettyprint* in action [here](#recursive-object-printing) 92 | ## Collection printing 93 | Collection printing is separed in two types of collections: 94 | - The ones that use keys: **Map** 95 | - The ones that don't: **Set, List and Queue** 96 | 97 | Printing a *Map* has the option `withKeys` instead of `enumerated`. The *Sets, Lists and Queues* 98 | will have their indexes printed. 99 | 100 | 101 | Collections use the **CollectionPrint** class, just like below: 102 | 103 | import io.github.woodenbell.pprint.CollectionPrint 104 | 105 | CollectionPrint.pprint(myMap, true); //Prints the map with it's keys and values 106 | CollectionPrint.pprint(myList, true); //Prints the list elements with their indexes 107 | 108 | **Collections unfortunately doesn't support recursive print, by the fact that collections, differently from objects that implement PrettyPrintable, have different ways to get keys and values. One example is Map and Set, because in map an Iterator is needed, while Set can be iterated by a for loop** 109 | 110 | ## TableFormat options 111 | **There are 3 options for TableFormat**: 112 | 113 | - **Util.TableFormat.UNDERSCORE (default TableFormat option)** 114 | top/bottom border: `_` 115 | right/left border: `|` 116 | 117 | - **Util.TableFormat.EQUALS** 118 | top/bottom border: `=` 119 | right/left border: `|` 120 | 121 | - **Util.TableFormat.HYPHEN** 122 | top/bottom border: `-` 123 | right/left border: `|` 124 | 125 | ___ 126 | 127 | 128 | ## Examples 129 | 130 | **The following examples are taken from the tests** 131 | 132 | ### Object printing 133 | 134 | import io.github.woodenbell.pprint.ObjectPrint; 135 | import io.github.woodenbell.pprint.PrettyPrintable; 136 | 137 | 138 | private static class Person implements PrettyPrintable { 139 | String name; 140 | int age; 141 | String address; 142 | 143 | Person(String name, int age, String address) { 144 | this.name = name; 145 | this.age = age; 146 | this.address = address; 147 | } 148 | 149 | public boolean ppIsRecursive() { 150 | return false; 151 | } 152 | 153 | public boolean ppHasKeys() { 154 | return true; 155 | } 156 | 157 | public Object[] ppGetKeys() { 158 | return new String[] { "Name", "Age", "Address" }; 159 | } 160 | 161 | public Object[] ppGetValues() { 162 | return new Object[] { name, age, address }; 163 | } 164 | 165 | } 166 | 167 | Person sherlock = new Person("Sherlock Holmes", 37, "Baker Street 221B"); 168 | 169 | ObjectPrint.pprint(sherlock); 170 | 171 | /* 172 | * Output: 173 | * 174 | * Sherlock Holmes 175 | * 37 176 | * Baker Street 221B 177 | */ 178 | 179 | ObjectPrint.pprint(sherlock, true); 180 | 181 | /* 182 | * Output: 183 | * 184 | * Name: Sherlock Holmes 185 | * Age: 37 186 | * Address: Baker Street 221B 187 | */ 188 | 189 | ObjectPrint.pprint(sherlock, true, true, Util.TableFormat.UNDERSCORE); 190 | 191 | /* 192 | * Output: 193 | * ____________________________ 194 | * |Name |Sherlock Holmes | 195 | * |Age |37 | 196 | * |Address |Baker Street 221B| 197 | * ____________________________ 198 | */ 199 | 200 | 201 | ### Object array printing 202 | 203 | import io.github.woodenbell.pprint.ObjectPrint; 204 | 205 | Object[] people = new String[] {"Me", "You", "Everyone else"}; 206 | 207 | ObjectPrint.pprint(invited); 208 | 209 | /* 210 | * Me 211 | * You 212 | * Everyone else 213 | */ 214 | 215 | ObjectPrint.pprint(invited, true); 216 | 217 | /* 218 | * 0: Me 219 | * 1: You 220 | * 2: Everyone else 221 | */ 222 | 223 | ObjectPrint.pprint(invited, true, true, Util.TableFormat.EQUALS); 224 | 225 | /* 226 | * ================== 227 | * |1 |Me | 228 | * |2 |You | 229 | * |3 |Everyone else| 230 | * ================== 231 | */ 232 | 233 | ### Recursive object printing 234 | 235 | import io.github.woodenbell.pprint.ObjectPrint; 236 | import io.github.woodenbell.pprint.PrettyPrintable; 237 | import java.util.ArrayList; 238 | 239 | // Simulates a JSON-like structure 240 | 241 | private static class DataStruct implements PrettyPrintable { 242 | 243 | List values; 244 | List keys; 245 | List types; 246 | 247 | public DataStruct() { 248 | keys = new ArrayList<>(); 249 | values = new ArrayList<>(); 250 | types = new ArrayList<>(); 251 | } 252 | 253 | public void addInt(String key, int n) { 254 | keys.add(key); 255 | values.add(n); 256 | types.add(DataStructType.INTEGER); 257 | } 258 | 259 | public void addString(String key, String str) { 260 | keys.add(key); 261 | values.add(str); 262 | types.add(DataStructType.STRING); 263 | } 264 | 265 | public void addDataStruct(String key, DataStruct ds) { 266 | keys.add(key); 267 | values.add(ds); 268 | types.add(DataStructType.DATASTRUCT); 269 | } 270 | 271 | @SuppressWarnings("unused") 272 | public Integer getInt(String key) { 273 | 274 | int index = -1; 275 | 276 | for (int i = 0; i <= keys.size(); i++) { 277 | if (keys.get(i).equals(key)) 278 | index = i; 279 | } 280 | 281 | if (index == -1) 282 | return null; 283 | 284 | if (types.get(index) == DataStructType.INTEGER) 285 | return (Integer) values.get(index); 286 | else 287 | return null; 288 | } 289 | 290 | @SuppressWarnings("unused") 291 | public String getStr(String key) { 292 | 293 | int index = -1; 294 | 295 | for (int i = 0; i <= keys.size(); i++) { 296 | if (keys.get(i).equals(key)) 297 | index = i; 298 | } 299 | 300 | if (index == -1) 301 | return null; 302 | 303 | if (types.get(index) == DataStructType.STRING) 304 | return (String) values.get(index); 305 | else 306 | return null; 307 | } 308 | 309 | @SuppressWarnings("unused") 310 | public DataStruct getDataStruct(String key) { 311 | 312 | int index = -1; 313 | 314 | for (int i = 0; i <= keys.size(); i++) { 315 | if (keys.get(i).equals(key)) 316 | index = i; 317 | } 318 | 319 | if (index == -1) 320 | return null; 321 | 322 | if (types.get(index) == DataStructType.DATASTRUCT) 323 | return (DataStruct) values.get(index); 324 | else 325 | return null; 326 | } 327 | 328 | public boolean ppIsRecursive() { 329 | return true; 330 | } 331 | 332 | public boolean ppHasKeys() { 333 | return true; 334 | } 335 | 336 | public Object[] ppGetKeys() { 337 | 338 | Object[] k = new Object[keys.size()]; 339 | 340 | for (int i = 0; i < k.length; i++) 341 | k[i] = keys.get(i); 342 | 343 | return k; 344 | } 345 | 346 | public Object[] ppGetValues() { 347 | Object[] v = new Object[values.size()]; 348 | 349 | for (int i = 0; i < v.length; i++) 350 | v[i] = values.get(i); 351 | 352 | return v; 353 | } 354 | 355 | } 356 | 357 | /* 358 | * Creates a people structure in the following form (JSON representation): 359 | * { 360 | * "Total": 5, 361 | * "Janine": "Old friend", 362 | * "Job": 363 | * { 364 | * "Jon": "Boss", 365 | * "Alan": 32, 366 | * "Friends": 367 | * { 368 | * "Paul": 40, 369 | * "Jake": 31 370 | * } 371 | * } 372 | * } 373 | 374 | DataStruct people = new DataStruct(); 375 | people.addInt("Total", 5); 376 | people.addString("Janine", "Old friend"); 377 | 378 | DataStruct jobPeople = new DataStruct(); 379 | 380 | jobPeople.addString("Jon", "Boss"); 381 | jobPeople.addInt("Alan", 32); 382 | 383 | DataStruct jobFriends = new DataStruct(); 384 | 385 | jobFriends.addInt("Paul", 40); 386 | jobFriends.addInt("Jake", 31); 387 | 388 | jobPeople.addDataStruct("Friends", jobFriends); 389 | 390 | people.addDataStruct("Job", jobPeople); 391 | 392 | ObjectPrint.pprintRecursive(people); 393 | 394 | /* Output (default padding character is '-') 395 | * 396 | * Total: 5 397 | * Janine: Old friend 398 | * Job: 399 | * ----Jon: Boss 400 | * ----Alan: 32 401 | * ----Friends: 402 | * --------Paul: 40 403 | * --------Jake: 31 404 | * 405 | */ 406 | 407 | -------------------------------------------------------------------------------- /src/main/java/io/github/woodenbell/pprint/ObjectPrint.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint; 2 | 3 | /* 4 | * Copyright 2017 Gabriel C. Barbosa 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** 20 | * Class with methods to print objects (not collections) and custom 21 | * pprint objects. 22 | * 23 | * @author Gabriel C. 24 | */ 25 | 26 | public class ObjectPrint { 27 | 28 | /** 29 | * Overloaded pprint for object with default options. 30 | * 31 | * @param p 32 | * The object that implements PrettyPrintable. 33 | */ 34 | public static void pprint(PrettyPrintable p) { 35 | pprint(p, false, false, Util.TableFormat.UNDERSCORE); 36 | } 37 | 38 | /** 39 | * Overloaded pprint for object with enumeration/key option. 40 | * 41 | * @param p 42 | * The object that implements PrettyPrintable. 43 | * @param enumerated 44 | * If the index (or keys) should be included before each element. 45 | */ 46 | 47 | public static void pprint(PrettyPrintable p, boolean enumerated) { 48 | pprint(p, false, enumerated, Util.TableFormat.UNDERSCORE); 49 | } 50 | 51 | /** 52 | * Overloaded pprint for object with option to default table format. 53 | * 54 | * @param p 55 | * The object that implements PrettyPrintable. 56 | * @param enumerated 57 | * If the index (or keys) should be included before each element. 58 | * @param tableLike 59 | * If the object should be printed in a table format. 60 | * @see io.github.woodenbell.pprint.Util.TableFormat#UNDERSCORE 61 | */ 62 | 63 | public static void pprint(PrettyPrintable p, boolean tableLike, boolean enumerated) { 64 | pprint(p, tableLike, enumerated, Util.TableFormat.UNDERSCORE); 65 | } 66 | 67 | /** 68 | * PrettyPrint method for object. 69 | * 70 | * @param p 71 | * The object that implements PrettyPrintable. 72 | * @param enumerated 73 | * If the index (or keys) should be included before each element. 74 | * @param tableLike 75 | * If the object should be printed in a table format. 76 | * @param tableFormat 77 | * The table format. 78 | * @see io.github.woodenbell.pprint.Util.TableFormat 79 | * @see io.github.woodenbell.pprint.PrettyPrintable 80 | */ 81 | 82 | public static void pprint(PrettyPrintable p, boolean tableLike, boolean enumerated, 83 | Util.TableFormat tableFormat) { 84 | 85 | if (p == null) 86 | return; 87 | 88 | if (p.ppGetValues() == null && (p.ppHasKeys() && p.ppGetKeys() == null)) 89 | return; 90 | 91 | int maxCharLenKeys = 0; 92 | int maxCharLenVals = 0; 93 | 94 | for (Object obj : p.ppGetValues()) { 95 | 96 | if (obj == null) { 97 | if (maxCharLenVals < 4) 98 | maxCharLenVals = 4; 99 | continue; 100 | } 101 | 102 | if (obj.toString().length() > maxCharLenVals) 103 | maxCharLenVals = obj.toString().length(); 104 | } 105 | 106 | if (enumerated && p.ppHasKeys()) { 107 | for (Object obj : p.ppGetKeys()) { 108 | 109 | if (obj == null) { 110 | if (maxCharLenKeys < 4) 111 | maxCharLenKeys = 4; 112 | continue; 113 | } 114 | 115 | if (obj.toString().length() > maxCharLenKeys) 116 | maxCharLenKeys = obj.toString().length(); 117 | 118 | } 119 | } 120 | 121 | if (tableLike) { 122 | 123 | if (enumerated) { 124 | 125 | if (p.ppHasKeys()) { 126 | 127 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenKeys + maxCharLenVals + 4)); 128 | 129 | int i = 0; 130 | for (Object o : p.ppGetValues()) { 131 | System.out.printf("%1s%-" + (maxCharLenKeys + 1) + "s%1s", tableFormat.division, 132 | p.ppGetKeys()[i], tableFormat.division); 133 | System.out.printf("%-" + maxCharLenVals + "s%1s\n", (o == null ? "null" : o.toString()), 134 | tableFormat.division); 135 | i++; 136 | } 137 | 138 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenKeys + maxCharLenVals + 4)); 139 | 140 | } else { 141 | 142 | int i = 0; 143 | 144 | System.out.println(Util.makePadding(tableFormat.border, 145 | maxCharLenVals + 4 + (p.ppGetValues().length + "").length())); 146 | 147 | for (Object o : p.ppGetValues()) { 148 | 149 | System.out.printf("%1s%-" + ((p.ppGetValues().length + "").length() + 1) + "s%1s", 150 | tableFormat.division, i, tableFormat.division); 151 | System.out.printf("%-" + maxCharLenVals + "s%1s\n", (o == null ? "null" : o.toString()), 152 | tableFormat.division); 153 | 154 | i++; 155 | } 156 | 157 | System.out.println(Util.makePadding(tableFormat.border, 158 | maxCharLenVals + 4 + (p.ppGetValues().length + "").length())); 159 | } 160 | 161 | } else { 162 | 163 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenVals + 3)); 164 | 165 | for (Object o : p.ppGetValues()) { 166 | System.out.printf("%1s%-" + maxCharLenVals + "s%1s\n", tableFormat.division, 167 | (o == null ? "null" : o.toString()), tableFormat.division); 168 | } 169 | 170 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenVals + 3)); 171 | 172 | } 173 | } else { 174 | 175 | if (enumerated) { 176 | 177 | if (p.ppHasKeys()) { 178 | int i = 0; 179 | for (Object o : p.ppGetValues()) { 180 | System.out.printf("%-" + (maxCharLenKeys + 2) + "s", 181 | (p.ppGetKeys()[i] == null ? "null" : p.ppGetKeys()[i].toString()) + ": "); 182 | System.out.printf("%-" + maxCharLenVals + "s\n", (o == null ? "null" : o.toString())); 183 | i++; 184 | } 185 | 186 | } else { 187 | int i = 0; 188 | for (Object o : p.ppGetValues()) { 189 | System.out.printf("%-" + ((p.ppGetValues().length + "").length() + 1 + 2) + "s", i + ": "); 190 | System.out.printf("%-" + maxCharLenVals + "s\n", (o == null ? "null" : o.toString())); 191 | i++; 192 | } 193 | } 194 | 195 | } else { 196 | for (Object o : p.ppGetValues()) { 197 | System.out.printf("%-" + maxCharLenVals + "s\n", (o == null ? "null" : o.toString())); 198 | } 199 | } 200 | } 201 | } 202 | 203 | /** 204 | * 205 | * 206 | * /** Overloaded pprintRecursive to PrettyPrintable objects with default 207 | * padding character. 208 | * 209 | * @param p 210 | * The PrettyPrintable object. 211 | */ 212 | 213 | public static void pprintRecursive(PrettyPrintable p) { 214 | pprintRecursive(p, '-', 0); 215 | } 216 | 217 | /** 218 | * Prints the PretttyPrintable object applying pprint to PrettyPrintable 219 | * fields. 220 | * 221 | * @param p 222 | * The PrettyPrintable object. 223 | * 224 | * @param paddingChar 225 | * The character used in padding to indicate lower levels in the 226 | * printing structure. 227 | * 228 | * @see io.github.woodenbell.pprint.PrettyPrintable 229 | */ 230 | 231 | public static void pprintRecursive(PrettyPrintable p, char paddingChar) { 232 | pprintRecursive(p, paddingChar, 0); 233 | } 234 | 235 | /** 236 | * Invisible overload of pprintRecursion used in recursion to print the 237 | * padding based in depth. 238 | * 239 | * @param p 240 | * The PrettyPrintable object. 241 | * @param paddingChar 242 | * The character used in padding to indicate lower levels in the 243 | * printing structure. 244 | * @param depth 245 | * The recursion depth, used for padding. 246 | */ 247 | 248 | private static void pprintRecursive(PrettyPrintable p, char paddingChar, int depth) { 249 | 250 | if (p == null || !p.ppIsRecursive() || (p.ppHasKeys() && p.ppGetKeys() == null) || p.ppGetValues() == null) 251 | return; 252 | 253 | int maxCharLenKeys = 0; 254 | int maxCharLenVals = 0; 255 | 256 | for (Object obj : p.ppGetValues()) { 257 | 258 | if (obj == null) { 259 | if (maxCharLenVals < 4) 260 | maxCharLenVals = 4; 261 | continue; 262 | } 263 | 264 | if (obj.toString().length() > maxCharLenVals) 265 | maxCharLenVals = obj.toString().length(); 266 | } 267 | 268 | if (p.ppHasKeys()) { 269 | for (Object obj : p.ppGetKeys()) { 270 | 271 | if (obj == null) { 272 | if (maxCharLenKeys < 4) 273 | maxCharLenKeys = 4; 274 | continue; 275 | } 276 | 277 | if (obj.toString().length() > maxCharLenKeys) 278 | maxCharLenKeys = obj.toString().length(); 279 | 280 | } 281 | } 282 | 283 | if (p.ppHasKeys()) { 284 | for (Object obj : p.ppGetKeys()) { 285 | if (obj.toString().length() > maxCharLenKeys) 286 | maxCharLenKeys = obj.toString().length(); 287 | } 288 | } 289 | 290 | if (p.ppHasKeys()) { 291 | 292 | int i = 0; 293 | 294 | for (Object o : p.ppGetValues()) { 295 | 296 | System.out.print(Util.makePadding(paddingChar, depth * 4)); 297 | 298 | System.out.printf("%-" + (maxCharLenKeys + 2) + "s", 299 | (p.ppGetKeys()[i] == null ? "null" : p.ppGetKeys()[i].toString()) + ": "); 300 | 301 | if (o instanceof PrettyPrintable) { 302 | 303 | System.out.println(); 304 | pprintRecursive((PrettyPrintable) o, paddingChar, depth + 1); 305 | 306 | } else 307 | System.out.println((o == null ? "null" : o.toString())); 308 | 309 | i++; 310 | } 311 | 312 | } else { 313 | 314 | int i = 0; 315 | 316 | for (Object o : p.ppGetValues()) { 317 | 318 | System.out.print(Util.makePadding(paddingChar, depth * 4)); 319 | 320 | System.out.printf("%-" + ((p.ppGetValues().length + "").length() + 1 + 2) + "s", i + ": "); 321 | 322 | if (o instanceof PrettyPrintable) { 323 | 324 | System.out.println(); 325 | pprintRecursive((PrettyPrintable) o, paddingChar, depth + 1); 326 | 327 | } else 328 | System.out.println((o == null ? "null" : o.toString())); 329 | 330 | i++; 331 | } 332 | } 333 | } 334 | 335 | /** 336 | * Overloaded pprint for Object array with default options. 337 | * 338 | * @param p 339 | * The object array. 340 | */ 341 | 342 | public static void pprint(Object[] p) { 343 | pprint(p, false, false, Util.TableFormat.UNDERSCORE); 344 | } 345 | 346 | /** 347 | * Overloaded pprint for Object array with enumeration option. 348 | * 349 | * @param p 350 | * The Object array. 351 | * @param enumerated 352 | * If the index of each element should be printed. 353 | */ 354 | 355 | public static void pprint(Object[] p, boolean enumerated) { 356 | pprint(p, false, enumerated, Util.TableFormat.UNDERSCORE); 357 | } 358 | 359 | /** 360 | * Overloaded pprint for Object array with option to default table format. 361 | * 362 | * @param p 363 | * The Object array. 364 | * @param tableLike 365 | * If the collection should be printed in a table format. 366 | * @param enumerated 367 | * If the index of each element should be printed. 368 | * @see io.github.woodenbell.pprint.Util.TableFormat#UNDERSCORE 369 | */ 370 | 371 | public static void pprint(Object[] p, boolean tableLike, boolean enumerated) { 372 | pprint(p, tableLike, enumerated, Util.TableFormat.UNDERSCORE); 373 | } 374 | 375 | /** 376 | * Prints a Object array with options to show it's elements. 377 | * 378 | * @param p 379 | * The Object array. 380 | * @param tableLike 381 | * If the collection should be printed in a table format. 382 | * @param enumerated 383 | * If the index of each element should be printed. 384 | * @param tableFormat 385 | * The table format. 386 | * @see io.github.woodenbell.pprint.Util.TableFormat 387 | */ 388 | 389 | public static void pprint(Object[] p, boolean tableLike, boolean enumerated, Util.TableFormat tableFormat) { 390 | 391 | if (p == null) 392 | return; 393 | 394 | for (Object ob : p) { 395 | if (ob == null) 396 | return; 397 | } 398 | 399 | int maxCharLen = 0; 400 | 401 | for (Object obj : p) { 402 | if (obj.toString().length() > maxCharLen) 403 | maxCharLen = obj.toString().length(); 404 | } 405 | 406 | if (tableLike) { 407 | if (enumerated) { 408 | 409 | int i = 0; 410 | 411 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.length + "").length())); 412 | 413 | for (Object o : p) { 414 | 415 | System.out.printf("%1s%-" + ((p.length + "").length() + 1) + "s%1s", tableFormat.division, i, 416 | tableFormat.division); 417 | System.out.printf("%-" + maxCharLen + "s%1s\n", (o == null ? "null" : o.toString()), 418 | tableFormat.division); 419 | 420 | i++; 421 | } 422 | 423 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.length + "").length())); 424 | 425 | } else { 426 | 427 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 428 | 429 | for (Object o : p) { 430 | System.out.printf("%1s%-" + maxCharLen + "s%1s\n", tableFormat.division, 431 | (o == null ? "null" : o.toString()), tableFormat.division); 432 | } 433 | 434 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 435 | 436 | } 437 | } else { 438 | if (enumerated) { 439 | int i = 0; 440 | for (Object o : p) { 441 | System.out.printf("%-" + ((p.length + "").length() + 2) + "s", i + ": "); 442 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 443 | i++; 444 | } 445 | } else { 446 | for (Object o : p) { 447 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 448 | } 449 | } 450 | } 451 | } 452 | } 453 | -------------------------------------------------------------------------------- /src/main/java/io/github/woodenbell/pprint/CollectionPrint.java: -------------------------------------------------------------------------------- 1 | package io.github.woodenbell.pprint; 2 | 3 | import java.util.Iterator; 4 | import java.util.List; 5 | import java.util.Map; 6 | import java.util.Queue; 7 | import java.util.Set; 8 | 9 | /** 10 | * PrettyPrint class for collections. 11 | * 12 | * @author Gabriel C. 13 | * 14 | */ 15 | 16 | public class CollectionPrint { 17 | 18 | /** 19 | * Overloaded pprint for List collection with default options. 20 | * 21 | * @param p 22 | * The List object. 23 | */ 24 | 25 | public static void pprint(List p) { 26 | pprint(p, false, false, Util.TableFormat.UNDERSCORE); 27 | } 28 | 29 | /** 30 | * Overloaded pprint for List collection with enumeration option. 31 | * 32 | * @param p 33 | * The List object. 34 | * @param enumerated 35 | * If the index of each element should be printed. 36 | */ 37 | 38 | public static void pprint(List p, boolean enumerated) { 39 | pprint(p, false, enumerated, Util.TableFormat.UNDERSCORE); 40 | } 41 | 42 | /** 43 | * Overloaded pprint for List collection with option to default table 44 | * format. 45 | * 46 | * @param p 47 | * The List object. 48 | * @param tableLike 49 | * If the collection should be printed in a table format. 50 | * @param enumerated 51 | * If the index of each element should be printed. 52 | * @see io.github.woodenbell.pprint.Util.TableFormat#UNDERSCORE 53 | */ 54 | 55 | public static void pprint(List p, boolean tableLike, boolean enumerated) { 56 | pprint(p, tableLike, enumerated, Util.TableFormat.UNDERSCORE); 57 | } 58 | 59 | /** 60 | * Prints a List collection with options to show it's elements. 61 | * 62 | * @param p 63 | * The List object. 64 | * @param tableLike 65 | * If the collection should be printed in a table format. 66 | * @param enumerated 67 | * If the index of each element should be printed. 68 | * @param tableFormat 69 | * The table format. 70 | * @see io.github.woodenbell.pprint.Util.TableFormat 71 | * @see java.util.List 72 | */ 73 | 74 | public static void pprint(List p, boolean tableLike, boolean enumerated, Util.TableFormat tableFormat) { 75 | 76 | if (p == null) 77 | return; 78 | 79 | int maxCharLen = 0; 80 | 81 | for (Object obj : p) { 82 | 83 | if (obj == null) { 84 | if (maxCharLen < 4) 85 | maxCharLen = 4; 86 | continue; 87 | } 88 | 89 | if (obj.toString().length() > maxCharLen) 90 | maxCharLen = obj.toString().length(); 91 | } 92 | 93 | if (tableLike) { 94 | if (enumerated) { 95 | 96 | int i = 0; 97 | 98 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.size() + "").length())); 99 | 100 | for (Object o : p) { 101 | 102 | System.out.printf("%1s%-" + ((p.size() + "").length() + 1) + "s%1s", tableFormat.division, i, 103 | tableFormat.division); 104 | System.out.printf("%-" + maxCharLen + "s%1s\n", (o == null ? "null" : o.toString()), 105 | tableFormat.division); 106 | 107 | i++; 108 | } 109 | 110 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.size() + "").length())); 111 | 112 | } else { 113 | 114 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 115 | 116 | for (Object o : p) { 117 | System.out.printf("%1s%-" + maxCharLen + "s%1s\n", tableFormat.division, 118 | (o == null ? "null" : o.toString()), tableFormat.division); 119 | } 120 | 121 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 122 | 123 | } 124 | } else { 125 | if (enumerated) { 126 | int i = 0; 127 | for (Object o : p) { 128 | System.out.printf("%-" + ((p.size() + "").length() + 2) + "s", i + ": "); 129 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 130 | i++; 131 | } 132 | } else { 133 | for (Object o : p) { 134 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 135 | } 136 | } 137 | } 138 | } 139 | 140 | /** 141 | * Overloaded pprint for Map collection with default options. 142 | * 143 | * @param p 144 | * The Map object. 145 | */ 146 | 147 | public static void pprint(Map p) { 148 | pprint(p, false, false, Util.TableFormat.UNDERSCORE); 149 | } 150 | 151 | /** 152 | * Overloaded pprint for Map collection with key printing option. 153 | * 154 | * @param p 155 | * The Map object. 156 | * @param withKeys 157 | * If the key corresponding each element should be printed. 158 | */ 159 | 160 | public static void pprint(Map p, boolean withKeys) { 161 | pprint(p, false, withKeys, Util.TableFormat.UNDERSCORE); 162 | } 163 | 164 | /** 165 | * Overloaded pprint for Map collection with option to default table 166 | * format. 167 | * 168 | * @param p 169 | * The Map object. 170 | * @param tableLike 171 | * If the collection should be printed in a table format. 172 | * @param withKeys 173 | * If the key corresponding each element should be printed. 174 | * @see io.github.woodenbell.pprint.Util.TableFormat#UNDERSCORE 175 | */ 176 | 177 | public static void pprint(Map p, boolean tableLike, boolean withKeys) { 178 | pprint(p, tableLike, withKeys, Util.TableFormat.UNDERSCORE); 179 | } 180 | 181 | /** 182 | * Prints a Map collection with options to show it's elements. 183 | * 184 | * @param p 185 | * The Map object. 186 | * @param tableLike 187 | * If the collection should be printed in a table format. 188 | * @param withKeys 189 | * If the key corresponding each element should be printed. 190 | * @param tableFormat 191 | * The table format. 192 | * @see io.github.woodenbell.pprint.Util.TableFormat 193 | * @see java.util.Map 194 | */ 195 | 196 | public static void pprint(Map p, boolean tableLike, boolean withKeys, Util.TableFormat tableFormat) { 197 | 198 | if (p == null) 199 | return; 200 | 201 | int maxCharLenKeys = 0; 202 | int maxCharLenVals = 0; 203 | 204 | Iterator iterKeys = p.keySet().iterator(); 205 | Iterator iterVals = p.values().iterator(); 206 | Object curr; 207 | 208 | while (withKeys && iterKeys.hasNext()) { 209 | 210 | curr = iterKeys.next(); 211 | 212 | if (curr == null) { 213 | if (maxCharLenKeys < 4) 214 | maxCharLenKeys = 4; 215 | continue; 216 | } 217 | 218 | if (curr.toString().length() > maxCharLenKeys) 219 | maxCharLenKeys = curr.toString().length(); 220 | } 221 | 222 | while (iterVals.hasNext()) { 223 | 224 | curr = iterVals.next(); 225 | 226 | if (curr == null) { 227 | if (maxCharLenVals < 4) 228 | maxCharLenVals = 4; 229 | continue; 230 | } 231 | 232 | if (curr.toString().length() > maxCharLenVals) 233 | maxCharLenVals = curr.toString().length(); 234 | } 235 | 236 | iterKeys = p.keySet().iterator(); 237 | 238 | if (tableLike) { 239 | 240 | if (withKeys) { 241 | 242 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenVals + maxCharLenKeys + 4)); 243 | 244 | while (iterKeys.hasNext()) { 245 | 246 | curr = iterKeys.next(); 247 | 248 | System.out.printf("%1s%-" + maxCharLenKeys + "s%1s", tableFormat.division, 249 | (curr == null ? "null" : curr.toString()), tableFormat.division); 250 | 251 | System.out.printf("%-" + maxCharLenVals + "s%1s\n", 252 | (p.get(curr) == null ? null : p.get(curr)).toString(), tableFormat.division); 253 | 254 | } 255 | 256 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenVals + maxCharLenKeys + 4)); 257 | 258 | } else { 259 | 260 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenKeys + 2)); 261 | 262 | while (iterKeys.hasNext()) { 263 | 264 | curr = iterKeys.next(); 265 | 266 | System.out.printf("%1s%-" + maxCharLenKeys + "s%1s\n", tableFormat.division, 267 | (p.get(curr) == null ? null : p.get(curr).toString()), tableFormat.division); 268 | } 269 | 270 | System.out.println(Util.makePadding(tableFormat.border, maxCharLenVals + 2)); 271 | 272 | } 273 | } else { 274 | 275 | if (withKeys) { 276 | 277 | while (iterKeys.hasNext()) { 278 | 279 | curr = iterKeys.next(); 280 | 281 | System.out.printf("%-" + (maxCharLenKeys + 2) + "s", 282 | (curr == null ? "null" : curr.toString()) + ": "); 283 | System.out.printf("%-" + maxCharLenVals + "s\n", 284 | (p.get(curr) == null ? null : p.get(curr).toString())); 285 | } 286 | 287 | } else { 288 | 289 | while (iterKeys.hasNext()) { 290 | 291 | curr = iterKeys.next(); 292 | System.out.printf("%-" + maxCharLenVals + "s\n", 293 | (p.get(curr) == null ? null : p.get(curr)).toString()); 294 | } 295 | } 296 | } 297 | } 298 | 299 | /** 300 | * Overloaded pprint for Set collection with default options. 301 | * 302 | * @param p 303 | * The Set object. 304 | */ 305 | 306 | public static void pprint(Set p) { 307 | pprint(p, false, false, Util.TableFormat.UNDERSCORE); 308 | } 309 | 310 | /** 311 | * Overloaded pprint for Set collection with enumeration option. 312 | * 313 | * @param p 314 | * The Set object. 315 | * @param enumerated 316 | * If the index of each element should be printed. 317 | */ 318 | 319 | public static void pprint(Set p, boolean enumerated) { 320 | pprint(p, false, enumerated, Util.TableFormat.UNDERSCORE); 321 | } 322 | 323 | /** 324 | * Overloaded pprint for Set collection with option to default table 325 | * format. 326 | * 327 | * @param p 328 | * The Set object. 329 | * @param tableLike 330 | * If the collection should be printed in a table format. 331 | * @param enumerated 332 | * If the index of each element should be printed. 333 | * @see io.github.woodenbell.pprint.Util.TableFormat#UNDERSCORE 334 | */ 335 | 336 | public static void pprint(Set p, boolean tableLike, boolean enumerated) { 337 | pprint(p, tableLike, enumerated, Util.TableFormat.UNDERSCORE); 338 | } 339 | 340 | /** 341 | * Prints a Set collection with options to show it's elements. 342 | * 343 | * @param p 344 | * The Set object. 345 | * @param tableLike 346 | * If the collection should be printed in a table format. 347 | * @param enumerated 348 | * If the index of each element should be printed. 349 | * @param tableFormat 350 | * The table format. 351 | * @see io.github.woodenbell.pprint.Util.TableFormat 352 | * @see java.util.Set 353 | */ 354 | 355 | public static void pprint(Set p, boolean tableLike, boolean enumerated, Util.TableFormat tableFormat) { 356 | 357 | if (p == null) 358 | return; 359 | 360 | int maxCharLen = 0; 361 | 362 | for (Object obj : p) { 363 | 364 | if (obj == null) { 365 | if (maxCharLen < 4) 366 | maxCharLen = 4; 367 | continue; 368 | } 369 | 370 | if (obj.toString().length() > maxCharLen) 371 | maxCharLen = obj.toString().length(); 372 | } 373 | 374 | if (tableLike) { 375 | if (enumerated) { 376 | 377 | int i = 0; 378 | 379 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.size() + "").length())); 380 | 381 | for (Object o : p) { 382 | 383 | System.out.printf("%1s%-" + ((p.size() + "").length() + 1) + "s%1s", tableFormat.division, i, 384 | tableFormat.division); 385 | System.out.printf("%-" + maxCharLen + "s%1s\n", (o == null ? "null" : o.toString()), 386 | tableFormat.division); 387 | 388 | i++; 389 | } 390 | 391 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.size() + "").length())); 392 | 393 | } else { 394 | 395 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 396 | 397 | for (Object o : p) { 398 | System.out.printf("%1s%-" + maxCharLen + "s%1s\n", tableFormat.division, 399 | (o == null ? "null" : o.toString()), tableFormat.division); 400 | } 401 | 402 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 403 | 404 | } 405 | } else { 406 | if (enumerated) { 407 | int i = 0; 408 | for (Object o : p) { 409 | System.out.printf("%-" + ((p.size() + "").length() + 2) + "s", i + ": "); 410 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 411 | i++; 412 | } 413 | } else { 414 | for (Object o : p) { 415 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 416 | } 417 | } 418 | } 419 | } 420 | 421 | /** 422 | * Overloaded pprint for Queue collection with default options. 423 | * 424 | * @param p 425 | * The Queue object. 426 | */ 427 | 428 | public static void pprint(Queue p) { 429 | pprint(p, false, false, Util.TableFormat.UNDERSCORE); 430 | } 431 | 432 | /** 433 | * Overloaded pprint for Queue collection with enumeration option. 434 | * 435 | * @param p 436 | * The Queue object. 437 | * @param enumerated 438 | * If the index of each element should be printed. 439 | */ 440 | 441 | public static void pprint(Queue p, boolean enumerated) { 442 | pprint(p, false, enumerated, Util.TableFormat.UNDERSCORE); 443 | } 444 | 445 | /** 446 | * Overloaded pprint for Queue collection with option to default table 447 | * format. 448 | * 449 | * @param p 450 | * The Queue object. 451 | * @param tableLike 452 | * If the collection should be printed in a table format. 453 | * @param enumerated 454 | * If the index of each element should be printed. 455 | * @see io.github.woodenbell.pprint.Util.TableFormat#UNDERSCORE 456 | */ 457 | 458 | public static void pprint(Queue p, boolean tableLike, boolean enumerated) { 459 | pprint(p, tableLike, enumerated, Util.TableFormat.UNDERSCORE); 460 | } 461 | 462 | /** 463 | * Prints a Queue collection with options to show it's elements. 464 | * 465 | * @param p 466 | * The Queue object. 467 | * @param tableLike 468 | * If the collection should be printed in a table format. 469 | * @param enumerated 470 | * If the index of each element should be printed. 471 | * @param tableFormat 472 | * The table format. 473 | * @see io.github.woodenbell.pprint.Util.TableFormat 474 | * @see java.util.Queue 475 | */ 476 | 477 | public static void pprint(Queue p, boolean tableLike, boolean enumerated, Util.TableFormat tableFormat) { 478 | 479 | if (p == null) 480 | return; 481 | 482 | int maxCharLen = 0; 483 | 484 | for (Object obj : p) { 485 | 486 | if (obj == null) { 487 | if (maxCharLen < 4) 488 | maxCharLen = 4; 489 | continue; 490 | } 491 | 492 | if (obj.toString().length() > maxCharLen) 493 | maxCharLen = obj.toString().length(); 494 | } 495 | 496 | if (tableLike) { 497 | if (enumerated) { 498 | 499 | int i = 0; 500 | 501 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.size() + "").length())); 502 | 503 | for (Object o : p) { 504 | 505 | System.out.printf("%1s%-" + ((p.size() + "").length() + 1) + "s%1s", tableFormat.division, i, 506 | tableFormat.division); 507 | System.out.printf("%-" + maxCharLen + "s%1s\n", (o == null ? "null" : o.toString()), 508 | tableFormat.division); 509 | 510 | i++; 511 | } 512 | 513 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 4 + (p.size() + "").length())); 514 | 515 | } else { 516 | 517 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 518 | 519 | for (Object o : p) { 520 | System.out.printf("%1s%-" + maxCharLen + "s%1s\n", tableFormat.division, 521 | (o == null ? "null" : o.toString()), tableFormat.division); 522 | } 523 | 524 | System.out.println(Util.makePadding(tableFormat.border, maxCharLen + 2)); 525 | 526 | } 527 | } else { 528 | if (enumerated) { 529 | int i = 0; 530 | for (Object o : p) { 531 | System.out.printf("%-" + ((p.size() + "").length() + 2) + "s", i + ": "); 532 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 533 | i++; 534 | } 535 | } else { 536 | for (Object o : p) { 537 | System.out.printf("%-" + maxCharLen + "s\n", (o == null ? "null" : o.toString())); 538 | } 539 | } 540 | } 541 | } 542 | } --------------------------------------------------------------------------------