Class ConsString

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.CharSequence

    public class ConsString
    extends java.lang.Object
    implements java.lang.CharSequence, java.io.Serializable
    This class represents a string composed of two components, each of which may be a java.lang.String or another ConsString.

    This string representation is optimized for concatenation using the "+" operator. Instead of immediately copying both components to a new character array, ConsString keeps references to the original components and only converts them to a String if either toString() is called or a certain depth level is reached.

    Note that instances of this class are only immutable if both parts are immutable, i.e. either Strings or ConsStrings that are ultimately composed of Strings.

    Both the name and the concept are borrowed from V8.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean isFlat  
      private java.lang.CharSequence left  
      private int length  
      private java.lang.CharSequence right  
      private static long serialVersionUID  
    • Constructor Summary

      Constructors 
      Constructor Description
      ConsString​(java.lang.CharSequence str1, java.lang.CharSequence str2)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char charAt​(int index)  
      private java.lang.String flatten()  
      int length()  
      java.lang.CharSequence subSequence​(int start, int end)  
      java.lang.String toString()  
      private java.lang.Object writeReplace()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.CharSequence

        chars, codePoints
    • Field Detail

      • left

        private java.lang.CharSequence left
      • right

        private java.lang.CharSequence right
      • length

        private final int length
      • isFlat

        private boolean isFlat
    • Constructor Detail

      • ConsString

        public ConsString​(java.lang.CharSequence str1,
                          java.lang.CharSequence str2)
    • Method Detail

      • writeReplace

        private java.lang.Object writeReplace()
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
      • flatten

        private java.lang.String flatten()
      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Specified by:
        subSequence in interface java.lang.CharSequence