Package net.morimekta.util
Class CharSlice
- java.lang.Object
-
- net.morimekta.util.CharSlice
-
- All Implemented Interfaces:
java.lang.CharSequence
,java.lang.Comparable<CharSlice>
,Stringable
- Direct Known Subclasses:
JsonToken
public class CharSlice extends java.lang.Object implements java.lang.Comparable<CharSlice>, Stringable, java.lang.CharSequence
A slice of a byte array. Note that all the helper methods assume UTF_8 encoding.
-
-
Constructor Summary
Constructors Constructor Description CharSlice(char[] fb, int off, int len)
Create a slice instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
asString()
Get the whole slice as a string.char
charAt(int i)
Get character at slice relative position.int
compareTo(CharSlice o)
Compare slice with other slice.boolean
contains(char a)
Checks if a single byte can be found in the slice.boolean
contains(char[] a)
Checks if the byte array is contained in the slice.boolean
containsAny(char... a)
Checks if any of the provided bytes is contained in the slice.boolean
equals(java.lang.Object o)
int
hashCode()
int
length()
Get the total length of the slice.int
offset()
Get the offset of the buffer.double
parseDouble()
Get the whole slice as a real number.long
parseInteger()
Get the whole slice as a simple integer.boolean
strEquals(char[] a)
Checks if the slice is equal to given byte array.boolean
strEquals(char[] a, int aOff, int aLen)
Checks if the slice is equal to a portion of a given byte array.java.lang.CharSequence
subSequence(int i, int i1)
CharSlice
substring(int start, int end)
Create a substring slice based on the current slice.java.lang.String
toString()
-
-
-
Constructor Detail
-
CharSlice
public CharSlice(char[] fb, int off, int len)
Create a slice instance. The slice is only meant to be internal state immutable, and not representing an immutable byte content.- Parameters:
fb
- The buffer to wrap.off
- The start offset to wrap.len
- The length to represent.
-
-
Method Detail
-
offset
public final int offset()
Get the offset of the buffer.- Returns:
- The slice offset.
-
length
public final int length()
Get the total length of the slice.- Specified by:
length
in interfacejava.lang.CharSequence
- Returns:
- The slice length.
-
subSequence
public java.lang.CharSequence subSequence(int i, int i1)
- Specified by:
subSequence
in interfacejava.lang.CharSequence
-
asString
public final java.lang.String asString()
Get the whole slice as a string.- Specified by:
asString
in interfaceStringable
- Returns:
- Slice decoded as UTF_8 string.
-
substring
public final CharSlice substring(int start, int end)
Create a substring slice based on the current slice.- Parameters:
start
- The internal start position, relative to the slice's offset.end
- The internal end position, relative to the slice's offset. If end is negative, then it is relative to the slice's end position.- Returns:
- The substring slice.
-
charAt
public final char charAt(int i)
Get character at slice relative position.- Specified by:
charAt
in interfacejava.lang.CharSequence
- Parameters:
i
- The position to get. If negative is relative to the slice's end position.- Returns:
- The char at given position.
-
parseInteger
public final long parseInteger()
Get the whole slice as a simple integer.- Returns:
- Integer long value.
-
parseDouble
public final double parseDouble()
Get the whole slice as a real number.- Returns:
- Real double value.
-
strEquals
public final boolean strEquals(char[] a)
Checks if the slice is equal to given byte array.- Parameters:
a
- The array to compare with.- Returns:
- True if equal.
-
strEquals
public final boolean strEquals(char[] a, int aOff, int aLen)
Checks if the slice is equal to a portion of a given byte array.- Parameters:
a
- The array to compare with.aOff
- The offset to compare with in the array.aLen
- The length to compare with.- Returns:
- True if equal.
-
containsAny
public final boolean containsAny(char... a)
Checks if any of the provided bytes is contained in the slice.- Parameters:
a
- Bytes to find in the slice.- Returns:
- True if any of the bytes were found.
-
contains
public final boolean contains(char[] a)
Checks if the byte array is contained in the slice.- Parameters:
a
- The byte array to find.- Returns:
- True if the byte array was found.
-
contains
public final boolean contains(char a)
Checks if a single byte can be found in the slice.- Parameters:
a
- The byte to find.- Returns:
- True of the byte was found.
-
toString
@Nonnull public java.lang.String toString()
- Specified by:
toString
in interfacejava.lang.CharSequence
- Overrides:
toString
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
compareTo
public int compareTo(CharSlice o)
Compare slice with other slice. Slice ordering: - Firstly ordered by start (offset) position. - Secondly ordered by reverse length (longest slice first). Result is undefined of the two slices point to different byte buffers.- Specified by:
compareTo
in interfacejava.lang.Comparable<CharSlice>
- Parameters:
o
- The other slice.- Returns:
- Compared value.
-
-