From: Janus Varmarken Date: Sat, 19 Jan 2019 09:13:14 +0000 (-0800) Subject: Add utility for printing to a file and to std out simultaneously X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=44526223045400a3ac2e14d92ac76ab2f3dfcd67;p=pingpong.git Add utility for printing to a file and to std out simultaneously --- diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PrintWriterUtils.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PrintWriterUtils.java new file mode 100644 index 0000000..167993d --- /dev/null +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PrintWriterUtils.java @@ -0,0 +1,43 @@ +package edu.uci.iotproject.io; + +import java.io.PrintWriter; + +/** + * Utility methods for (jointly) printing to a {@link PrintWriter} (and standard output). + * + * @author Janus Varmarken {@literal } + * @author Rahmadi Trimananda {@literal } + */ +public final class PrintWriterUtils { + + private PrintWriterUtils() { + // Disallow instantiation. Static-only class. + } + + /** + * Invoke {@link PrintWriter#println(Object)} passing {@code line} as argument while also printing {@code line} to + * standard output if {@code duplicateToStdOut} is {@code true}. + * @param line The line to be printed. + * @param writer The {@link PrintWriter} that is to print {@code line}. + * @param duplicateToStdOut Set to {@code true} if {@code line} should also be printed in standard output. + */ + public static void println(Object line, PrintWriter writer, boolean duplicateToStdOut) { + if (duplicateToStdOut) { + System.out.println(line); + } + writer.println(line); + } + + /** + * Make writer (and standard output, if {@code duplicateToStdOut} is {@code true}) print an empty line. + * @param writer The writer that {@link PrintWriter#println()} is to be invoked on. + * @param duplicateToStdOut If {@code true}, prints an empty line to standard output. + */ + public static void printEmptyLine(PrintWriter writer, boolean duplicateToStdOut) { + if (duplicateToStdOut) { + System.out.println(); + } + writer.println(); + } + +}