bb.io
Class ConsoleUtil

java.lang.Object
  extended by bb.io.ConsoleUtil

public final class ConsoleUtil
extends Object

Provides static utility methods for dealing with consoles (i.e. the default System.in and System.out).

One problem that must be dealt with is if output requests occur very rapidly: there is no way that the human eye can meaningfully keep up with the changes, but to insist that every line be output drains cpu resources for no human benefit. To deal with this, the output method overwriteLine merely notes output requests as its only immediate action. An internal thread will take care of periodically writing the last requested line, and will disregard any other requested lines. Thus, the output will appear to be changing as rapidly as is meaningful to the human eye, yet the cpu usage will be limited. In contrast, the eraseLine method always immediately executes.

This class is multithread safe: every method is synchronized on this class.

Author:
Brent Boyer

Nested Class Summary
private static class ConsoleUtil.LineWriterTask
           
static class ConsoleUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private static String backspacesLine
           
private static String blankLine
           
static String clearLine
          A piece of text that when printed clears the current line on the console.
private static long delay
           
private static String line
           
private static int lineLength
           
static int maxCharsPerLine
          Maximum number of chars that will appear on a line of console output.
private static long period
           
private static Timer timer
           
 
Constructor Summary
private ConsoleUtil()
          This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.
 
Method Summary
static void eraseLine()
          Erases the console's current line.
static void overwriteLine(String s)
          Merely records a request to replace the current line with s.
private static void writeLine()
          Writes line.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lineLength

private static final int lineLength
See Also:
Constant Field Values

maxCharsPerLine

public static final int maxCharsPerLine
Maximum number of chars that will appear on a line of console output. The value here is a hard coded guess, and very well could be wrong for many screen size/font size combinations.

Contract: is > 0.

See Also:
Constant Field Values

backspacesLine

private static final String backspacesLine
See Also:
Constant Field Values

blankLine

private static final String blankLine

clearLine

public static final String clearLine
A piece of text that when printed clears the current line on the console.

Contract: is never null.


delay

private static final long delay
See Also:
Constant Field Values

period

private static final long period
See Also:
Constant Field Values

timer

private static final Timer timer

line

private static String line
Constructor Detail

ConsoleUtil

private ConsoleUtil()
This sole private constructor suppresses the default (public) constructor, ensuring non-instantiability outside of this class.

Method Detail

overwriteLine

public static void overwriteLine(String s)
Merely records a request to replace the current line with s. Note: if s == null, it is replaced with the empty String before being assigned to line.


eraseLine

public static void eraseLine()
Erases the console's current line.


writeLine

private static void writeLine()
Writes line. If line is longer than maxCharsPerLine, it will be truncated by a call to StringUtil.keepWithinLength. Should only be called by timer.