Class CharSequenceSubSequence

java.lang.Object
com.intellij.util.text.CharSequenceSubSequence
All Implemented Interfaces:
CharSequenceWithStringHash, CharArrayExternalizable, CharSequence

public class CharSequenceSubSequence extends Object implements CharSequence, CharArrayExternalizable, CharSequenceWithStringHash
CharSequenceSubSequence allows to save time and memory in text processing code. It avoids creation of a new char array on every subSequence(int, int) call in contrast to String.subSequence(int, int), which actually creates a new String instance every time it's invoked.

The downside of using CharSequenceSubSequence is that it keeps reference to the original sequence, which may be large. Therefore, results of text processing should always be stored as String, to allow garbage collection of the original sequence.

CharSequenceSubSequence implements `hashCode` and `equals` in such a way that it can be compared against String map keys and set elements without creating a String instance. However, CharSequenceSubSequence should not be used as a map key or set element, since it keeps reference to the original sequence and prevents its collection.

  • Constructor Details

    • CharSequenceSubSequence

      public CharSequenceSubSequence(@NotNull @NotNull CharSequence chars)
    • CharSequenceSubSequence

      public CharSequenceSubSequence(@NotNull @NotNull CharSequence chars, int start, int end)
  • Method Details

    • length

      public final int length()
      Specified by:
      length in interface CharSequence
    • charAt

      public final char charAt(int index)
      Specified by:
      charAt in interface CharSequence
    • subSequence

      @NotNull public @NotNull CharSequence subSequence(int start, int end)
      Specified by:
      subSequence in interface CharSequence
    • toString

      @NotNull public @NotNull String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • getChars

      public void getChars(int start, int end, char @NotNull [] dest, int destPos)
      Description copied from interface: CharArrayExternalizable
      Copies own character sub-sequence to the given array
      Specified by:
      getChars in interface CharArrayExternalizable
      Parameters:
      start - the index where to start taking chars from in this sequence
      end - the index where to end taking chars in this sequence
      dest - the array to put characters into
      destPos - the index where to put the characters in the dest array
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface CharSequenceWithStringHash
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object