Skip to content

Instantly share code, notes, and snippets.

@JabDoesThings
Created August 11, 2017 19:00
Show Gist options
  • Save JabDoesThings/ee12041f374bff07fe2f1cd7ce2cecb2 to your computer and use it in GitHub Desktop.
Save JabDoesThings/ee12041f374bff07fe2f1cd7ce2cecb2 to your computer and use it in GitHub Desktop.
A printable utility class.
package jab.util;
/**
* Interface designed to handle advanced printing. Only available in JDK8+.
*
* @author Jab
*
*/
public interface Printable {
public static boolean PRINT_NEW_LINE = false;
/**
* Static shortcut to the newline property from the System running this
* program.
*/
static String newLine = System.getProperty("line.separator");
/**
* Prints lines with "getName(): [message...]".
*
* @param messages
*/
public default void println(Object... messages) {
if (messages.length == 0) {
System.out.println();
} else {
// Grab the name of the instance.
String name = getName();
// Create the header, based on if the name String is null of empty.
String header = name == null || name.isEmpty() ? "" : name + ": ";
// Go through each Object, and print them as a separate line.
String compiledString = "";
for (Object message : messages) {
compiledString += header + message + (PRINT_NEW_LINE ? newLine : "");
}
// Print the result.
System.out.print(Indentation.getIndentationsString() + compiledString);
}
}
/**
* Prints lines with "getName(): [message...]".
*
* @param messages
*/
public default void errln(Object... messages) {
if (messages.length == 0) {
System.err.println();
} else {
// Grab the name of the instance.
String name = getName();
// Create the header, based on if the name String is null of empty.
String header = name == null || name.isEmpty() ? "" : name + ": ";
// Go through each Object, and print them as a separate line.
String compiledString = "";
for (Object message : messages) {
compiledString += header + message + (PRINT_NEW_LINE ? newLine : "");
}
// Print the result.
System.err.print(Indentation.getIndentationsString() + compiledString);
}
}
/**
* Prints a message with a header, without a new-line.
*
* @param message
*/
public default void printH(Object message) {
// Grab the name of the instance.
String name = getName();
// Create the header, based on if the name String is null of empty.
String header = name == null || name.isEmpty() ? "" : name + ": ";
// Print the result.
System.out.print(Indentation.getIndentationsString() + header + message);
}
/**
* Prints a message, without a new-line.
*
* @param message
*/
public default void print(Object message) {
// Print the result.
System.out.print(message);
}
/**
*
* @param throwable
*/
public default void stackTrace(Throwable throwable) {
stackTrace(throwable.getClass().getName(), throwable);
}
/**
*
* @param errorText
* @param throwable
*/
public default void stackTrace(String errorText, Throwable throwable) {
if (errorText != null && !errorText.isEmpty()) {
errorText = errorText.trim() + ": " + throwable.getCause();
}
errln("Error: " + (errorText != null ? errorText : "") + ": " + throwable.getMessage());
for (StackTraceElement element : throwable.getStackTrace()) {
errln(element);
}
}
/**
*
*/
public default void stackTrace() {
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
errln(element);
}
}
/**
* Returns the Class name as a String.
*
* @return
*/
public default String getClassName() {
String[] split = getClass().getName().split("\\.");
return split[split.length - 1];
}
/**
* Globally indents all Printable Objects by 1. (uses \t for every
* indentation)
*/
public default void indent() {
indent(1);
}
/**
* Globally indents all Printable Objects by a given amount. (uses /t for
* every indentation)
*
* @param amount
*/
public default void indent(int amount) {
Indentation.indent(amount);
}
/**
* Globally unindents all Printable Objects by 1. (uses /t for every
* indentation)
*/
public default void unindent() {
unindent(1);
}
/**
* Globally unindents all Printable Objects by a given amount. (uses /t for
* every indentation)
*
* @param amount
*/
public default void unindent(int amount) {
Indentation.unindent(amount);
}
public default void resetIndentation() {
Indentation.reset();
}
/**
* Grabs the name of the instance.
*
* @return
*/
public String getName();
}
/**
* Appended Class to handle the static indentation functionality of Printable.
*
* @author Jab
*
*/
final class Indentation {
/** The amount of indentations for a print. */
static int indentations = 0;
/** The rendered indentations string. */
static String _indentationsString = "";
/** Flag to note when the indentations string needs to be re-rendered. */
static boolean dirty;
/**
* Globally indents all Printable Objects by 1. (uses /t for every
* indentation)
*/
static void indent(int value) {
indentations += value;
dirty = true;
}
/**
* Resets indentation.
*/
static void reset() {
indentations = 0;
dirty = true;
}
/**
* Globally unindents all Printable Objects by 1. (uses /t for every
* indentation)
*/
static void unindent(int value) {
indentations -= value;
dirty = true;
}
/**
* Returns a built String to represent the amount of indentations a
* Printable Object uses when printing.
*
* @return
*/
static String getIndentationsString() {
// If dirty, rebuild the rendered indentation String.
if (dirty) {
// Reset the String.
_indentationsString = "";
// Loop through each count, adding a new tab to the rendered String.
for (int index = 0; index < Indentation.indentations; index++) {
_indentationsString += "\t";
}
// Set the dirty flag to false, as we have rendered the indentation
// String.
dirty = false;
}
// Return the rendered indentation String.
return _indentationsString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment