public final class SegmentSequence
extends java.lang.Object
implements java.lang.CharSequence
==
can be used instead of Object.equals(Object)
to test for equality,
i.e., at most one instance can exist for any given delimiter and sequence of segments.
The hash code
implementation ensures that the hash code of a segment sequence is identical to the hash code of it's string
representation.
In addition, the delimiter and the segments use string and string array pooling.
Null segments are not permitted.Modifier and Type | Class and Description |
---|---|
static class |
SegmentSequence.Builder
A builder for creating a
composed string or a SegmentSequence.Builder.toSegmentSequence() . |
protected static class |
SegmentSequence.SegmentSequencePool
A cached pool of weakly referenced segment sequences.
|
protected static class |
SegmentSequence.StringArrayPool
A specialized pool for caching string arrays.
|
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
delimiter
The delimiter for this instance.
|
protected int |
hashCode
The cached hash code for this instance.
|
protected static SegmentSequence.SegmentSequencePool |
POOL
The cached pool for the segment sequence instances.
|
protected java.lang.String[] |
segments
The segments for this instance.
|
protected static SegmentSequence.StringArrayPool |
STRING_ARRAY_POOL
The cached pool for string arrays.
|
protected java.lang.ref.WeakReference<java.lang.String> |
toString
The cached value for
toString() . |
Modifier and Type | Method and Description |
---|---|
SegmentSequence |
append(SegmentSequence segmentSequence)
Returns an instance with the given additional segments.
|
SegmentSequence |
append(java.lang.String... segments) |
SegmentSequence |
append(java.lang.String segment)
Returns an instance with the additional segment or segments, if the given segment contains '/'.
|
char |
charAt(int index)
Returns the specified character in the
string representation of the sequence. |
static SegmentSequence |
create(java.lang.String delimiter)
Returns the empty segment sequence for the given delimiter.
|
static SegmentSequence |
create(java.lang.String delimiter,
java.lang.String... segments)
Returns the segment sequence for the given segments,
splitting individual segments as necessary, i.e., if they contain the delimiter character,
or omitting empty segments in the case of the empty delimiter.
|
static SegmentSequence |
create(java.lang.String delimiter,
java.lang.String value)
Returns the segment sequence for the value, splitting it into segments as appropriate, or
null if the value is null. |
protected static SegmentSequence |
create(java.lang.String delimiter,
java.lang.String[] segments,
int count) |
java.lang.String |
delimiter()
Returns the delimiter of the segment sequence.
|
java.lang.String |
firstSegment()
Returns the first segment of the segment sequence, or
null if there are no segments. |
int |
hashCode()
Although there is an override of hash code, there is no override of
Object.equals(Object)
because the static methods for creation guarantee that there is at most one instance created for each unique segment sequence. |
java.lang.String |
lastSegment()
Returns the last segment of the segment sequence, or
null if there are no segments. |
int |
length()
Returns the number of characters in the
string representation. |
protected boolean |
matches(java.lang.String delimiter,
java.lang.String string)
Returns true if the delimiter is the same as this segment sequence's delimiter and the
string representation of this segment sequence is equal to the given string. |
static SegmentSequence.Builder |
newBuilder(java.lang.String delimiter)
Creates a new builder for the given delimiter.
|
static SegmentSequence.Builder |
newBuilder(java.lang.String delimiter,
int capacity)
Creates a new builder for the given delimiter with the given capacity of strings before the buffer needs to grow.
|
java.lang.String |
segment(int index)
Returns the segment at the given index of the segment sequence.
|
int |
segmentCount()
Returns the number of segments in the segment sequence.
|
java.lang.String[] |
segments()
Returns the segments of this segment sequence.
|
java.util.List<java.lang.String> |
segmentsList()
Returns a read-only list view of the segments.
|
protected static java.lang.String[] |
split(java.lang.String delimiter,
java.lang.String[] segments,
int length)
Splits individual segments as necessary, i.e., if they contain the delimiter character,
or omits empty segments in the case of the empty delimiter.
|
java.lang.String[] |
subSegments(int start)
Returns the segments from the given start.
|
java.lang.String[] |
subSegments(int start,
int end)
Returns the segments from the given start and before the given end.
|
java.util.List<java.lang.String> |
subSegmentsList(int start)
Returns a read-only list view of the segments from the given start.
|
java.util.List<java.lang.String> |
subSegmentsList(int start,
int end)
Returns a read-only list view of the segments from the given start and before the given end.
|
java.lang.CharSequence |
subSequence(int start,
int end)
Returns the requested subsequence in the representation of the sequence.
|
java.lang.String |
toString()
|
protected static final SegmentSequence.SegmentSequencePool POOL
protected static final SegmentSequence.StringArrayPool STRING_ARRAY_POOL
protected final int hashCode
protected final java.lang.String delimiter
protected final java.lang.String[] segments
protected java.lang.ref.WeakReference<java.lang.String> toString
toString()
.public SegmentSequence append(SegmentSequence segmentSequence)
delimiter
is different from that of the argument,
the segments of the argument will be split if they contain the delimiter.public SegmentSequence append(java.lang.String segment)
public SegmentSequence append(java.lang.String... segments)
public int length()
string
representation.length
in interface java.lang.CharSequence
public char charAt(int index)
string
representation of the sequence.charAt
in interface java.lang.CharSequence
public java.lang.CharSequence subSequence(int start, int end)
subSequence
in interface java.lang.CharSequence
public java.lang.String toString()
delimiter
-separated segments
.toString
in interface java.lang.CharSequence
toString
in class java.lang.Object
public java.lang.String delimiter()
public java.lang.String[] segments()
public java.lang.String[] subSegments(int start)
public java.lang.String[] subSegments(int start, int end)
public java.util.List<java.lang.String> segmentsList()
public java.util.List<java.lang.String> subSegmentsList(int start)
public java.util.List<java.lang.String> subSegmentsList(int start, int end)
public int segmentCount()
public java.lang.String segment(int index)
public java.lang.String lastSegment()
null
if there are no segments.public java.lang.String firstSegment()
null
if there are no segments.public int hashCode()
Object.equals(Object)
because the static methods for creation guarantee that there is at most one instance created for each unique segment sequence.hashCode
in class java.lang.Object
protected boolean matches(java.lang.String delimiter, java.lang.String string)
string representation
of this segment sequence is equal to the given string.public static SegmentSequence create(java.lang.String delimiter, java.lang.String value)
null
if the value is null.public static SegmentSequence create(java.lang.String delimiter)
public static SegmentSequence create(java.lang.String delimiter, java.lang.String... segments)
java.lang.NullPointerException
- if the segments itself or any individual segment among the segments is null
.protected static SegmentSequence create(java.lang.String delimiter, java.lang.String[] segments, int count)
protected static java.lang.String[] split(java.lang.String delimiter, java.lang.String[] segments, int length)
java.lang.NullPointerException
- if the segments itself or any individual segments among the segments is null
.public static SegmentSequence.Builder newBuilder(java.lang.String delimiter)
java.lang.NullPointerException
- if the delimiter is null.public static SegmentSequence.Builder newBuilder(java.lang.String delimiter, int capacity)
java.lang.NullPointerException
- if the delimiter is null.