Class SegmentSequence

java.lang.Object
org.eclipse.emf.common.util.SegmentSequence
All Implemented Interfaces:
CharSequence

public final class SegmentSequence extends Object implements CharSequence
A memory efficient structure to store a sequence of delimited string segments. A segment sequence can only be created through one the create methods. Pooling ensures that == 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.
Since:
2.9
  • Field Details

    • POOL

      protected static final SegmentSequence.SegmentSequencePool POOL
      The cached pool for the segment sequence instances.
    • STRING_ARRAY_POOL

      protected static final SegmentSequence.StringArrayPool STRING_ARRAY_POOL
      The cached pool for string arrays.
    • hashCode

      protected final int hashCode
      The cached hash code for this instance.
    • delimiter

      protected final String delimiter
      The delimiter for this instance.
    • segments

      protected final String[] segments
      The segments for this instance.
    • toString

      protected WeakReference<String> toString
      The cached value for toString().
  • Method Details

    • append

      public SegmentSequence append(SegmentSequence segmentSequence)
      Returns an instance with the given additional segments. If this segment sequence's delimiter is different from that of the argument, the segments of the argument will be split if they contain the delimiter.
    • append

      public SegmentSequence append(String segment)
      Returns an instance with the additional segment or segments, if the given segment contains '/'.
    • append

      public SegmentSequence append(String... segments)
    • length

      public int length()
      Returns the number of characters in the string representation.
      Specified by:
      length in interface CharSequence
    • charAt

      public char charAt(int index)
      Returns the specified character in the string representation of the sequence.
      Specified by:
      charAt in interface CharSequence
    • subSequence

      public CharSequence subSequence(int start, int end)
      Returns the requested subsequence in the representation of the sequence.
      Specified by:
      subSequence in interface CharSequence
    • toString

      public String toString()
      Returns the string representation for this sequence, i.e., the concatenation of the delimiter-separated segments.
      Specified by:
      toString in interface CharSequence
      Overrides:
      toString in class Object
    • delimiter

      public String delimiter()
      Returns the delimiter of the segment sequence.
    • segments

      public String[] segments()
      Returns the segments of this segment sequence.
    • subSegments

      public String[] subSegments(int start)
      Returns the segments from the given start.
    • subSegments

      public String[] subSegments(int start, int end)
      Returns the segments from the given start and before the given end.
    • segmentsList

      public List<String> segmentsList()
      Returns a read-only list view of the segments.
    • subSegmentsList

      public List<String> subSegmentsList(int start)
      Returns a read-only list view of the segments from the given start.
    • subSegmentsList

      public List<String> subSegmentsList(int start, int end)
      Returns a read-only list view of the segments from the given start and before the given end.
    • segmentCount

      public int segmentCount()
      Returns the number of segments in the segment sequence.
    • segment

      public String segment(int index)
      Returns the segment at the given index of the segment sequence.
    • lastSegment

      public String lastSegment()
      Returns the last segment of the segment sequence, or null if there are no segments.
    • firstSegment

      public String firstSegment()
      Returns the first segment of the segment sequence, or null if there are no segments.
    • hashCode

      public 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.
      Overrides:
      hashCode in class Object
    • matches

      protected boolean matches(String delimiter, 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.
    • create

      public static SegmentSequence create(String delimiter, String value)
      Returns the segment sequence for the value, splitting it into segments as appropriate, or null if the value is null.
    • create

      public static SegmentSequence create(String delimiter)
      Returns the empty segment sequence for the given delimiter.
    • create

      public static SegmentSequence create(String delimiter, 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.
      Throws:
      NullPointerException - if the segments itself or any individual segment among the segments is null.
    • create

      protected static SegmentSequence create(String delimiter, String[] segments, int count)
    • split

      protected static String[] split(String delimiter, 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. If no segment contains the delimiter, or in the case of the empty delimiter, no segments are empty, the original argument is returned.
      Throws:
      NullPointerException - if the segments itself or any individual segments among the segments is null.
    • newBuilder

      public static SegmentSequence.Builder newBuilder(String delimiter)
      Creates a new builder for the given delimiter. The delimiter may be the empty string.
      Throws:
      NullPointerException - if the delimiter is null.
    • newBuilder

      public static SegmentSequence.Builder newBuilder(String delimiter, int capacity)
      Creates a new builder for the given delimiter with the given capacity of strings before the buffer needs to grow. The delimiter may be the empty string.
      Throws:
      NullPointerException - if the delimiter is null.