public final class Out extends java.lang.Object implements TrueStatic
A collection of static methods providing advanced output
to System.out
, mostly for debugging needs.
In particular, implements:
toString
method;
println
calls.
This class can be executed since JVM 1.0 and can be compiled since JDK 1.1.
Note! Under JVM 1.0, the following methods
returns the standard string representation of the given number, as the standardString.valueOf
method. Since JVM 1.1,
these methods work properly.Modifier and Type | Class and Description |
---|---|
static interface |
Out.Flusher
See
flush() method. |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
abc(int v)
Equivalent to
|
static java.lang.String |
abc(long v)
Returns the representation of the integer argument in a special
"spreadsheet" form: 0 is represented as "A",
1 as "B", 2 as "C", then
"D", "E", ..., "Z",
"AA", "AB", ..., "AZ",
"BA", "AB", ..., "ZY", "ZZ",
"AAA", "AAB", ...
|
static void |
addFlusher(Out.Flusher flusher)
Registers (adds to the internal list) a flusher,
that (it's
flush() method) will be called
in the flush() method. |
static java.lang.String |
avoidDebugSignalsForExceptions(java.lang.String s)
Replaces every char sequence "Exception" in the string
s with
the char sequence "[[E]]xception". |
static java.lang.String |
dec(double v,
int d)
Returns the string decimal representation of the double argument
with
d digits after the decimal point. |
static java.lang.String |
dec(double v,
int d,
int padLen)
Equivalent to
|
static java.lang.String |
deTab(java.lang.String s,
int tabLen)
Equivalent to
|
static java.lang.String |
deTab(java.lang.String s,
int[] tabPositions)
Equivalent to
|
static java.lang.String |
deTab(java.lang.String s,
int[] tabPositions,
char lineSeparator)
Performs detab operation with the given set of tabulation positions.
|
static java.lang.String |
deTab(java.lang.String s,
int tabLen,
char lineSeparator)
Performs standard detab operation with the given tabulation length.
|
static java.lang.String |
dup(char c,
int len)
Returns a string consisting of
len copies of the
specified character c . |
static java.lang.String |
exp(double v,
int d)
Returns the string decimal representation of the double argument
in exponent form ("scientific" notation) with
d
digits after the decimal point. |
static java.lang.String |
exp(double v,
int d,
int padLen)
Equivalent to
|
static void |
flush()
Immediately flushs the buffer filled by previous calls of
print(String) ,
println(String) ,
printNoCorrections(String)
to the System.out . |
static long |
getPrintDelay()
Returns the print delay set by the last
setPrintDelay(long) call. |
static java.lang.String |
hex(byte v)
Returns a hexadecimal unsigned representation of the byte argument.
|
static java.lang.String |
hex(int v)
Returns a hexadecimal unsigned representation of the int argument.
|
static java.lang.String |
hex(long v)
Returns a hexadecimal unsigned representation of the long argument.
|
static java.lang.String |
hex(short v)
Returns a hexadecimal unsigned representation of the short argument.
|
static boolean |
isAutomaticAvoidingDebugSignalsForExceptions()
Returns the global flag set by the last
setAutomaticAvoidingDebugSignalsForExceptions(boolean) call
(false by default). |
static java.lang.String |
join(java.lang.Object o,
java.lang.String separator)
If
o is an instance of Java array, standard collection,
map (or Java 1 Dictionary ), Iterator
(or Java 1 Enumeration ), joins and returns as a string
the standard string representations for all elements of this array
(or collection, or all objects returned by the iterator),
separating elements by the given separator . |
static java.lang.String |
pad(int v,
int padLen)
Equivalent to
|
static java.lang.String |
pad(long v,
int padLen)
Equivalent to
|
static java.lang.String |
pad(java.lang.String s,
int padLen)
If
padLen < s.length() , returns a reference to the original
s string object; in other case, returns the s
padded leftward to padLen length by the space character: |
static java.lang.String |
pad(java.lang.String s,
int padLen,
char pad,
boolean right)
If
padLen < s.length() , returns a reference to the original
s string object; in other case, returns the s
padded leftward or rightward to padLen length by the given pad
character: |
static long |
parseLongAbc(java.lang.String s)
Returns such an integer number
x that
. |
static void |
print(java.lang.String value)
Prints the given string on
System.out . |
static void |
printFlush(java.lang.String value)
A brief equivalent of two
calls. |
static void |
println()
A brief equivalent of
call (or : remind that
print method automatically replaces "\n"
with GlobalProperties.LINE_SEPARATOR ). |
static void |
println(java.lang.String value)
This method differs from
System.out.println in the same
respects as the print(String) method differs from
System.out.print . |
static void |
printlnFlush(java.lang.String value)
A brief equivalent of two
calls. |
static void |
printNoCorrections(java.lang.String value)
An analog of
print(String) method that doesn't perform any
replacements in the value string. |
static void |
removeFlusher(Out.Flusher flusher)
Deregisters (removes from the internal list) the given flusher.
|
static java.lang.String |
replace(java.lang.String s,
java.lang.String oldSubstring,
java.lang.String newSubstring)
Returns a new string resulting from replacing all occurrences of
oldSubstring in the s source string
with newSubstring . |
static java.lang.String |
replaceLFToLineSeparator(java.lang.String s)
Replaces every "\n" character in the string
s with
GlobalProperties.LINE_SEPARATOR string. |
static java.lang.String |
rightPad(java.lang.String s,
int padLen)
If
padLen < s.length() , returns a reference to the original
s string object; in other case, returns the s
padded rightward to padLen length by the space character: |
static void |
setAutomaticAvoidingDebugSignalsForExceptions(boolean value)
Sets the flag defining whether
print(String) and
println(String) methods should perform replacements
provided by avoidDebugSignalsForExceptions(String)
method in the printed text. |
static void |
setPrintDelay(long ms)
Sets the delay performed after the last call of
print(String) ,
println(String) ,
printNoCorrections(String)
methods until the moment when the printed text will
be really sent to System.out (by System.out.print
call). |
static java.lang.String |
spaced(int v,
char space)
Equivalent to
|
static java.lang.String |
spaced(long v,
char space)
Returns the string decimal representation of the long integer argument
where all terns of digits (from the right to the left) are separated
by the given space character.
|
public static java.lang.String replace(java.lang.String s, java.lang.String oldSubstring, java.lang.String newSubstring)
oldSubstring
in the s
source string
with newSubstring
.
If s == null
, no exceptions are thrown;
in this case, the method returns null
.
If oldSubstring
does not occur in the
the s
source string, then a reference to s
object is returned.
This method can be a useful addition to the standard String.replace
method, especially for debug output. It works more quickly than
Java 1.4 String.replaceAll
method, and is more simple in use.
s
- the stringoldSubstring
- the old substringnewSubstring
- the new substrings
string by replacing every
occurrence of oldSubstring
with newSubstring
java.lang.NullPointerException
- if oldSubstring == null
or newSubstring == null
java.lang.IllegalArgumentException
- if oldSubstring.length() == 0
public static java.lang.String avoidDebugSignalsForExceptions(java.lang.String s)
s
with
the char sequence "[[E]]xception".
It allows to avoid wrong signals generated by some IDEs
when they detect "Exception" word in the standard application
output while debugging the program. For example, Borland JavaBuilder(TM)
sounds a clank every time when the debugged application prints "Exception" into
System.out
or System.err
. It's a good idea usually,
when the application prints standard exception information (for
example, by e.printStackTrace()
call). But it can be very
bad if you need to print some debugging text containing a lot of
"Exception" words while normal execution of the application.
This method is called internally by print(String)
and
println(String)
methods of this class, if you
set the corresponding flag by
setAutomaticAvoidingDebugSignalsForExceptions(boolean)
method.
s
- the corrected string; can be null
replace
(s,"Exception","[[E]]xception")
;
null
if s == null
print(String)
,
println(String)
,
setAutomaticAvoidingDebugSignalsForExceptions(boolean)
public static java.lang.String replaceLFToLineSeparator(java.lang.String s)
s
with
GlobalProperties.LINE_SEPARATOR
string.
If "\n" character is already a part of some line separator,
it is not replaced. So, you can also apply method to texts
where the lines are separated by GlobalProperties.LINE_SEPARATOR
.
It can simplify generation of a text with lines separated
by GlobalProperties.LINE_SEPARATOR
.
This method is called internally by print(String)
and
println(String)
methods of this class.
s
- the corrected string; can be null
replace
(s,"\n",GlobalProperties.LINE_SEPARATOR)
;
null
if s == null
print(String)
,
println(String)
public static java.lang.String dup(char c, int len)
len
copies of the
specified character c
.c
- the character that is duplicatedlen
- the number of copies of c
characters in the result stringlen
characters, each character is equal to c
public static java.lang.String pad(java.lang.String s, int padLen)
padLen < s.length()
, returns a reference to the original
s
string object; in other case, returns the s
padded leftward to padLen
length by the space character:dup(' ', padLen - s.length()) + s
s
- the string that should be paddedpadLen
- the minimal length of the resulting stringpadLen
length by the space characterpublic static java.lang.String rightPad(java.lang.String s, int padLen)
padLen < s.length()
, returns a reference to the original
s
string object; in other case, returns the s
padded rightward to padLen
length by the space character:s + dup(' ', padLen - s.length())
s
- the string that should be paddedpadLen
- the minimal length of the resulting stringpadLen
length by the space characterpublic static java.lang.String pad(java.lang.String s, int padLen, char pad, boolean right)
padLen < s.length()
, returns a reference to the original
s
string object; in other case, returns the s
padded leftward or rightward to padLen
length by the given pad
character:right? s + dup(pad, padLen - s.length()): dup(pad, padLen - s.length()) + s
s
- the string that should be paddedpadLen
- the minimal length of the resulting stringpad
- the character used for paddingright
- if true
, the string is padded rightward, else leftwardpadLen
length by the pad
characterpublic static java.lang.String pad(int v, int padLen)
pad(String.valueOf(v),padLen)
v
- the integer number that should be converted to string and paddedpadLen
- the minimal number of characters of the resulting stringpadLen
length by the space characterpad(String, int)
public static java.lang.String pad(long v, int padLen)
pad(String.valueOf(v),padLen)
v
- the long number that should be converted to string and paddedpadLen
- the minimal number of characters of the resulting stringpadLen
length by the space characterpad(String, int)
public static java.lang.String deTab(java.lang.String s, int tabLen)
deTab(s,tabLen,'\n')
s
- the multiline text where all tab characters (\t) should be replaced with spacestabLen
- every \t charater corresponds to tabLen
positionsdeTab(String, int, char)
public static java.lang.String deTab(java.lang.String s, int tabLen, char lineSeparator)
s
text are separated
by one character lineSeparator
or by a sequence of characters
where the last character is lineSeparator
. In other words,
the position in the line of the first character after each
lineSeparator
is 0.
(We consider that the positions in lines start from 0, not from 1.)
For example:
deTab("\t",4,'\n') == " " deTab("aa\tbb",4,'\n') == "aa bb" deTab("aa\t\r\n\tbb",4,'\n') == "aa \r\n bb" but deTab("aa\t\n\r\tbb",4,'\n') == "aa \n\r bb"
s
- the multiline text where all tab characters (\t) should be replaced with spacestabLen
- every \t charater corresponds to tabLen
positionslineSeparator
- the character used for line separation, or the last character
in the sequence that is used for line separationpublic static java.lang.String deTab(java.lang.String s, int[] tabPositions)
deTab(s,tabPositions,'\n')
s
- the multiline text where all tab characters (\t) should be replaced with spacestabPositions
- tabulation positionsdeTab(String, int)
,
deTab(String, int[], char)
public static java.lang.String deTab(java.lang.String s, int[] tabPositions, char lineSeparator)
s
is replaced
with several spaces ' ' (at least by one space) so that the index of the
next character in this line becomes equal to the nearest next element of
tabPositions
array which is >= the current
position in the line. If there are no available elements in
tabPositions
(the array is exhausted), '\t' character
is merely replaced with space ' '.
More simple method deTab(String, int, char)
is equivalent
to the call of this one when tabPositions
array contains
an "infinite" (large enough) sequence of integer numbers
8, 16, 24, ..., 8*n, ...
This method assumes that the lines in the s
text are separated
by one character lineSeparator
or by a sequence of characters
where the last character is lineSeparator
. In other words,
the position in the line of the first character after each
lineSeparator
is 0.
(We consider that the positions in lines start from 0, not from 1.)
For example:
deTab("\t",new int[] {2},'\n') == " " deTab("aa\tbb",new int[0],'\n') == "aa bb" deTab("aa\tbb\tcc",new int[] {4,10,20},'\n') == "aa bb cc" but deTab("aa\tbb\tcc",new int[] {1,10,20},'\n') == "aa bb cc"In the last case, the first element of
tabPositions
array is less than the position of the first '\t' character; so,
this element was skipped, and elements 10 and 20 were used.
This method is very convenient for generating logs and reports formatted by spaces.
s
- the multiline text where all tab characters (\t) should be replaced with spacestabPositions
- tabulation positionslineSeparator
- the character used for line separation, or the last character
in the sequence that is used for line separationdeTab(String, int, char)
public static java.lang.String hex(byte v)
Integer.toHexString(v)
call, the result
always contains 2 digits (leaded by '0' if needed).v
- the byte argumentpublic static java.lang.String hex(short v)
Integer.toHexString(v)
call, the result
always contains 4 digits (leaded by '0' if needed).v
- the short argumentpublic static java.lang.String hex(int v)
Integer.toHexString(v)
call, the result
always contains 8 digits (leaded by '0' if needed).v
- the int argumentpublic static java.lang.String hex(long v)
Long.toHexString(v)
call, the result
always contains 16 digits (leaded by '0' if needed).v
- the long argumentpublic static java.lang.String dec(double v, int d)
d
digits after the decimal point. If
d <= 0
, the mantissa is not included in the result.
Examples:
dec(100*Math.PI,5) == "314.15927", dec(100.0/3.0,1) == "33.3", dec(-2.0/3.0,0) == "-1"As for
String.valueOf(v)
call, the result is
always written in american (scientific) standard, regardless
of the current regional settings.v
- the double numberd
- the number of digits after the decimal pointexp(double, int)
public static java.lang.String exp(double v, int d)
d
digits after the decimal point. If d <= 0
,
the mantissa is not included in the result.
Examples:
exp(100*Math.PI,5) == "3.14159E2", exp(100.0/3.0,5) == "3.3E1", dec(-2.0/3.0,0) == "-7E-1"As for
String.valueOf(v)
call, the result is
always written in american (scientific) standard, regardless
of the current regional settings.v
- the double numberd
- the number of digits after the decimal pointdec(double, int)
public static java.lang.String dec(double v, int d, int padLen)
v
- the double numberd
- the number of digits after the decimal pointpadLen
- the minimal number of characters in the resultpadLen
length by the space characterexp(double, int, int)
public static java.lang.String exp(double v, int d, int padLen)
v
- the double numberd
- the number of digits after the decimal pointpadLen
- the minimal number of characters in the resultpadLen
length by the space characterdec(double, int, int)
public static java.lang.String spaced(int v, char space)
spaced(v,space)
v
- the integer numberspace
- the character used for separating terns of digitspublic static java.lang.String spaced(long v, char space)
Examples:
spaced(345234,' ') == "345234" spaced(-2234,',') == "-2,234" spaced(22,' ') == "22"
v
- the integer numberspace
- the character used for separating terns of digitspublic static java.lang.String abc(int v)
abc((long)v)
v
- the integer numberparseLongAbc(String)
public static java.lang.String abc(long v)
v
- the integer numberparseLongAbc(String)
public static long parseLongAbc(java.lang.String s)
x
that
abc(x)
.equals(s)
.public static java.lang.String join(java.lang.Object o, java.lang.String separator)
o
is an instance of Java array, standard collection,
map (or Java 1 Dictionary
), Iterator
(or Java 1 Enumeration
), joins and returns as a string
the standard string representations for all elements of this array
(or collection, or all objects returned by the iterator),
separating elements by the given separator
.
For all kinds of maps, joins "key=value" pairs instead
elements. For null
, returns an empty string.
For all other classes of o
object,
throws an exception.
Convenient while debugging the code processing not-too-large arrays and collections.
Examples:
join(new int[] {1,2,3},",") == "1,2,3" join(new ArrayList(),";") == "" join(null,",") == ""
o
- the given array, collection, map, iterator, or null
valueseparator
- the string used for separating elementsjava.lang.IllegalArgumentException
- if the class of o
argument is unsuitablepublic static void print(java.lang.String value)
System.out
. Unlike the standard
System.out.print
method, this one perform the following
additional actions.replaceLFToLineSeparator(String)
are performed ("value = replaceLFToLineSeparator(value)
").
So, this method can be safely use for printing
several lines in one simple call, such asOut.print("First line\nSecond line")
setAutomaticAvoidingDebugSignalsForExceptions(boolean)
method, is set, the replacements provided by
avoidDebugSignalsForExceptions(String)
are performed ("value = avoidDebugSignalsForExceptions(value)
").
setPrintDelay(long)
call,
then the resulting string will not be printed immediately. Instead,
it will be added to an internal buffer that will be fully printed
(using System.out.print(buffer)
call) after
the specified delay following the last call of
this method (or println(String)
method, or
printNoCorrections(String)
).
If no print delay was specified, the resulting string will
be just printed by System.out.print(value)
call. Such a delay can greatly increase output performance,
especially while debugging under some IDE.value
- the printed string; if null
, the "null"
string will be printedreplaceLFToLineSeparator(String)
,
avoidDebugSignalsForExceptions(String)
,
setAutomaticAvoidingDebugSignalsForExceptions(boolean)
,
flush()
,
setPrintDelay(long)
,
println(String)
public static void printFlush(java.lang.String value)
print(value)
;
flush()
calls.value
- the printed stringprint(String)
,
flush()
public static void println()
println
("")
call (or print
("\n")
: remind that
print
method automatically replaces "\n"
with GlobalProperties.LINE_SEPARATOR
).print(String)
,
println(String)
public static void printlnFlush(java.lang.String value)
println(value)
;
flush()
calls.value
- the printed stringprintln(String)
,
flush()
public static void println(java.lang.String value)
System.out.println
in the same
respects as the print(String)
method differs from
System.out.print
. It's equivalent toprint(value + "\n");call. (
print
call automatically replaces "\n"
with GlobalProperties.LINE_SEPARATOR
.)value
- the printed string (followed by line separator
).replaceLFToLineSeparator(String)
,
avoidDebugSignalsForExceptions(String)
,
setAutomaticAvoidingDebugSignalsForExceptions(boolean)
,
flush()
,
setPrintDelay(long)
,
print(String)
public static void printNoCorrections(java.lang.String value)
print(String)
method that doesn't perform any
replacements in the value
string. So, it only
performs a delay before printing (if it was set by
setPrintDelay(long)
call) in addition to standard
System.out.print
actions.value
- the printed stringprint(String)
public static void flush()
print(String)
,
println(String)
,
printNoCorrections(String)
to the System.out
.
Does nothing if no positive delay was specified by
setPrintDelay(long)
call.
Also, regardless on the delay value, this method
calls flush()
methods for all objects
registered by addFlusher(Out.Flusher)
method.
This method is useful only if you need to synchronize
the output performed by this class and the standard
output tools such as direct System.out.println
calls or printing to System.err
(that is
not buffered by this class at all).
print(String)
,
println(String)
,
setPrintDelay(long)
public static void setPrintDelay(long ms)
print(String)
,
println(String)
,
printNoCorrections(String)
methods until the moment when the printed text will
be really sent to System.out
(by System.out.print
call). This value is global for this class and usually should be set only
once at the start of your application.ms
- new delay in milliseconds; 0
value (which is default)
means that no delay will be performedjava.lang.IllegalArgumentException
- if ms < 0
print(String)
,
println(String)
,
flush()
public static long getPrintDelay()
setPrintDelay(long)
call.
Return (0
before the first call of setPrintDelay(long)
.setPrintDelay(long)
callpublic static void setAutomaticAvoidingDebugSignalsForExceptions(boolean value)
print(String)
and
println(String)
methods should perform replacements
provided by avoidDebugSignalsForExceptions(String)
method in the printed text. This flag is global for this class
and usually should be set only once at the start of your application.
Default value of this flag if false
(no replacements).
We recommend to set this flag to true
in large
applications that use console for debugging needs.value
- new value of this flagprint(String)
,
println(String)
,
avoidDebugSignalsForExceptions(String)
public static boolean isAutomaticAvoidingDebugSignalsForExceptions()
setAutomaticAvoidingDebugSignalsForExceptions(boolean)
call
(false
by default).setAutomaticAvoidingDebugSignalsForExceptions(boolean)
callpublic static void addFlusher(Out.Flusher flusher)
flush()
method) will be called
in the flush()
method.flusher
- New registered flusher.removeFlusher(Out.Flusher)
,
flush()
public static void removeFlusher(Out.Flusher flusher)
flusher
- New registered flusher.addFlusher(Out.Flusher)
,
flush()