RangeTokens { public static Iterable<Range<Value>> convertToRange(RangeToken token) { List<Range<Value>> ranges = Lists.newArrayListWithCapacity(1); if(token.getOperator() == Operator.EQUALS || token.getOperator() == null) { ranges.add(Range.singleton(token.getValues()[0])); } else if(token.getOperator() == Operator.NOT_EQUALS) { ranges.add(Range.lessThan(token.getValues()[0])); ranges.add(Range.greaterThan(token.getValues()[0])); } else if(token.getOperator() == Operator.GREATER_THAN) { ranges.add(Range.greaterThan(token.getValues()[0])); } else if(token.getOperator() == Operator.GREATER_THAN_OR_EQUALS) { ranges.add(Range.atLeast(token.getValues()[0])); } else if(token.getOperator() == Operator.LESS_THAN) { ranges.add(Range.lessThan(token.getValues()[0])); } else if(token.getOperator() == Operator.LESS_THAN_OR_EQUALS) { ranges.add(Range.atMost(token.getValues()[0])); } else if(token.getOperator() == Operator.BETWEEN) { Value a = token.getValues()[0]; Value b = token.getValues()[1]; if(a == Value.NEGATIVE_INFINITY && b == Value.POSITIVE_INFINITY) { ranges.add(Range.<Value> all()); } else if(token.getValues().length == 3) { ranges.add(Range.open(a, b)); } else if(token.getValues().length == 4) { ranges.add(Range.closed(a, b)); } else if(token.getValues().length == 5) { ranges.add(Range.openClosed(a, b)); } else { ranges.add(Range.closedOpen(a, b)); } } else if(token.getOperator() == Operator.REGEX || token.getOperator() == Operator.NOT_REGEX) { ranges.add(Range.<Value> all()); } else { throw new UnsupportedOperationException(); } return ranges; }  static Iterable<Range<Value>> convertToRange(RangeToken token);  }
@Test public void testConvertEqualRangeToken() { Text key = TestData.getText(); Operator operator = Operator.EQUALS; Value value = TestData.getValue(); RangeToken token = RangeToken.forReading(key, operator, value); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.singleton(value)); }
@Test public void testConvertWriteRangeToken() { Text key = TestData.getText(); Value value = TestData.getValue(); RangeToken token = RangeToken.forWriting(key, value); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.singleton(value)); }
@Test public void testConvertNotEqualRangeToken() { Text key = TestData.getText(); Operator operator = Operator.NOT_EQUALS; Value value = TestData.getValue(); RangeToken token = RangeToken.forReading(key, operator, value); Iterable<Range<Value>> ranges = RangeTokens.convertToRange(token); Assert.assertEquals(Lists.newArrayList(Range.lessThan(value), Range.greaterThan(value)), ranges); }
@Test public void testConvertGreaterThanRangeToken() { Text key = TestData.getText(); Operator operator = Operator.GREATER_THAN; Value value = TestData.getValue(); RangeToken token = RangeToken.forReading(key, operator, value); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.greaterThan(value)); }
@Test public void testConvertGreaterThanOrEqualsRangeToken() { Text key = TestData.getText(); Operator operator = Operator.GREATER_THAN_OR_EQUALS; Value value = TestData.getValue(); RangeToken token = RangeToken.forReading(key, operator, value); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.atLeast(value)); }
@Test public void testConvertLessThanRangeToken() { Text key = TestData.getText(); Operator operator = Operator.LESS_THAN; Value value = TestData.getValue(); RangeToken token = RangeToken.forReading(key, operator, value); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.lessThan(value)); }
@Test public void testConvertLessThanOrEqualsRangeToken() { Text key = TestData.getText(); Operator operator = Operator.LESS_THAN_OR_EQUALS; Value value = TestData.getValue(); RangeToken token = RangeToken.forReading(key, operator, value); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.atMost(value)); }
@Test public void testConvertBetweenRangeToken() { Text key = TestData.getText(); Operator operator = Operator.BETWEEN; Value value1 = TestData.getValue(); Value value2 = null; while (value2 == null || value1.equals(value2)) { value2 = TestData.getValue(); } RangeToken token = RangeToken.forReading(key, operator, value1.compareTo(value2) < 0 ? value1 : value2, value1.compareTo(value2) > 0 ? value1 : value2); Range<Value> range = Iterables .getOnlyElement(RangeTokens.convertToRange(token)); Assert.assertEquals(range, Range.closedOpen(value1.compareTo(value2) < 0 ? value1 : value2, value1.compareTo(value2) > 0 ? value1 : value2)); }