public final class StringFormatter extends java.lang.Object implements java.lang.Appendable, java.io.Flushable, StringFunctions_C.PrepareBufferPos
setIntPicture(long, String)
or setFloatPicture(long, String)
pos(int)
.
In the default overwrite mode the add methods do not insert in buffer with shifting the rest to right
(like StringBuffer.insert()), but they overwrite the content at the currrent position.
The wording 'add' means, the current position is increment, so the next add()-operation adds
something behind the previous add()-operation. In the insert mode the content at pos_ is shifted to right.
pos(int)
-operation is successfully. If the buffer in shorter as the required position, spaces will be filled
onto the required position. So a buffer content can also be filled first right, than left.Modifier and Type | Class and Description |
---|---|
private class |
StringFormatter.Appendable_Intern
Helper class only internally for static functions which outputs to Appendable,
It appends a prepared number of characters to this buffer on
pos_ ++ |
Modifier and Type | Field and Description |
---|---|
private StringFormatter.Appendable_Intern |
appendable
See
StringFormatter.Appendable_Intern , only internal usage. |
private boolean |
bInsert
True than add inserts, false than it overwrites.
|
private boolean |
bShouldLineoutClose |
protected java.lang.StringBuilder |
buffer |
private char |
cDecimalSeparator |
(package private) java.text.SimpleDateFormat |
dateFormatNewer |
(package private) java.text.SimpleDateFormat |
dateFormatOlder |
(package private) java.text.SimpleDateFormat |
dateFormatToday |
(package private) java.text.SimpleDateFormat |
dateFormatYear |
static short |
k1
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k2left
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k2right
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k4left
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k4right
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k6left
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k6right
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k8left
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
static short |
k8right
The constant determine the number of digits representing a (hex) value and the decision, use first byte left or right side.
|
protected java.lang.Appendable |
lineout
Destination to output a full line.
|
private static byte |
mBytesInWordBigEndian
If this bit is set in mode, the byte with the lower index is interpreted as higher part of word
and is written left in insertHexLine().
|
private static byte |
mNrofBytesInWord |
protected int |
pos_
The position of actual writing.
|
protected int |
posLine_ |
(package private) java.lang.String |
sDatePrefixNewer |
(package private) java.lang.String |
sDatePrefixOlder |
(package private) java.lang.String |
sDatePrefixToday |
(package private) java.lang.String |
sDatePrefixYear |
private char |
secondNewline
The last written Character on
append(char) . |
private java.lang.String |
sNewline |
private static java.lang.String |
spaces |
static java.lang.String |
version
Version, history and license.
|
Constructor and Description |
---|
StringFormatter() |
StringFormatter(java.lang.Appendable lineout,
boolean shouldClose,
java.lang.String newlineString,
int defaultBufferLength)
Constructs an instance with a linked line-out channel and a StringBuffer of the given length.
|
StringFormatter(int length)
Constructs an instance with a StringBuffer of the given length.
|
StringFormatter(java.lang.String str)
Constructs an instance with a StringBuffer initialized with the given string.
|
StringFormatter(java.lang.StringBuilder buffer)
Constructs an instance with a existing StringBuffer.
|
Modifier and Type | Method and Description |
---|---|
StringFormatter |
add(char ch) |
StringFormatter |
add(char[] str)
Adds at the current position a char[].
|
StringFormatter |
add(java.lang.CharSequence str)
Adds on the current position a string.
|
StringFormatter |
add(java.lang.String str)
Adds at the current position a string.
|
StringFormatter |
addBinary(int value,
java.lang.String sPicture,
java.lang.String sBitCharLo,
java.lang.String sBitCharHi)
Adds a number containing in a long variable in hexa form
|
StringFormatter |
addBool(boolean value,
java.lang.String hilo)
Adds one character for a boolean value.
|
void |
addBufferPos(char cc)
Adds a character either on end (Appendable#append)
or on a defined position
|
StringFormatter |
addDate(java.util.Date date,
java.text.SimpleDateFormat format) |
StringFormatter |
addFloat(double value,
int digitsBeforePoint,
int digitsAfterPoint)
adds a double value in a fix point representation without exponent.
|
int |
addFloatPicture(float src,
java.lang.String pict)
Writes a float value in technical representation with exponent as short char a..T
NOTE: This algorithm is taken over from C++ routines in strpict.cpp written by JcHartmut in 1993..1999.
|
StringFormatter |
addHex(long value,
int nrofDigitsArg)
Adds a number containing in a long variable in hexa form
|
StringFormatter |
addHex44(long value)
Adds a number in form 12ab'cd34, it is typical to show 4-byte-values at example addresses.
|
void |
addHexBlock(byte[] data,
int from,
int to,
short bytesInLine,
short mode)
Writes a block in hex.
|
StringFormatter |
addHexLine(byte[] data,
int idx,
int nrofBytes,
short mode)
Adds at the current position a line of hex numbers.
|
static java.lang.String |
addHexLn(byte[] data,
int length,
int idxStart)
Adds a line with representation of byte content in a fixed nice format.
|
private StringFormatter |
addHexWord_(byte[] data,
int idx,
short mode)
Adds a number as one word readed from data in hexa form, internal routine without prepareBufferPos
|
StringFormatter |
addHexWord(byte[] data,
int idx,
short mode)
Adds a number as one word readed from data in hexa form
|
StringFormatter |
addint(long nr,
java.lang.String sPict)
Sets a integer value at current position, use the picture to determine the number of characters etc.
|
StringFormatter |
addIntPicture(long nr,
java.lang.String sPict)
Deprecated.
|
StringFormatter |
addReplaceLinefeed(java.lang.CharSequence str,
java.lang.CharSequence replaceLinefeed,
int maxCharsArg)
Adds the given str at the current position but replaces line feed characters by given one.
|
StringFormatter |
addStringLine(byte[] data,
int idx,
int nrofBytes,
java.lang.String charsetName)
Adds a line of ascii representation of bytes.
|
StringFormatter |
append(char c)
Appends one character and flushes a line on end-line character.
|
StringFormatter |
append(java.lang.CharSequence csq)
It invokes
append(char) for any char.Therewith a \n and \r is handled specially. |
StringFormatter |
append(java.lang.CharSequence csq,
int start,
int end)
It invokes
append(char) for any char.Therewith a \n and \r is handled specially. |
void |
close()
close of StringFormatter stores yet existing text in a aggregated
lineout
A lineout is the first argument of ctor StringFormatter(Appendable, boolean, String, int)
Then this Stringformatter is reset() . |
java.lang.String |
convertTimestampToday(long timestamp)
Converts a timestamp in a String representation with
dateFormatToday etc. |
StringFormatter |
end()
Sets the current position to the end of the string.
|
void |
flush() |
int |
flushLine(java.lang.String sNewline)
Flushes the stored content in the lineout and adds the given sNewline
|
java.lang.CharSequence |
getBuffer()
Gets the accumulated content.
|
java.lang.String |
getContent()
Gets the accumulated content.
|
int |
getPos()
returns the current position for add in buffer.
|
StringFormatter |
insert()
sets the insert mode.
|
StringFormatter |
insert(java.lang.String str)
Inserts a String at current position with shifting the actual content to right.
|
int |
length()
returns the current length of string.
|
void |
newline() |
StringFormatter |
overwrite()
sets the overwrite mode.
|
StringFormatter |
pos(int newPos)
Sets the current write position to the given position.
|
StringFormatter |
pos(int newPos,
int minChars)
Sets the current write position to the given position.
|
int |
prepareBufferPos(int nrofChars)
Ensures, that the space in buffer started on pos is writeable with setCharAt.
|
int |
replace(java.lang.String search,
int from,
java.lang.CharSequence src)
Replaces the given String in the buffer with a new CharSequence.
|
int |
replaceHolder(java.lang.String name,
java.lang.String value)
Replaces a String "<&name>" with the given value.
|
StringFormatter |
reset()
Resets the internal buffer.
|
StringFormatter |
setAt(int pos_,
char ch) |
void |
setDecimalSeparator(char sep)
Sets an deviant decimal separator for floating point digigs, country-specific.
|
boolean |
setInsertMode(boolean insert)
sets the insert or overwrite mode, returns the current mode before change.
|
java.lang.String |
toString()
Same as getContent, overwrites Object.toString().
|
public static final java.lang.String version
pos_
as insertion position, and not (posLine_
+ pos_
)
which is necessary after this change, in many operations.
The change was done: prepareBufferPos(int)
returns the insertion pos, can be used, that is consequent.
The prepareBufferPos(int)
may also set the new position after insertion due to its argument 'nrofChars',
but this is not done. Instead the using operations are responsible to increment pos_
by itself.
It may be important that in special cases also posLine_
is incremented, that is done now correctly
but cannot be done in prepareBufferPos(int)
. Hence for change later, have a boolean argument whether set the new position.
Now it is primary correct.
addHexLine(byte[], int, int, short)
programmed but not used and not tested yet.
newline()
the position counts from 0 for the next line,
and a next line can be prepared. It is tested for a simple example in testJava_vishiaBase but not yet elaborately used.
The other also proper possibility is: Store the line in an Appendable, so that the StringFormatter has anytime only one line.
This may be better. The concept should be considered before elaborately usage. The idea was, for vertical test recording of signals.
StringFunctions_C.strPicture(long, String, String, char, org.vishia.util.StringFunctions_C.PrepareBufferPos)
because common usage. Tu fulfill, the interface StringFunctions_C.PrepareBufferPos
is implemented here.
addHex(long, int)
now uses StringFunctions_C.appendHex(Appendable, long, int)
.
For that the StringFormatter.Appendable_Intern
instance is necessary.
addBool(boolean, String)
, interesting for hardware simulation.
close()
again. Now this class is not Closeable,
because of too much sophisticated warnings. close() is now optional. It flushs, closes and removes
the content if a lineout
exists, else does nothing.
close()
. Only if #lineout exists and the content was flushed,
the content is deleted yet. That is always true. After close the content is able to evaluate
respectively stored furthermore in the aggregated getContent()
if no lineout is given.
See documentation to close()
.
#addHexBlock(byte[], int, int, short, short))
addHex(long, int)
with upper chars
append(char)
and flushLine(String)
, now output of the given line end is supported
if the StringFormatter(Appendable, boolean, String, int)
argument 'newlineString' is null.
add(String)
additional to add(CharSequence)
only for Java2C-translation. In Java it is equal.
append(char)
: if more as one line feed 0d 0a 0d 0a follows, it was recognized as only one line feed.
close()
,
ctor StringFormatter(Appendable, boolean, String, int)
to close the aggregated appendable.
Need for JZcmd
pos(int, int)
convertTimestampToday(long)
addReplaceLinefeed(CharSequence, CharSequence, int)
.
#strPicture(long, String, String, char)
was created in C++
private static final byte mNrofBytesInWord
private static final byte mBytesInWordBigEndian
public static final short k1
public static final short k2right
public static final short k2left
public static final short k4right
public static final short k4left
public static final short k6right
public static final short k6left
public static final short k8right
public static final short k8left
private static final java.lang.String spaces
protected final java.lang.StringBuilder buffer
protected java.lang.Appendable lineout
private final boolean bShouldLineoutClose
private char secondNewline
append(char)
.
If it is a '\r' a following '\n' does not force a newline.
If if is a '\n' a following '\r' does not force a newline.protected int pos_
protected int posLine_
private boolean bInsert
private java.lang.String sNewline
private char cDecimalSeparator
java.lang.String sDatePrefixNewer
java.text.SimpleDateFormat dateFormatNewer
java.lang.String sDatePrefixToday
java.text.SimpleDateFormat dateFormatToday
java.lang.String sDatePrefixYear
java.text.SimpleDateFormat dateFormatYear
java.lang.String sDatePrefixOlder
java.text.SimpleDateFormat dateFormatOlder
private final StringFormatter.Appendable_Intern appendable
StringFormatter.Appendable_Intern
, only internal usage.public StringFormatter()
public StringFormatter(java.lang.Appendable lineout, boolean shouldClose, java.lang.String newlineString, int defaultBufferLength)
append(char)
or append(CharSequence)
or append(CharSequence, int, int)
to this instance, the part till the '\n' will be written to the lineout and removed.
It means the internal buffer contains only the current line. This current line
can be formatted in the known kind.lineout
- Any appendable (Writer)shouldClose
- if true then closes the lineout if this is closed. If true lineout have to be instanceof Closeable.newlineString
- usual "\n", "\r\n" or "\r". If null then the newline characters or output as usual. See append(char)
.defaultBufferLength
- usual about 100..200 for the length of line. The buffer will be increased
if a longer line is necessary.public StringFormatter(int length)
length
- length of the internal StringBuffer.public StringFormatter(java.lang.String str)
str
- initial value.public StringFormatter(java.lang.StringBuilder buffer)
buffer
- The buffer.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String getContent()
public java.lang.CharSequence getBuffer()
public void setDecimalSeparator(char sep)
public StringFormatter reset()
public StringFormatter end()
public StringFormatter pos(int newPos)
public StringFormatter pos(int newPos, int minChars)
public int replaceHolder(java.lang.String name, java.lang.String value)
name
- value
- public int replace(java.lang.String search, int from, java.lang.CharSequence src)
search
- the String to replacefrom
- search from this positionsrc
- the replaces stringpublic int length()
public int getPos()
public int prepareBufferPos(int nrofChars)
prepareBufferPos
in interface StringFunctions_C.PrepareBufferPos
nrofChars
- after pos to write somewhat.public StringFormatter add(java.lang.CharSequence str)
str
- Stringpublic StringFormatter add(java.lang.String str)
add(CharSequence)
str
- Stringpublic StringFormatter addReplaceLinefeed(java.lang.CharSequence str, java.lang.CharSequence replaceLinefeed, int maxCharsArg)
str
- to insertreplaceLinefeed
- String with 4 characters, first replaces a 0x0a, second for 0x0d, third for 0x0c,4.for all other control keys.maxCharsArg
- limits length to insertpublic StringFormatter add(char ch)
public StringFormatter add(char[] str)
str
- char array. 0-chars from backward are not added.public StringFormatter insert(java.lang.String str)
str
- public StringFormatter overwrite()
public StringFormatter insert()
public boolean setInsertMode(boolean insert)
insert
- true than insert, false than overwrite.public StringFormatter addStringLine(byte[] data, int idx, int nrofBytes, java.lang.String charsetName)
data
- byte dataidx
- offsetbytes
- nr of bytescharsetName
- encoding, typical "ISO-8859-1" or "US-ASCII", using "UTF-8" more as one byte may be present 1 char.public StringFormatter addHexLine(byte[] data, int idx, int nrofBytes, short mode)
data
- byte dataidx
- offsetbytes
- nr of bytesmode
- mode see k1
to k8Right
public void addHexBlock(byte[] data, int from, int to, short bytesInLine, short mode) throws java.io.IOException
data
- from
- to
- exclusive position. 0: till end. negativ: position back from end.bytesInLine
- mode
- nr of bytes in word, | mBytesInWordBigEndian
if necessary.java.io.IOException
public StringFormatter addHexWord(byte[] data, int idx, short mode)
data
- byte dataidx
- offsetmode
- nr of bytes and big/little endian, use k4BytePerWordLittleEndian etc.private StringFormatter addHexWord_(byte[] data, int idx, short mode)
data
- byte dataidx
- offsetmode
- nr of bytes and big/little endian, use k4BytePerWordLittleEndian etc.public StringFormatter addHex(long value, int nrofDigitsArg)
value
- the valuenrofDigits
- if negativ then writes with upper casespublic StringFormatter addBool(boolean value, java.lang.String hilo)
value
- the valuehilo
- must have two characters. "hl" write left character for true value, right char for falsepublic StringFormatter addBinary(int value, java.lang.String sPicture, java.lang.String sBitCharLo, java.lang.String sBitCharHi)
value
- the valuemode
- Ones of k1BytePerWordBigEndian to k4BytePerWordLittleEndian resp. k1MSD to k8LSDsPicture
- String as picture of output. A char 1 means, output the bit. Other chars are copied in output.sBitCharLo
- Characters for lo bit at the same position like sPicturesBitCharHi
- Characters for hi bit at the same position like sPicturepublic StringFormatter addHex44(long value)
public StringFormatter addFloat(double value, int digitsBeforePoint, int digitsAfterPoint)
value
- The valuedigitsBeforePoint
- Number of digits before decimal separator.
If the number of digits of the value is less, spaces will be outputted instead.
The decimal separator will be set at the same position everytime independent of the value.digitsAfterPoint
- Number of digits after decimal separator. All digits will be shown.public static java.lang.String addHexLn(byte[] data, int length, int idxStart)
addHexLine(byte[], int, int, int)
and [addStringLine(byte[], int, int, String)
to write a special defined format. This method writes 4-digit hex values lo byte right and the ASCII-represantation
inclusive a \n char on end. It is a static method working internal with StringFormatter.public StringFormatter addDate(java.util.Date date, java.text.SimpleDateFormat format)
public StringFormatter setAt(int pos_, char ch)
public StringFormatter addint(long nr, java.lang.String sPict)
_
means a space.
picture | Example numbers | ||||
---|---|---|---|---|---|
0 | 1 | -123 | 12345 | 12345678 | |
+2221.111 |
A integer number will be shown with exactly 7 digits. A point is set between digits at the shown position.
The sign is shown everytime, as + or - The number shows like a float point number. At example the value in integer is a milli-Value, the user can read it in a comfortable format. | ||||
+___0.000 | +___0.001 | -___0.123 | +__12.2345 | +####.### | |
+3321.111 |
A integer number will be shown with a maximum of 7 digits, but a minimum of 5 digits.
The sign is shown only if it is necessary. Because leftside there is a 3 a positive sign needs no space. | ||||
_0.000 | _0.001 | -_0.123 | 12.345 | ##.### |
char | meaning |
---|---|
0 | The digit 0 in number is alway shown, also leftside or rightside. |
1 | A leftside ditit 0 in number is shown as 0, but a rightside 0 is shown as space. |
2 | A leftside or rightside digit 0 of number is shown as space, any middle digit 0 is shown as 0. |
3 | In differece to 2 , a leftside or rightside digit 0 produces no output. With the number of 3 -digits,
the maximal shown number of digits is limited. At example with a picture "3331.03 " only a maximum of 7 digits are shown,
but a minimum of 3 digits. |
value
- A value as integer, it is a long type (64 bit). Range from -9223372036854775808 to 9223372036854775807, about 10 power 19.
It is a possible style to apply float values with multiplication to show float values with a fix position of decimal point.picture
- The picture of digit.@Deprecated public StringFormatter addIntPicture(long nr, java.lang.String sPict)
addint(long, java.lang.String)
public int addFloatPicture(float src, java.lang.String pict)
nDigit, mode |
|