public static Range lessThan(Type type, Object high) { return new Range(Marker.lowerUnbounded(type), Marker.below(type, high)); }
public static Range range(Type type, Object low, boolean lowInclusive, Object high, boolean highInclusive) { Marker lowMarker = lowInclusive ? Marker.exactly(type, low) : Marker.above(type, low); Marker highMarker = highInclusive ? Marker.exactly(type, high) : Marker.below(type, high); return new Range(lowMarker, highMarker); }
@Setup(Level.Iteration) public void init() { ranges = new ArrayList<>(); int factor = 0; for (int i = 0; i < 10000; i++) { long from = ThreadLocalRandom.current().nextLong(100) + factor * 100; long to = ThreadLocalRandom.current().nextLong(100) + (factor + 1) * 100; factor++; ranges.add(new Range(Marker.above(BIGINT, from), Marker.below(BIGINT, to))); } } }
@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)); } } }
@Test public void testTypes() { assertEquals(Marker.lowerUnbounded(BIGINT).getType(), BIGINT); assertEquals(Marker.below(BIGINT, 1L).getType(), BIGINT); assertEquals(Marker.exactly(BIGINT, 1L).getType(), BIGINT); assertEquals(Marker.above(BIGINT, 1L).getType(), BIGINT); assertEquals(Marker.upperUnbounded(BIGINT).getType(), BIGINT); }
assertEquals(marker, mapper.readValue(mapper.writeValueAsString(marker), Marker.class)); marker = Marker.below(DOUBLE, 0.123); assertEquals(marker, mapper.readValue(mapper.writeValueAsString(marker), Marker.class));
@Test public void testUnbounded() { assertTrue(Marker.lowerUnbounded(BIGINT).isLowerUnbounded()); assertFalse(Marker.lowerUnbounded(BIGINT).isUpperUnbounded()); assertTrue(Marker.upperUnbounded(BIGINT).isUpperUnbounded()); assertFalse(Marker.upperUnbounded(BIGINT).isLowerUnbounded()); assertFalse(Marker.below(BIGINT, 1L).isLowerUnbounded()); assertFalse(Marker.below(BIGINT, 1L).isUpperUnbounded()); assertFalse(Marker.exactly(BIGINT, 1L).isLowerUnbounded()); assertFalse(Marker.exactly(BIGINT, 1L).isUpperUnbounded()); assertFalse(Marker.above(BIGINT, 1L).isLowerUnbounded()); assertFalse(Marker.above(BIGINT, 1L).isUpperUnbounded()); }
@Test public void testLessThanRange() { Range range = Range.lessThan(BIGINT, 1L); assertEquals(range.getLow(), Marker.lowerUnbounded(BIGINT)); assertEquals(range.getHigh(), Marker.below(BIGINT, 1L)); assertFalse(range.isSingleValue()); assertFalse(range.isAll()); assertEquals(range.getType(), BIGINT); assertTrue(range.includes(Marker.lowerUnbounded(BIGINT))); assertFalse(range.includes(Marker.exactly(BIGINT, 1L))); assertTrue(range.includes(Marker.exactly(BIGINT, 0L))); assertFalse(range.includes(Marker.upperUnbounded(BIGINT))); }
@Test public void testAllRange() { Range range = Range.all(BIGINT); assertEquals(range.getLow(), Marker.lowerUnbounded(BIGINT)); assertEquals(range.getHigh(), Marker.upperUnbounded(BIGINT)); assertFalse(range.isSingleValue()); assertTrue(range.isAll()); assertEquals(range.getType(), BIGINT); assertTrue(range.includes(Marker.lowerUnbounded(BIGINT))); assertTrue(range.includes(Marker.below(BIGINT, 1L))); assertTrue(range.includes(Marker.exactly(BIGINT, 1L))); assertTrue(range.includes(Marker.above(BIGINT, 1L))); assertTrue(range.includes(Marker.upperUnbounded(BIGINT))); }
.put(Marker.lowerUnbounded(BIGINT), -1000) .put(Marker.above(BIGINT, 0L), -100) .put(Marker.below(BIGINT, 1L), -1) .put(Marker.exactly(BIGINT, 1L), 0) .put(Marker.above(BIGINT, 1L), 1) .put(Marker.below(BIGINT, 2L), 100) .put(Marker.upperUnbounded(BIGINT), 1000) .build(); assertEquals(Marker.below(BIGINT, 1L).greaterAdjacent(), Marker.exactly(BIGINT, 1L)); assertEquals(Marker.exactly(BIGINT, 1L).greaterAdjacent(), Marker.above(BIGINT, 1L)); assertEquals(Marker.above(BIGINT, 1L).lesserAdjacent(), Marker.exactly(BIGINT, 1L)); assertEquals(Marker.exactly(BIGINT, 1L).lesserAdjacent(), Marker.below(BIGINT, 1L)); Marker.below(BIGINT, 1L).lesserAdjacent(); fail();
public static Range lessThan(Type type, Object high) { return new Range(Marker.lowerUnbounded(type), Marker.below(type, high)); }
public static Range range(Type type, Object low, boolean lowInclusive, Object high, boolean highInclusive) { Marker lowMarker = lowInclusive ? Marker.exactly(type, low) : Marker.above(type, low); Marker highMarker = highInclusive ? Marker.exactly(type, high) : Marker.below(type, high); return new Range(lowMarker, highMarker); }