Class StrReplacer
- java.lang.Object
-
- kz.greetgo.util.StrReplacer
-
public class StrReplacer extends java.lang.ObjectProvides the ability to generate large string with the possibility of using substitutional parameters. Substitutional parameters are defined within the string in curly brackets, for example: {PARAMETER_NAME}.
The parameters are substituted with their values at the time of reading the resulting string. Reading the resulting string is executed with method call
toString().Parameters values are defined with the method
prm(String, String).
-
-
Constructor Summary
Constructors Constructor Description StrReplacer()Constructs a string replacer with no characters in it and an initial capacity of 16 characters.StrReplacer(int capacity)Constructs a string replacer with no characters in it and an initial capacity specified by thecapacityargument.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StrReplaceradd(char c)Appends a character as a characterStrReplaceradd(char[] str)The same asadd(char[], int, int), but takes the array entirelyStrReplaceradd(char[] str, int offset, int len)Appends the string representation of a subarray of thechararray argument to this sequence.StrReplaceradd(int i)Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(int)StrReplaceradd(long l)Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(long)StrReplaceradd(java.lang.CharSequence s)Appends third-party string entirelyStrReplaceradd(java.lang.CharSequence s, int start, int end)Appends a part of third-party stringStrReplaceradd(java.lang.Integer i)Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(int)StrReplaceradd(java.lang.Long l)Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(long)StrReplaceradd(java.lang.StringBuffer sb)StrReplaceradd(java.lang.StringBuilder sb)StrReplaceradd(StrReplacer sr)voidappendResultTo(java.lang.StringBuilder target)Appends resulting text into specifiedStringBuildervoiddecLen(int decValue)Decreases the length of the text by the specified number of characters.java.lang.StringBuildergenerateResultStringBuilder()Generates resultingStringBuilder, where instead of the parameters their values areintgetLen()Get the lenght (number of characters) of current generated text (without substitution of values of parameters)StrReplacerprm(java.lang.String key, java.lang.String value)Sets the value of the parametervoidsetLen(int newLen)Sets the length of the character sequence.java.lang.StringtoString()Generates the resulting string, where instead of the parameters their values arestatic StrReplacerwrap(java.lang.CharSequence charSequence)Wraps a string replacer that contains the same characters as the specifiedCharSequence.
-
-
-
Constructor Detail
-
StrReplacer
public StrReplacer()
Constructs a string replacer with no characters in it and an initial capacity of 16 characters.
-
StrReplacer
public StrReplacer(int capacity)
Constructs a string replacer with no characters in it and an initial capacity specified by thecapacityargument.- Parameters:
capacity- the initial capacity.- Throws:
java.lang.NegativeArraySizeException- if thecapacityargument is less than0.
-
-
Method Detail
-
wrap
public static StrReplacer wrap(java.lang.CharSequence charSequence)
Wraps a string replacer that contains the same characters as the specifiedCharSequence. The initial capacity of the string builder is16plus the length of theCharSequenceargument.- Parameters:
charSequence- the sequence to copy.- Throws:
java.lang.NullPointerException- ifcharSequenceisnull
-
add
public StrReplacer add(java.lang.CharSequence s)
Appends third-party string entirely- Parameters:
s- third-party string to be appended- Returns:
this
-
add
public StrReplacer add(java.lang.CharSequence s, int start, int end)
Appends a part of third-party string- Parameters:
s- third-party stringstart- start of part to be appended in third-party stringend- end of part to be appended in third-party string- Returns:
this
-
add
public StrReplacer add(int i)
Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(int)- Parameters:
i- number to be appended- Returns:
this
-
add
public StrReplacer add(char c)
Appends a character as a character- Parameters:
c- a character to be appended- Returns:
this
-
add
public StrReplacer add(char[] str, int offset, int len)
Appends the string representation of a subarray of thechararray argument to this sequence.Characters of the
chararraystr, starting at indexoffset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value oflen.The overall effect is exactly as if the arguments were converted to a string by the method
String.valueOf(char[], int, int), and the characters of that string were thenappendedto this character sequence.- Parameters:
str- the characters to be appended.offset- the index of the firstcharto append.len- the number ofchars to append.- Returns:
- a reference to this object.
- Throws:
java.lang.IndexOutOfBoundsException- ifoffset < 0orlen < 0oroffset+len > str.length
-
add
public StrReplacer add(char[] str)
The same asadd(char[], int, int), but takes the array entirely- Parameters:
str- array to be appended entirely- Returns:
this
-
add
public StrReplacer add(java.lang.Integer i)
Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(int)- Parameters:
i- number to be appended- Returns:
this
-
add
public StrReplacer add(long l)
Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(long)- Parameters:
l- number to be appended- Returns:
this
-
add
public StrReplacer add(java.lang.Long l)
Appends an integer, converting it to a string in the same manner as the method doesString.valueOf(long)- Parameters:
l- number to be appended- Returns:
this
-
add
public StrReplacer add(java.lang.StringBuilder sb)
-
add
public StrReplacer add(java.lang.StringBuffer sb)
-
add
public StrReplacer add(StrReplacer sr)
-
prm
public StrReplacer prm(java.lang.String key, java.lang.String value)
Sets the value of the parameter- Parameters:
key- parameter keyvalue- parameter value- Returns:
this
-
generateResultStringBuilder
public java.lang.StringBuilder generateResultStringBuilder()
Generates resultingStringBuilder, where instead of the parameters their values are
-
appendResultTo
public void appendResultTo(java.lang.StringBuilder target)
Appends resulting text into specifiedStringBuilder- Parameters:
target- a place where result is appended
-
toString
public java.lang.String toString()
Generates the resulting string, where instead of the parameters their values are- Overrides:
toStringin classjava.lang.Object
-
getLen
public int getLen()
Get the lenght (number of characters) of current generated text (without substitution of values of parameters)- Returns:
- the lenght of current text
-
setLen
public void setLen(int newLen)
Sets the length of the character sequence. The sequence is changed to a new character sequence whose length is specified by the argument. For every nonnegative index k less thannewLength, the character at index k in the new character sequence is the same as the character at index k in the old sequence if k is less than the length of the old character sequence; otherwise, it is the null character'\u0000'.In other words, if the
newLengthargument is less than the current length, the length is changed to the specified length.If the
newLengthargument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes thenewLengthargument.The
newLengthargument must be greater than or equal to0.- Parameters:
newLen- the new length- Throws:
java.lang.IndexOutOfBoundsException- if thenewLengthargument is negative.
-
decLen
public void decLen(int decValue)
Decreases the length of the text by the specified number of characters. If resulting size is less than 0, no error occur, just the whole text is cleared- Parameters:
decValue- size by which the text is decreased
-
-