Interface CharAppender
-
- All Superinterfaces:
java.lang.CharSequence
- All Known Implementing Classes:
DefaultCharAppender
,ElasticCharAppender
,ExpandingCharAppender
,NoopCharAppender
,WriterCharAppender
public interface CharAppender extends java.lang.CharSequence
The general interface for classes responsible for appending characters efficiently while handling whitespaces and padding characters.Calls to
appendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
andappendIgnoringWhitespaceAndPadding(char, char)
should accumulate the given character and only discard whitespaces/padding if no non-whitespace is appended:For example:
append('a'); // accumulated value is now "a"; whitespaceCount = 0; appendIgnoringWhitespace('b'); // accumulated value is now "ab"; whitespaceCount = 0; appendIgnoringWhitespace(' '); // accumulated value remains "ab"; whitespaceCount = 1; appendIgnoringWhitespace(' '); // accumulated value remains "ab"; whitespaceCount = 2; appendIgnoringWhitespace('c'); // accumulated value is now "ab c"; whitespaceCount = 0; appendIgnoringWhitespace(' '); // accumulated value remains "ab c"; whitespaceCount = 1; appendIgnoringWhitespace('d'); // accumulated value is now "ab c d"; whitespaceCount = 0; append(' '); // accumulated value is now "ab c d "; whitespaceCount = 0;
Implementation note: White spaces should be identified as any character
<= ' '
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
append(char ch)
Appends the given character.void
append(char[] ch)
Appends characters from an input arrayvoid
append(char[] ch, int from, int length)
Appends characters from an input arrayvoid
append(int ch)
Appends the given codepoint.void
append(int[] ch)
Appends codepoints from an input arrayvoid
append(java.lang.Object obj)
Appends theString
representation of a given object.void
append(java.lang.String string)
Appends characters from an inputString
void
append(java.lang.String string, int from, int to)
Appends the contents of a String to this appendervoid
appendIgnoringPadding(char ch, char padding)
Appends the given character and marks it as ignored if it is a padding character (the definition of a padding character is implementation dependent.)void
appendIgnoringWhitespace(char ch)
Appends the given character and marks it as ignored if it is a whitespace (ch <= ' '
)void
appendIgnoringWhitespaceAndPadding(char ch, char padding)
Appends the given character and marks it as ignored if it is a whitespace (ch <= ' '
) or a padding character (the definition of a padding character is implementation dependent.)char
appendUntil(char ch, CharInput input, char stop)
Appends characters from the input, until a stop character is foundchar
appendUntil(char ch, CharInput input, char stop1, char stop2)
Appends characters from the input, until a stop character is foundchar
appendUntil(char ch, CharInput input, char stop1, char stop2, char stop3)
Appends characters from the input, until a stop character is foundvoid
delete(int count)
Deletes a given number of characters from the end of the appended content.void
fill(char ch, int length)
Adds a sequence of repeated characters to the input.java.lang.String
getAndReset()
Returns the accumulated value as a String, discarding any trailing whitespace characters identified when usingappendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
char[]
getChars()
Returns the internal character array.char[]
getCharsAndReset()
Returns the accumulated characters, discarding any trailing whitespace characters identified when usingappendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
void
ignore(int count)
Ignores the given number of characters at the end of the appended content, effectively marking these as whitespace.int
indexOf(char[] charSequence, int from)
Returns first the position of a given character sequenceint
indexOf(char ch, int from)
Returns first the position of a given characterint
indexOf(java.lang.CharSequence charSequence, int from)
Returns first the position of a given character sequenceint
indexOfAny(char[] chars, int from)
Returns the first position of any given characterboolean
isEmpty()
Indicates whether this appender represents an emptyString
.int
lastIndexOf(char ch)
Returns the last index of a given character in the current appended (characters that have been marked as whitespace will be ignored)int
length()
Returns the current accumulated value length (the sum of all appended characters - whitespaceCount).void
prepend(char ch)
Prepends the current accumulated value with a charactervoid
prepend(char[] chars)
Prepends the current accumulated value a sequence of charactersvoid
prepend(char ch1, char ch2)
Prepends the current accumulated value with a couple of charactersvoid
remove(int from, int length)
Removes a section from the appended contentvoid
reset()
Clears the accumulated value and the whitespace count.void
resetWhitespaceCount()
Resets the number of whitespaces accumulated after the last non-whitespace character.java.lang.String
substring(int from, int length)
Returns a section of the appended contentvoid
updateWhitespace()
Updates the internal whitespace count of this appender to trim trailing whitespaces.int
whitespaceCount()
Returns the current number of whitespaces accumulated after the last non-whitespace character.
-
-
-
Method Detail
-
appendIgnoringWhitespace
void appendIgnoringWhitespace(char ch)
Appends the given character and marks it as ignored if it is a whitespace (ch <= ' '
)- Parameters:
ch
- character to append
-
appendIgnoringPadding
void appendIgnoringPadding(char ch, char padding)
Appends the given character and marks it as ignored if it is a padding character (the definition of a padding character is implementation dependent.)- Parameters:
ch
- character to appendpadding
- the padding character to ignore
-
appendIgnoringWhitespaceAndPadding
void appendIgnoringWhitespaceAndPadding(char ch, char padding)
Appends the given character and marks it as ignored if it is a whitespace (ch <= ' '
) or a padding character (the definition of a padding character is implementation dependent.)- Parameters:
ch
- character to appendpadding
- the padding character to ignore
-
append
void append(char ch)
Appends the given character.- Parameters:
ch
- the character to append
-
indexOf
int indexOf(char ch, int from)
Returns first the position of a given character- Parameters:
ch
- the character to look forfrom
- the starting index from where the search will begin.- Returns:
- the position of the given character in the appended content,
-1
if not found
-
indexOf
int indexOf(char[] charSequence, int from)
Returns first the position of a given character sequence- Parameters:
charSequence
- the character sequence to look forfrom
- the starting index from where the search will begin.- Returns:
- the position of the given character sequence in the appended content,
-1
if not found
-
indexOf
int indexOf(java.lang.CharSequence charSequence, int from)
Returns first the position of a given character sequence- Parameters:
charSequence
- the character sequence to look forfrom
- the starting index from where the search will begin.- Returns:
- the position of the given character sequence in the appended content,
-1
if not found
-
indexOfAny
int indexOfAny(char[] chars, int from)
Returns the first position of any given character- Parameters:
chars
- the characters to look forfrom
- the starting index from where the search will begin.- Returns:
- the position any one of the given characters in the appended content,
-1
if none found
-
substring
java.lang.String substring(int from, int length)
Returns a section of the appended content- Parameters:
from
- the starting position in the bufferlength
- the number of characters to accumulate from the given start position- Returns:
- a
String
with the section of characters accumulated by this appender.
-
remove
void remove(int from, int length)
Removes a section from the appended content- Parameters:
from
- the starting position in the buffer (inclusive)length
- the number of characters to accumulate from the given start position
-
append
void append(int ch)
Appends the given codepoint.- Parameters:
ch
- the codepoint to append
-
append
void append(java.lang.Object obj)
Appends theString
representation of a given object.- Parameters:
obj
- the object whoseString
representation will be appended.
-
length
int length()
Returns the current accumulated value length (the sum of all appended characters - whitespaceCount).- Specified by:
length
in interfacejava.lang.CharSequence
- Returns:
- the current accumulated value length (the sum of all appended characters - whitespaceCount).
-
whitespaceCount
int whitespaceCount()
Returns the current number of whitespaces accumulated after the last non-whitespace character.This is the number of whitespaces accumulated using
appendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
- Returns:
- the number of whitespaces accumulated using
appendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
-
resetWhitespaceCount
void resetWhitespaceCount()
Resets the number of whitespaces accumulated after the last non-whitespace character.This is the number of whitespaces accumulated using
appendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
A subsequent call to
whitespaceCount()
should return 0.
-
getAndReset
java.lang.String getAndReset()
Returns the accumulated value as a String, discarding any trailing whitespace characters identified when usingappendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
The internal accumulated value is discarded after invoking this method (as in
reset()
)- Returns:
- a String containing the accumulated characters without the trailing whitespaces.
-
reset
void reset()
Clears the accumulated value and the whitespace count.
-
getCharsAndReset
char[] getCharsAndReset()
Returns the accumulated characters, discarding any trailing whitespace characters identified when usingappendIgnoringWhitespace(char)
,appendIgnoringPadding(char, char)
orappendIgnoringWhitespaceAndPadding(char, char)
The internal accumulated value is discarded after invoking this method (as in
reset()
)- Returns:
- a character array containing the accumulated characters without the trailing whitespaces.
-
getChars
char[] getChars()
Returns the internal character array.- Returns:
- the internal character array.
-
fill
void fill(char ch, int length)
Adds a sequence of repeated characters to the input.- Parameters:
ch
- the character to appendlength
- the number of times the given character should be appended.
-
prepend
void prepend(char ch)
Prepends the current accumulated value with a character- Parameters:
ch
- the character to prepend in front of the current accumulated value.
-
prepend
void prepend(char ch1, char ch2)
Prepends the current accumulated value with a couple of characters- Parameters:
ch1
- the first character to prepend in front of the current accumulated value.ch2
- the second character to prepend in front of the current accumulated value.
-
prepend
void prepend(char[] chars)
Prepends the current accumulated value a sequence of characters- Parameters:
chars
- the character sequence to prepend in front of the current accumulated value.
-
updateWhitespace
void updateWhitespace()
Updates the internal whitespace count of this appender to trim trailing whitespaces.
-
appendUntil
char appendUntil(char ch, CharInput input, char stop)
Appends characters from the input, until a stop character is found- Parameters:
ch
- the first character of the input to be appended.input
- the input whose the following characters will be appendedstop
- the stop character- Returns:
- the stop character found on the input.
-
appendUntil
char appendUntil(char ch, CharInput input, char stop1, char stop2)
Appends characters from the input, until a stop character is found- Parameters:
ch
- the first character of the input to be appended.input
- the input whose the following characters will be appendedstop1
- the first stop characterstop2
- the second stop character- Returns:
- one of the stop characters found on the input.
-
appendUntil
char appendUntil(char ch, CharInput input, char stop1, char stop2, char stop3)
Appends characters from the input, until a stop character is found- Parameters:
ch
- the first character of the input to be appended.input
- the input whose the following characters will be appendedstop1
- the first stop characterstop2
- the second stop characterstop3
- the third stop character- Returns:
- one of the stop characters found on the input.
-
append
void append(char[] ch, int from, int length)
Appends characters from an input array- Parameters:
ch
- the character arrayfrom
- the position of the first character in the array to be appendedlength
- the number of characters to be appended from the given posiion.
-
append
void append(char[] ch)
Appends characters from an input array- Parameters:
ch
- the character array
-
append
void append(int[] ch)
Appends codepoints from an input array- Parameters:
ch
- the codepoint array
-
append
void append(java.lang.String string)
Appends characters from an inputString
- Parameters:
string
- the input String
-
append
void append(java.lang.String string, int from, int to)
Appends the contents of a String to this appender- Parameters:
string
- the string whose characters will be appended.from
- the index of the first character to appendto
- the index of the last character to append
-
ignore
void ignore(int count)
Ignores the given number of characters at the end of the appended content, effectively marking these as whitespace. InvokingresetWhitespaceCount()
orupdateWhitespace()
will undo this effect.- Parameters:
count
- the number of characters to ignore
-
delete
void delete(int count)
Deletes a given number of characters from the end of the appended content. Will reset the internal whitespace count if any. InvokeupdateWhitespace()
to recalculate the number of trailing whitespaces in the appended content.- Parameters:
count
- the number of characters to delete.
-
isEmpty
boolean isEmpty()
Indicates whether this appender represents an emptyString
.- Returns:
- true calling
getAndReset()
would returnnull
, otherwisefalse
.
-
lastIndexOf
int lastIndexOf(char ch)
Returns the last index of a given character in the current appended (characters that have been marked as whitespace will be ignored)- Parameters:
ch
- the character to look for- Returns:
- the last position of the given character in the appended content, or
-1
if not found.
-
-