Class StrReplacer


  • public class StrReplacer
    extends java.lang.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 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 the capacity argument.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      StrReplacer add​(char c)
      Appends a character as a character
      StrReplacer add​(char[] str)
      The same as add(char[], int, int), but takes the array entirely
      StrReplacer add​(char[] str, int offset, int len)
      Appends the string representation of a subarray of the char array argument to this sequence.
      StrReplacer add​(int i)
      Appends an integer, converting it to a string in the same manner as the method does String.valueOf(int)
      StrReplacer add​(long l)
      Appends an integer, converting it to a string in the same manner as the method does String.valueOf(long)
      StrReplacer add​(java.lang.CharSequence s)
      Appends third-party string entirely
      StrReplacer add​(java.lang.CharSequence s, int start, int end)
      Appends a part of third-party string
      StrReplacer add​(java.lang.Integer i)
      Appends an integer, converting it to a string in the same manner as the method does String.valueOf(int)
      StrReplacer add​(java.lang.Long l)
      Appends an integer, converting it to a string in the same manner as the method does String.valueOf(long)
      StrReplacer add​(java.lang.StringBuffer sb)  
      StrReplacer add​(java.lang.StringBuilder sb)  
      StrReplacer add​(StrReplacer sr)  
      void appendResultTo​(java.lang.StringBuilder target)
      Appends resulting text into specified StringBuilder
      void decLen​(int decValue)
      Decreases the length of the text by the specified number of characters.
      java.lang.StringBuilder generateResultStringBuilder()
      Generates resulting StringBuilder, where instead of the parameters their values are
      int getLen()
      Get the lenght (number of characters) of current generated text (without substitution of values of parameters)
      StrReplacer prm​(java.lang.String key, java.lang.String value)
      Sets the value of the parameter
      void setLen​(int newLen)
      Sets the length of the character sequence.
      java.lang.String toString()
      Generates the resulting string, where instead of the parameters their values are
      static StrReplacer wrap​(java.lang.CharSequence charSequence)
      Wraps a string replacer that contains the same characters as the specified CharSequence.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • 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 the capacity argument.
        Parameters:
        capacity - the initial capacity.
        Throws:
        java.lang.NegativeArraySizeException - if the capacity argument is less than 0.
    • Method Detail

      • wrap

        public static StrReplacer wrap​(java.lang.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:
        java.lang.NullPointerException - if charSequence is null
      • 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 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:
        java.lang.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​(java.lang.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​(java.lang.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​(java.lang.StringBuilder sb)
      • add

        public StrReplacer add​(java.lang.StringBuffer sb)
      • prm

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

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

        public void appendResultTo​(java.lang.StringBuilder target)
        Appends resulting text into specified StringBuilder
        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:
        toString in class java.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 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:
        java.lang.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