@Test void testSegmentSplitAddsUp() { final List<Long> identifiers = domainEventMessages.stream().map(de -> { final String aggregateIdentifier = de.getAggregateIdentifier(); return UUID.fromString(aggregateIdentifier).getLeastSignificantBits(); }).collect(Collectors.toList()); final long count = identifiers.stream().filter(Segment.ROOT_SEGMENT::matches).count(); assertThat(count, is((long) identifiers.size())); final Segment[] splitSegment = Segment.ROOT_SEGMENT.split(); final long splitCount1 = identifiers.stream().filter(splitSegment[0]::matches).count(); final long splitCount2 = identifiers.stream().filter(splitSegment[1]::matches).count(); assertThat(splitCount1 + splitCount2, is((long) identifiers.size())); }
public Segment[] split() { if ((mask << 1) < 0) { throw new IllegalArgumentException("Unable to split the given segmentId, as the mask exceeds the max mask size."); } Segment[] segments = new Segment[2]; int newMask = ((mask << 1) + 1); final int newSegment = segmentId + (mask == 0 ? 1 : newMask ^ mask); segments[0] = new Segment(segmentId, newMask); segments[1] = new Segment(newSegment, newMask); return segments; }
Segment implements Comparable<Segment> { public Segment[] split() { if ((mask << 1) < 0) { throw new IllegalArgumentException("Unable to split the given segmentId, as the mask exceeds the max mask size."); } Segment[] segments = new Segment[2]; int newMask = ((mask << 1) + 1); final int newSegment = segmentId + (mask == 0 ? 1 : newMask ^ mask); segments[0] = new Segment(segmentId, newMask); segments[1] = new Segment(newSegment, newMask); return segments; } }
Segment implements Comparable<Segment> { public Segment[] split() { if ((mask << 1) < 0) { throw new IllegalArgumentException("Unable to split the given segmentId, as the mask exceeds the max mask size."); } Segment[] segments = new Segment[2]; int newMask = ((mask << 1) + 1); final int newSegment = segmentId + (mask == 0 ? 1 : newMask ^ mask); segments[0] = new Segment(segmentId, newMask); segments[1] = new Segment(newSegment, newMask); return segments; } protected  Segment(int segmentId, int mask); }
Segment implements Comparable<Segment> { public Segment[] split() { if ((mask << 1) < 0) { throw new IllegalArgumentException("Unable to split the given segmentId, as the mask exceeds the max mask size."); } Segment[] segments = new Segment[2]; int newMask = ((mask << 1) + 1); final int newSegment = segmentId + (mask == 0 ? 1 : newMask ^ mask); segments[0] = new Segment(segmentId, newMask); segments[1] = new Segment(newSegment, newMask); return segments; } protected  Segment(int segmentId, int mask); static Segment[] computeSegments(int... segments); static Segment computeSegment(int segmentId, int... availableSegmentIds); static List<Segment> splitBalanced(Segment segment, int numberOfTimes); Segment mergedWith(Segment other); int mergeableSegmentId(); boolean isMergeableWith(Segment other); int getSegmentId(); int getMask(); boolean matches(int value); boolean matches(Object value); Segment[] split(); int splitSegmentId(); @Override boolean equals(Object o); @Override String toString(); @Override int hashCode(); @Override int compareTo(Segment that); }
Segment implements Comparable<Segment> { public Segment[] split() { if ((mask << 1) < 0) { throw new IllegalArgumentException("Unable to split the given segmentId, as the mask exceeds the max mask size."); } Segment[] segments = new Segment[2]; int newMask = ((mask << 1) + 1); final int newSegment = segmentId + (mask == 0 ? 1 : newMask ^ mask); segments[0] = new Segment(segmentId, newMask); segments[1] = new Segment(newSegment, newMask); return segments; } protected  Segment(int segmentId, int mask); static Segment[] computeSegments(int... segments); static Segment computeSegment(int segmentId, int... availableSegmentIds); static List<Segment> splitBalanced(Segment segment, int numberOfTimes); Segment mergedWith(Segment other); int mergeableSegmentId(); boolean isMergeableWith(Segment other); int getSegmentId(); int getMask(); boolean matches(int value); boolean matches(Object value); Segment[] split(); int splitSegmentId(); @Override boolean equals(Object o); @Override String toString(); @Override int hashCode(); @Override int compareTo(Segment that); static final Segment ROOT_SEGMENT; }