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 ClassesModifier and TypeClassDescriptionstatic final classBuilder class for constructing CharsWrappers. -
Constructor Summary
ConstructorsConstructorDescriptionCharsWrapper(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.CharsWrapper(CharSequence csq) 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.CharsWrapper(String str) 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 TypeMethodDescriptioncharcharAt(int index) clone()Creates and returns a copy of this CharsWrapper.booleancontains(char c) Checks if this CharsWrapper contains the specified character.booleancontentEquals(char[] array) Compares this CharsWrapper to an array of characters.booleanCompares this CharsWrapper to a CharSequence.booleanbooleanCompares this CharsWrapper to a CharSequence, ignoring case considerations.charget(int index) inthashCode()Calculates the hash code of this CharsWrapper.intindexOf(char c) Returns the index within this CharsWrapper of the first occurrence of the specified character.intindexOfFirst(char... ch) Returns the index within this CharsWrapper of the first occurrence of one of the specified characters.booleanisEmpty()Checks if this CharsWrapper is empty, ie if its length is zero.iterator()intlength()voidreplaceAll(char ch, char replacement) Replaces all occurences in this Wrapper of a character by another one.voidset(int index, char ch) Sets the value of a character.booleanChecks 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.toString()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, waitMethods inherited from interface java.lang.CharSequence
chars, codePointsMethods 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 useoffset- the index (in the array) of the first character to uselimit- the index +1 (in the array) of the last character to use
-
CharsWrapper
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
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 copybegin- index of the first character to copy from strend- index after the last character to copy from str
-
CharsWrapper
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
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 copybegin- index of the first character to copy from csqend- 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:
isEmptyin interfaceCharSequence- Returns:
- true if it's empty, false otherwise
-
length
public int length()- Specified by:
lengthin interfaceCharSequence
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
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 replacereplacement- 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-1if 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-1if not found.
-
equals
-
equalsIgnoreCase
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
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
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
This method copies the data so the returned CharsWrapper doesn't share its array with this CharsWrapper and is completely independant.
- Specified by:
subSequencein interfaceCharSequence
-
subView
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, inclusiveend- the end index, exclusive- Returns:
- a new CharsWrapper that is a view of a part of this CharsWrapper
-
subView
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
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
- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-
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 aswrapper.toString().hashCode(). That is, if a String and a CharsWrapper contain exactly the same characters then they will have the same hash code. -
clone
Creates and returns a copy of this CharsWrapper. The underlying char array is copied and used to create a new instance of CharsWrapper. -
iterator
-