Class Segment

java.lang.Object
org.axonframework.eventhandling.Segment
All Implemented Interfaces:
Comparable<Segment>

public class Segment extends Object implements Comparable<Segment>
A representation of a segment and corresponding mask with various capabilities.

Definition

A Segment is a fraction of the total population of events. The 'mask' is a bitmask to be applied to an identifier, resulting in the segmentId of the Segment.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Segment
    Represents the Segment that matches against all input, but can be split to start processing elements in parallel.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Segment(int segmentId, int mask)
    Construct a new Segment instance with given segmentId and mask
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    static Segment
    computeSegment(int segmentId, int... availableSegmentIds)
    Creates a Segment instance for the given segmentId based on the given availableSegmentsIds.
    static Segment[]
    computeSegments(int... segments)
    Compute the Segment's from a given list of segmentId's.
    boolean
     
    int
    Getter for the segment mask.
    int
    Getter for the segment identifier.
    int
     
    boolean
    Indicates whether this segment can be merged with the given other segment.
    boolean
    matches(int value)
    Returns true when the mask applied to the given value, matches the segment id.
    boolean
    matches(Object value)
    Indicates whether the given value matches this segment.
    int
    Returns the segmentId of the segment this one can be merged with
    Calculates the Segment that represents the merger of this segment with the given other segment.
    Returns an array with two segments with a corresponding mask.

    The first entry contains the original segmentId, with the newly calculated mask.
    static List<Segment>
    splitBalanced(Segment segment, int numberOfTimes)
    Split a given Segment n-times in round robin fashion.
    int
    Returns the segmentId of the counterpart of this segment, if this segment were to be split.
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • ROOT_SEGMENT

      public static final Segment ROOT_SEGMENT
      Represents the Segment that matches against all input, but can be split to start processing elements in parallel.
  • Constructor Details

    • Segment

      protected Segment(int segmentId, int mask)
      Construct a new Segment instance with given segmentId and mask
      Parameters:
      segmentId - The id of the segment
      mask - The mask of the segment
  • Method Details

    • computeSegments

      public static Segment[] computeSegments(int... segments)
      Compute the Segment's from a given list of segmentId's.
      Parameters:
      segments - The segment id's for which to compute Segments.
      Returns:
      an array of computed Segment
    • computeSegment

      public static Segment computeSegment(int segmentId, int... availableSegmentIds)
      Creates a Segment instance for the given segmentId based on the given availableSegmentsIds.
      Parameters:
      segmentId - The Id of the segment to return
      availableSegmentIds - The available segment Ids, to base the mask of the segment on
      Returns:
      the Segment instance representing for the given id
    • splitBalanced

      public static List<Segment> splitBalanced(Segment segment, int numberOfTimes)
      Split a given Segment n-times in round robin fashion.
      Parameters:
      segment - The Segment to split.
      numberOfTimes - The number of times to split it.
      Returns:
      a collection of Segment's.
    • mergedWith

      public Segment mergedWith(Segment other)
      Calculates the Segment that represents the merger of this segment with the given other segment.
      Parameters:
      other - the segment to merge this one with
      Returns:
      The Segment representing the merged segments
    • mergeableSegmentId

      public int mergeableSegmentId()
      Returns the segmentId of the segment this one can be merged with
      Returns:
      the segmentId of the segment this one can be merged with
    • isMergeableWith

      public boolean isMergeableWith(Segment other)
      Indicates whether this segment can be merged with the given other segment.

      Two segments can be merged when their mask is identical, and the only difference in SegmentID is in the first 1-bit of their mask.

      Parameters:
      other - the Segment to verify mergeability for
      Returns:
      true if the segments can be merged, otherwise false
    • getSegmentId

      public int getSegmentId()
      Getter for the segment identifier.
      Returns:
      the Segment identifier.
    • getMask

      public int getMask()
      Getter for the segment mask.
      Returns:
      the Segment mask.
    • matches

      public boolean matches(int value)
      Returns true when the mask applied to the given value, matches the segment id.
      Parameters:
      value - The value to be tested.
      Returns:
      true when matching this segment.
    • matches

      public boolean matches(Object value)
      Indicates whether the given value matches this segment. A value matches when the hashCode of a value, after applying this segments mask, equals to this segment ID.
      Parameters:
      value - The value to verify against.
      Returns:
      true if the given value matches this segment, otherwise false
    • split

      public Segment[] split()
      Returns an array with two segments with a corresponding mask.

      The first entry contains the original segmentId, with the newly calculated mask. (Simple left shift, adding a '1' as LSB). The 2nd entry is a new segmentId with the same derived mask.

      Callers must ensure that either the two returned Segments are used, or the instance from which they are derived, but not both.

      Returns:
      an array of two Segment's.
    • splitSegmentId

      public int splitSegmentId()
      Returns the segmentId of the counterpart of this segment, if this segment were to be split.
      Returns:
      the segmentId of the counterpart of this segment
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Segment that)
      Specified by:
      compareTo in interface Comparable<Segment>