Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Sandbox;
- import java.io.*;
- import java.lang.reflect.*;
- import java.nio.file.*;
- import java.util.*;
- /** Contains various commonly used procedures that should be part of the JDK, but aren't:<br>
- * - better and shorter print methods<br>
- * - safely write a list or array to a file and read a list from a file<br>
- * Most often used outside of this class:<br>
- * - <code>print(Object)</code><br>
- * - <code>fromFile(String)</code><br>
- * - <code>toFile(String,List<Object>)</code><br>
- * - <code>toFile(String,Object)</code><br>
- * The complex methods, most likely to be the cause of bugs:<br>
- * - <code>stringify(Object)</code><br>
- * - <code>prepareFile(String,boolean)</code> */
- public class Util{
- /** If true, errors will be printed to console if things go wrong. */
- public static boolean output=true;
- public static boolean stacks=true;
- /** Converts an object (usually a list, map, etc.) into a string.<br>
- * Arrays become similar to <code>[1,2,3]</code>, lists to <code>{1,2,3}</code> and maps to <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be converted to <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static String stringify(Object arg){
- if(arg.getClass().isArray()){
- StringBuilder builder=new StringBuilder("[");
- int length=Array.getLength(arg);
- for(int i=0;i<length;i++){
- // similar to "builder+=arg[i]"
- builder.append(stringify(Array.get(arg,i)));
- // add a comma between all elements, but not after the last
- if(i<length-1) builder.append(",");
- }
- builder.append("]");
- return builder.toString();
- // a map is just a set (collection) of Map.Entry
- }else if(arg instanceof Map) return stringify(((Map)arg).entrySet());
- else if(arg instanceof Collection){
- StringBuilder builder=new StringBuilder("{");
- boolean first=true;
- for(Object element:(Collection)arg){
- // add a comma before all elements that are not the first=add a comma between all elements, but not before the first or after the last
- if(!first) builder.append(",");
- first=false;
- builder.append(stringify(element));
- }
- builder.append("}");
- return builder.toString(); // not recursion
- }else if(arg instanceof Map.Entry) return stringify(((Map.Entry)arg).getKey())+"="+stringify(((Map.Entry)arg).getValue());
- else return arg.toString();
- }
- /** Prints a single object to the console without appending a newline. Use <code>print</code> for also appending a newline.<br>
- * Similar to <code>System.out.print</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void pri(Object arg){
- if(arg instanceof String&&((String)arg).startsWith("ERROR")) System.err.print(stringify(arg));
- else System.out.print(stringify(arg));
- }
- public static void printArray(Object[] array){
- for(Object element:array){
- print(element);
- }
- }
- /** Prints a newline. Shortcut for <code>System.out.println()</code>. */
- public static void print(){
- System.out.println();
- }
- /** Prints one object to the console and appends a newline. Use <code>pri</code> for printing without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0){
- pri(arg0);
- System.out.println();
- }
- /** Prints two objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- }
- /** Prints three objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- }
- /** Prints four objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- }
- /** Prints five objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- }
- /** Prints six objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- }
- /** Prints seven objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- }
- /** Prints eight objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- }
- /** Prints nine objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- }
- /** Prints ten objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- }
- /** Prints eleven objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9,Object arg10){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- pri(arg10);
- System.out.println();
- }
- /** Prints twelve objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9,Object arg10,Object arg11){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- pri(arg10);
- System.out.println();
- pri(arg11);
- System.out.println();
- }
- /** Prints thirteen objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9,Object arg10,Object arg11,Object arg12){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- pri(arg10);
- System.out.println();
- pri(arg11);
- System.out.println();
- pri(arg12);
- System.out.println();
- }
- /** Prints fourteen objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9,Object arg10,Object arg11,Object arg12,Object arg13){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- pri(arg10);
- System.out.println();
- pri(arg11);
- System.out.println();
- pri(arg12);
- System.out.println();
- pri(arg13);
- System.out.println();
- }
- /** Prints fifteen objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9,Object arg10,Object arg11,Object arg12,Object arg13,Object arg14){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- pri(arg10);
- System.out.println();
- pri(arg11);
- System.out.println();
- pri(arg12);
- System.out.println();
- pri(arg13);
- System.out.println();
- pri(arg14);
- System.out.println();
- }
- /** Prints sixteen objects to the console and appends a newline after each. Use <code>pri</code> for printing one object without appending a newline.<br>
- * Use <code>print</code> multiple times for more objects. Varargs would interfere with arrays of objects (not of primitive types) and adding methods with up to 255 arguments would lag autocomplete too much in some IDEs.<br>
- * Similar to <code>System.out.println</code>, but better.<br>
- * Prints arrays like <code>[1,2,3]</code>, lists like <code>{1,2,3}</code> and maps like <code>{1=A,2=B,3=C}</code>.<br>
- * Nested arrays, lists and maps also work, for example a list of arrays of arrays would be displayed as <code>{[[1,2],[3,4]],[[5,6],[7,8]]}</code>. */
- public static void print(Object arg0,Object arg1,Object arg2,Object arg3,Object arg4,Object arg5,Object arg6,Object arg7,Object arg8,Object arg9,Object arg10,Object arg11,Object arg12,Object arg13,Object arg14,Object arg15){
- pri(arg0);
- System.out.println();
- pri(arg1);
- System.out.println();
- pri(arg2);
- System.out.println();
- pri(arg3);
- System.out.println();
- pri(arg4);
- System.out.println();
- pri(arg5);
- System.out.println();
- pri(arg6);
- System.out.println();
- pri(arg7);
- System.out.println();
- pri(arg8);
- System.out.println();
- pri(arg9);
- System.out.println();
- pri(arg10);
- System.out.println();
- pri(arg11);
- System.out.println();
- pri(arg12);
- System.out.println();
- pri(arg13);
- System.out.println();
- pri(arg14);
- System.out.println();
- pri(arg15);
- System.out.println();
- }
- /** Reads the lines of a file at location <code>path</code> into a list of strings. */
- public static List<String> fromFile(String path){
- try{
- return Files.readAllLines(Paths.get(path));
- }catch(IOException e){
- if(output){
- print("ERROR: File \""+path+"\" does not exist.");
- if(stacks) e.printStackTrace();
- }
- return null;
- }
- }
- /** Empties the file at <code>path</code> and writes all elements of <code>lines</code> into it. Creates the file if it doesn't exist already.<br>
- * Returns <code>true</code> if writing was successful, else <code>false</code> */
- public static boolean toFile(String path,List lines){
- return toFile(path,false,lines);
- }
- /** Writes all elements of <code>lines</code> into a file at location <code>path</code>. Creates the file if it doesn't exist already. Empties the file first if <code>append</code> is <code>false</code>.<br>
- * Returns <code>true</code> if writing was successful, else <code>false</code> */
- public static boolean toFile(String path,boolean append,List<Object> lines){
- try(BufferedWriter writer=prepareFile(path,append)){
- // abort if prepareFile encountered an error
- if(writer==null) return false;
- for(int index=0;index<lines.size();index++){
- writer.write(stringify(lines.get(index)));
- if(index!=lines.size()-1) writer.newLine();
- }
- }catch(IOException e){
- if(output){
- print("ERROR: File \""+path+"\" is write-only.");
- if(stacks) e.printStackTrace();
- }
- return false;
- }
- return true;
- }
- /** Empties the file at <code>path</code> and writes all elements of <code>lines</code> into it. Creates the file if it doesn't exist already.<br>
- * Returns <code>true</code> if writing was successful, else <code>false</code> */
- public static boolean writeFile(String path,Object line){
- return writeFile(path,true,stringify(line));
- }
- /** Writes all elements of <code>lines</code> into a file at location <code>path</code>. Creates the file if it doesn't exist already. Empties the file first if <code>append</code> is <code>false</code>.<br>
- * Returns <code>true</code> if writing was successful, else <code>false</code> */
- public static boolean writeFile(String path,boolean append,Object line){
- try(BufferedWriter writer=prepareFile(path,append)){
- // abort if prepareFile encountered an error
- if(writer==null) return false;
- writer.write(stringify(line));
- }catch(IOException e){
- if(output){
- print("ERROR: File \""+path+"\" is write-only.");
- if(stacks) e.printStackTrace();
- }
- return false;
- }
- return true;
- }
- /** Gets a BufferedReader for writing to a file at location <code>path</code> and prepares the file. Mainly used in <code>toFile</code>. */
- public static BufferedWriter prepareFile(String path,boolean append){
- File file=new File(path);
- File folder=file.getParentFile();
- // create folder, if it doesn't exit
- if(!folder.exists()&&!folder.mkdirs()){
- print("ERROR: Could not create parent directory of "+path+".");
- return null;
- }
- try{
- // create a new file or use the existing one
- file.createNewFile();
- }catch(IOException e){
- if(output){
- print("ERROR: File \""+path+"\" can't be created.");
- if(stacks) e.printStackTrace();
- }
- return null;
- }
- try{
- BufferedWriter writer=new BufferedWriter(new FileWriter(path,append));
- // if new text should be appended after existing text, write a newline
- if(append&&file.length()!=0) writer.newLine();
- return writer;
- }catch(IOException e){
- if(output){
- print("ERROR: File \""+path+"\" is write-only.");
- if(stacks) e.printStackTrace();
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment