public static Marker max(Marker marker1, Marker marker2) { return marker1.compareTo(marker2) >= 0 ? marker1 : marker2; }
public static Marker min(Marker marker1, Marker marker2) { return marker1.compareTo(marker2) <= 0 ? marker1 : marker2; }
@JsonCreator public Range( @JsonProperty("low") Marker low, @JsonProperty("high") Marker high) { requireNonNull(low, "value is null"); requireNonNull(high, "value is null"); if (!low.getType().equals(high.getType())) { throw new IllegalArgumentException(String.format("Marker types do not match: %s vs %s", low.getType(), high.getType())); } if (low.getBound() == Marker.Bound.BELOW) { throw new IllegalArgumentException("low bound must be EXACTLY or ABOVE"); } if (high.getBound() == Marker.Bound.ABOVE) { throw new IllegalArgumentException("high bound must be EXACTLY or BELOW"); } if (low.compareTo(high) > 0) { throw new IllegalArgumentException("low must be less than or equal to high"); } this.low = low; this.high = high; }
@Override public SortedRangeSet intersect(ValueSet other) { SortedRangeSet otherRangeSet = checkCompatibility(other); Builder builder = new Builder(type); Iterator<Range> iterator1 = getOrderedRanges().iterator(); Iterator<Range> iterator2 = otherRangeSet.getOrderedRanges().iterator(); if (iterator1.hasNext() && iterator2.hasNext()) { Range range1 = iterator1.next(); Range range2 = iterator2.next(); while (true) { if (range1.overlaps(range2)) { builder.add(range1.intersect(range2)); } if (range1.getHigh().compareTo(range2.getHigh()) <= 0) { if (!iterator1.hasNext()) { break; } range1 = iterator1.next(); } else { if (!iterator2.hasNext()) { break; } range2 = iterator2.next(); } } } return builder.build(); }
@Test public void testComparisons() { ImmutableList<Marker> markers = ImmutableList.of( Marker.lowerUnbounded(BIGINT), Marker.above(BIGINT, 0L), Marker.below(BIGINT, 1L), Marker.exactly(BIGINT, 1L), Marker.above(BIGINT, 1L), Marker.below(BIGINT, 2L), Marker.upperUnbounded(BIGINT)); assertTrue(Ordering.natural().isStrictlyOrdered(markers)); // Compare every marker with every other marker // Since the markers are strictly ordered, the value of the comparisons should be equivalent to the comparisons // of their indexes. for (int i = 0; i < markers.size(); i++) { for (int j = 0; j < markers.size(); j++) { assertTrue(markers.get(i).compareTo(markers.get(j)) == Integer.compare(i, j)); } } }
public static Marker min(Marker marker1, Marker marker2) { return marker1.compareTo(marker2) <= 0 ? marker1 : marker2; }
public static Marker max(Marker marker1, Marker marker2) { return marker1.compareTo(marker2) >= 0 ? marker1 : marker2; }
@JsonCreator public Range( @JsonProperty("low") Marker low, @JsonProperty("high") Marker high) { requireNonNull(low, "value is null"); requireNonNull(high, "value is null"); if (!low.getType().equals(high.getType())) { throw new IllegalArgumentException(String.format("Marker types do not match: %s vs %s", low.getType(), high.getType())); } if (low.getBound() == Marker.Bound.BELOW) { throw new IllegalArgumentException("low bound must be EXACTLY or ABOVE"); } if (high.getBound() == Marker.Bound.ABOVE) { throw new IllegalArgumentException("high bound must be EXACTLY or BELOW"); } if (low.compareTo(high) > 0) { throw new IllegalArgumentException("low must be less than or equal to high"); } this.low = low; this.high = high; }
@Override public SortedRangeSet intersect(ValueSet other) { SortedRangeSet otherRangeSet = checkCompatibility(other); Builder builder = new Builder(type); Iterator<Range> iterator1 = getOrderedRanges().iterator(); Iterator<Range> iterator2 = otherRangeSet.getOrderedRanges().iterator(); if (iterator1.hasNext() && iterator2.hasNext()) { Range range1 = iterator1.next(); Range range2 = iterator2.next(); while (true) { if (range1.overlaps(range2)) { builder.add(range1.intersect(range2)); } if (range1.getHigh().compareTo(range2.getHigh()) <= 0) { if (!iterator1.hasNext()) { break; } range1 = iterator1.next(); } else { if (!iterator2.hasNext()) { break; } range2 = iterator2.next(); } } } return builder.build(); }