Class CharsWrapper
- java.lang.Object
-
- com.electronwill.nightconfig.core.io.CharsWrapper
-
- All Implemented Interfaces:
java.lang.CharSequence,java.lang.Cloneable,java.lang.Iterable<java.lang.Character>
public final class CharsWrapper extends java.lang.Object implements java.lang.CharSequence, java.lang.Cloneable, java.lang.Iterable<java.lang.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 classCharsWrapper.BuilderBuilder 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.CharsWrapper(java.lang.CharSequence csq)Creates a new CharsWrapper containing the same characters as the specified CharSequence.CharsWrapper(java.lang.CharSequence csq, int begin, int end)Creates a new CharsWrapper containing the same characters as the specified CharSequence.CharsWrapper(java.lang.String str)Creates a new CharsWrapper containing the same characters as the specified String.CharsWrapper(java.lang.String str, int begin, int end)Creates a new CharsWrapper containing the same characters as the specified String.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description charcharAt(int index)CharsWrapperclone()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.booleancontentEquals(java.lang.CharSequence cs)Compares this CharsWrapper to a CharSequence.booleanequals(java.lang.Object obj)booleanequalsIgnoreCase(java.lang.CharSequence cs)Compares 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.java.util.Iterator<java.lang.Character>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.booleanstartsWith(java.lang.CharSequence cs)Checks if this CharsWrapper starts with the same characters as the given CharSequence.CharsWrappersubSequence(int start, int end)CharsWrappersubView(int start)Creates a view of a part of this CharsWrapper.CharsWrappersubView(int start, int end)Creates a view of a part of this CharsWrapper.java.lang.StringtoString()CharsWrappertrimmedView()Creates a trimmed view of this CharsWrapper, with any leading and trailing whitespace removed.
-
-
-
Constructor Detail
-
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
public CharsWrapper(java.lang.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(java.lang.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 copybegin- index of the first character to copy from strend- index after the last character to copy from str
-
CharsWrapper
public CharsWrapper(java.lang.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(java.lang.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 copybegin- index of the first character to copy from csqend- index after the last character to copy from csq
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Checks if this CharsWrapper is empty, ie if its length is zero.- Returns:
- true if it's empty, false otherwise
-
length
public int length()
- Specified by:
lengthin interfacejava.lang.CharSequence
-
charAt
public char charAt(int index)
- Specified by:
charAtin interfacejava.lang.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 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
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
equalsIgnoreCase
public boolean equalsIgnoreCase(java.lang.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:
String.equalsIgnoreCase(String)
-
contentEquals
public boolean contentEquals(java.lang.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:
String.contentEquals(CharSequence)
-
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(java.lang.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:
subSequencein interfacejava.lang.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, inclusiveend- 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:
String.trim()
-
toString
public java.lang.String toString()
- Specified by:
toStringin interfacejava.lang.CharSequence- Overrides:
toStringin classjava.lang.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 aswrapper.toString().hashCode(). That is, if a String and a CharsWrapper contain exactly the same characters then they will have the same hash code.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hash code for the current content of this CharsWrapper
- See Also:
String.hashCode()
-
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:
clonein classjava.lang.Object- Returns:
- a copy of this CharsWrapper
-
iterator
public java.util.Iterator<java.lang.Character> iterator()
- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Character>
-
-