Class CharsWrapper

java.lang.Object
com.electronwill.nightconfig.core.io.CharsWrapper
All Implemented Interfaces:
CharSequence, Cloneable, Iterable<Character>

public final class CharsWrapper extends Object implements CharSequence, Cloneable, Iterable<Character>
A simple, efficient implementation of CharSequence, designed to avoid data copying and to maximize performance.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Builder class for constructing CharsWrappers.
  • Constructor Summary

    Constructors
    Constructor
    Description
    CharsWrapper(char... chars)
    Creates a new CharsWrapper backed by the given char array.
    CharsWrapper(char[] chars, int offset, int limit)
    Creates a new CharsWrapper backed by the given char array.
    Creates a new CharsWrapper containing the same characters as the specified CharSequence.
    CharsWrapper(CharSequence csq, int begin, int end)
    Creates a new CharsWrapper containing the same characters as the specified CharSequence.
    Creates a new CharsWrapper containing the same characters as the specified String.
    CharsWrapper(String str, int begin, int end)
    Creates a new CharsWrapper containing the same characters as the specified String.
  • Method Summary

    Modifier and Type
    Method
    Description
    char
    charAt(int index)
     
    Creates and returns a copy of this CharsWrapper.
    boolean
    contains(char c)
    Checks if this CharsWrapper contains the specified character.
    boolean
    contentEquals(char[] array)
    Compares this CharsWrapper to an array of characters.
    boolean
    Compares this CharsWrapper to a CharSequence.
    boolean
     
    boolean
    Compares this CharsWrapper to a CharSequence, ignoring case considerations.
    char
    get(int index)
     
    int
    Calculates the hash code of this CharsWrapper.
    int
    indexOf(char c)
    Returns the index within this CharsWrapper of the first occurrence of the specified character.
    int
    indexOfFirst(char... ch)
    Returns the index within this CharsWrapper of the first occurrence of one of the specified characters.
    boolean
    Checks if this CharsWrapper is empty, ie if its length is zero.
     
    int
     
    void
    replaceAll(char ch, char replacement)
    Replaces all occurences in this Wrapper of a character by another one.
    void
    set(int index, char ch)
    Sets the value of a character.
    boolean
    Checks if this CharsWrapper starts with the same characters as the given CharSequence.
    subSequence(int start, int end)
    subView(int start)
    Creates a view of a part of this CharsWrapper.
    subView(int start, int end)
    Creates a view of a part of this CharsWrapper.
     
    Creates a trimmed view of this CharsWrapper, with any leading and trailing whitespace removed.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.CharSequence

    chars, codePoints

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • CharsWrapper

      public CharsWrapper(char... chars)
      Creates a new CharsWrapper backed by the given char array. Any modification to the array is reflected in the CharsWrapper and vice-versa.
      Parameters:
      chars - the char array to use
    • CharsWrapper

      public CharsWrapper(char[] chars, int offset, int limit)
      Creates a new CharsWrapper backed by the given char array. Any modification to the array is reflected in the CharsWrapper and vice-versa.
      Parameters:
      chars - the char array to use
      offset - the index (in the array) of the first character to use
      limit - the index +1 (in the array) of the last character to use
    • CharsWrapper

      public CharsWrapper(String str)
      Creates a new CharsWrapper containing the same characters as the specified String. The data is copied and the new CharsWrapper is completely independent.
      Parameters:
      str - the String to copy
    • CharsWrapper

      public CharsWrapper(String str, int begin, int end)
      Creates a new CharsWrapper containing the same characters as the specified String. The data is copied and the new CharsWrapper is completely independent.
      Parameters:
      str - the String to copy
      begin - index of the first character to copy from str
      end - index after the last character to copy from str
    • CharsWrapper

      public CharsWrapper(CharSequence csq)
      Creates a new CharsWrapper containing the same characters as the specified CharSequence. The data is copied and the new CharsWrapper is completely independent.
      Parameters:
      csq - the sequence to copy
    • CharsWrapper

      public CharsWrapper(CharSequence csq, int begin, int end)
      Creates a new CharsWrapper containing the same characters as the specified CharSequence. The data is copied and the new CharsWrapper is completely independent.
      Parameters:
      csq - the sequence to copy
      begin - index of the first character to copy from csq
      end - index after the last character to copy from csq
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Checks if this CharsWrapper is empty, ie if its length is zero.
      Specified by:
      isEmpty in interface CharSequence
      Returns:
      true if it's empty, false otherwise
    • length

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

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

      public char get(int index)
      Parameters:
      index - the character's index (the first character is at index 0)
      Returns:
      the character at the specified index
    • set

      public void set(int index, char ch)
      Sets the value of a character.
      Parameters:
      index - the character's index (the first character is at index 0)
      ch - the character value to set
    • replaceAll

      public void replaceAll(char ch, char replacement)
      Replaces all occurences in this Wrapper of a character by another one.
      Parameters:
      ch - the character to replace
      replacement - the replacement to use
    • contains

      public boolean contains(char c)
      Checks if this CharsWrapper contains the specified character.
      Parameters:
      c - the character to look for
      Returns:
      true if it contains the character, false if it does not
    • indexOf

      public int indexOf(char c)
      Returns the index within this CharsWrapper of the first occurrence of the specified character. Returns -1 if this CharsWrapper doesn't contain the character.
      Parameters:
      c - the character to look for
      Returns:
      the index of the first occurence of c, or -1 if not found.
    • indexOfFirst

      public int indexOfFirst(char... ch)
      Returns the index within this CharsWrapper of the first occurrence of one of the specified characters. Returns -1 if this CharsWrapper doesn't contain any of these characters.
      Parameters:
      ch - the characters to look for
      Returns:
      the index of the first occurence of a character of ch, or -1 if not found.
    • equals

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

      public boolean equalsIgnoreCase(CharSequence cs)
      Compares this CharsWrapper to a CharSequence, ignoring case considerations.
      Parameters:
      cs - the CharSequence to compare with this CharsWrapper
      Returns:
      true if cs isn't null and contains the same characters as this CharsWrapper, ignoring case considerations.
      See Also:
    • contentEquals

      public boolean contentEquals(CharSequence cs)
      Compares this CharsWrapper to a CharSequence.
      Parameters:
      cs - the CharSequence to compare with this CharsWrapper
      Returns:
      true if cs isn't null and contains the same characters as this CharsWrapper
      See Also:
    • contentEquals

      public boolean contentEquals(char[] array)
      Compares this CharsWrapper to an array of characters.
      Parameters:
      array - the array to compare with this CharsWrapper
      Returns:
      true if the array isn't null and contains the same characters as this CharsWrapper
    • startsWith

      public boolean startsWith(CharSequence cs)
      Checks if this CharsWrapper starts with the same characters as the given CharSequence.
      Parameters:
      cs - the sequence to compare to the beginning of this CharsWrapper
      Returns:
      true if the first characters of this wrapper are the same as the given sequence
    • subSequence

      public CharsWrapper subSequence(int start, int end)

      This method copies the data so the returned CharsWrapper doesn't share its array with this CharsWrapper and is completely independant.

      Specified by:
      subSequence in interface CharSequence
    • subView

      public CharsWrapper subView(int start, int end)
      Creates a view of a part of this CharsWrapper. Any modification to the view is reflected in the original CharsWrapper and vice-versa.
      Parameters:
      start - the start index, inclusive
      end - the end index, exclusive
      Returns:
      a new CharsWrapper that is a view of a part of this CharsWrapper
    • subView

      public CharsWrapper subView(int start)
      Creates a view of a part of this CharsWrapper. Any modification to the view is reflected in the original CharsWrapper and vice-versa.
      Parameters:
      start - the start index, inclusive
      Returns:
      a new CharsWrapper that is a view of a part of this CharsWrapper
    • trimmedView

      public CharsWrapper trimmedView()
      Creates a trimmed view of this CharsWrapper, with any leading and trailing whitespace removed. Any modification to the view is reflected in the original CharsWrapper and vice-versa.
      Returns:
      a new CharsWrapper that is a trimmed view of this CharsWrapper
      See Also:
    • toString

      public String toString()
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Calculates the hash code of this CharsWrapper. Relation to String's hash code The hash code calculated by this method is guaranteed to return the same thing as wrapper.toString().hashCode(). That is, if a String and a CharsWrapper contain exactly the same characters then they will have the same hash code.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code for the current content of this CharsWrapper
      See Also:
    • clone

      public CharsWrapper clone()
      Creates and returns a copy of this CharsWrapper. The underlying char array is copied and used to create a new instance of CharsWrapper.
      Overrides:
      clone in class Object
      Returns:
      a copy of this CharsWrapper
    • iterator

      public Iterator<Character> iterator()
      Specified by:
      iterator in interface Iterable<Character>