Class StrReplacer

java.lang.Object
kz.greetgo.util.StrReplacer

public class StrReplacer extends Object

Provides 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 Details

    • 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 the capacity argument.
      Parameters:
      capacity - the initial capacity.
      Throws:
      NegativeArraySizeException - if the capacity argument is less than 0.
  • Method Details

    • wrap

      public static StrReplacer wrap(CharSequence charSequence)
      Wraps a string replacer that contains the same characters as the specified CharSequence. The initial capacity of the string builder is 16 plus the length of the CharSequence argument.
      Parameters:
      charSequence - the sequence to copy.
      Throws:
      NullPointerException - if charSequence is null
    • add

      public StrReplacer add(CharSequence s)
      Appends third-party string entirely
      Parameters:
      s - third-party string to be appended
      Returns:
      this
    • add

      public StrReplacer add(CharSequence s, int start, int end)
      Appends a part of third-party string
      Parameters:
      s - third-party string
      start - start of part to be appended in third-party string
      end - 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 does String.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 the char array argument to this sequence.

      Characters of the char array str, starting at index offset, are appended, in order, to the contents of this sequence. The length of this sequence increases by the value of len.

      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 then appended to this character sequence.

      Parameters:
      str - the characters to be appended.
      offset - the index of the first char to append.
      len - the number of chars to append.
      Returns:
      a reference to this object.
      Throws:
      IndexOutOfBoundsException - if offset < 0 or len < 0 or offset+len > str.length
    • add

      public StrReplacer add(char[] str)
      The same as add(char[], int, int), but takes the array entirely
      Parameters:
      str - array to be appended entirely
      Returns:
      this
    • add

      public StrReplacer add(Integer i)
      Appends an integer, converting it to a string in the same manner as the method does String.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 does String.valueOf(long)
      Parameters:
      l - 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 does String.valueOf(long)
      Parameters:
      l - number to be appended
      Returns:
      this
    • add

      public StrReplacer add(StringBuilder sb)
    • add

      public StrReplacer add(StringBuffer sb)
    • add

      public StrReplacer add(StrReplacer sr)
    • prm

      public StrReplacer prm(String key, String value)
      Sets the value of the parameter
      Parameters:
      key - parameter key
      value - parameter value
      Returns:
      this
    • generateResultStringBuilder

      public StringBuilder generateResultStringBuilder()
      Generates resulting StringBuilder, where instead of the parameters their values are
    • appendResultTo

      public void appendResultTo(StringBuilder target)
      Appends resulting text into specified StringBuilder
      Parameters:
      target - a place where result is appended
    • toString

      public String toString()
      Generates the resulting string, where instead of the parameters their values are
      Overrides:
      toString in class 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 than newLength, 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 newLength argument is less than the current length, the length is changed to the specified length.

      If the newLength argument is greater than or equal to the current length, sufficient null characters ('\u0000') are appended so that length becomes the newLength argument.

      The newLength argument must be greater than or equal to 0.

      Parameters:
      newLen - the new length
      Throws:
      IndexOutOfBoundsException - if the newLength argument 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