@Test public void testIntersection() { SubRange range0To20 = new SubRange(0, 20); SubRange range3To7 = new SubRange(3, 7); SubRange range5To7 = new SubRange(5, 7); SubRange range5To10 = new SubRange(5, 10); SubRange range6To8 = new SubRange(6, 8); SubRange range8To20 = new SubRange(8, 20); SubRange range8To10 = new SubRange(8, 10); SubRange range15To20 = new SubRange(15, 20); assertThat(range5To10.intersection(range6To8), equalTo(Optional.of(range6To8))); assertThat(range5To10.intersection(range8To20), equalTo(Optional.of(range8To10))); assertThat(range5To10.intersection(range3To7), equalTo(Optional.of(range5To7))); assertThat(range5To10.intersection(range0To20), equalTo(Optional.of(range5To10))); assertThat(range5To10.intersection(range15To20), equalTo(Optional.empty())); }
public Optional<SubRange> intersection(SubRange other) { int start = Integer.max(_start, other._start); int end = Integer.min(_end, other._end); return start <= end ? Optional.of(new SubRange(start, end)) : Optional.empty(); }
SubRange implements Serializable, Comparable<SubRange> { public Optional<SubRange> intersection(SubRange other) { int start = Integer.max(_start, other._start); int end = Integer.min(_end, other._end); return start <= end ? Optional.of(new SubRange(start, end)) : Optional.empty(); } }
SubRange implements Serializable, Comparable<SubRange> { public Optional<SubRange> intersection(SubRange other) { int start = Integer.max(_start, other._start); int end = Integer.min(_end, other._end); return start <= end ? Optional.of(new SubRange(start, end)) : Optional.empty(); } SubRange(int start, int end); @JsonCreator  SubRange(@Nullable Object o); }
SubRange implements Serializable, Comparable<SubRange> { public Optional<SubRange> intersection(SubRange other) { int start = Integer.max(_start, other._start); int end = Integer.min(_end, other._end); return start <= end ? Optional.of(new SubRange(start, end)) : Optional.empty(); } SubRange(int start, int end); @JsonCreator  SubRange(@Nullable Object o); static SubRange singleton(int value); IntStream asStream(); @Override int compareTo(@Nonnull SubRange rhs); boolean contains(@Nonnull SubRange other); @Override boolean equals(Object o); int getEnd(); int getStart(); @Override int hashCode(); boolean includes(@Nullable Integer integer); boolean includes(int integer); Optional<SubRange> intersection(SubRange other); boolean isEmpty(); boolean isSingleValue(); @JsonValue String serializedForm(); @Override String toString(); }
SubRange implements Serializable, Comparable<SubRange> { public Optional<SubRange> intersection(SubRange other) { int start = Integer.max(_start, other._start); int end = Integer.min(_end, other._end); return start <= end ? Optional.of(new SubRange(start, end)) : Optional.empty(); } SubRange(int start, int end); @JsonCreator  SubRange(@Nullable Object o); static SubRange singleton(int value); IntStream asStream(); @Override int compareTo(@Nonnull SubRange rhs); boolean contains(@Nonnull SubRange other); @Override boolean equals(Object o); int getEnd(); int getStart(); @Override int hashCode(); boolean includes(@Nullable Integer integer); boolean includes(int integer); Optional<SubRange> intersection(SubRange other); boolean isEmpty(); boolean isSingleValue(); @JsonValue String serializedForm(); @Override String toString();  }