public static <T extends Comparable<T>> Range<T> parse(String value, Function<String, T> parser) { Validate.notNull(parser, "Parser is required"); if (StringUtils.isBlank(value) || StringUtils.equals(SEPARATOR, value)) { return Ranges.all(); } else if (!StringUtils.contains(value, SEPARATOR)) { T element = parser.apply(value); return Ranges.atMost(element); } else { String lower = StringUtils.substringBefore(value, SEPARATOR); String upper = StringUtils.substringAfter(value, SEPARATOR); if (StringUtils.isBlank(lower)) { // ..n Pair<T, BoundType> boundary = parseUpperBoundary(upper, parser); return Ranges.upTo(boundary.getLeft(), boundary.getRight()); } else if (StringUtils.isBlank(upper)) { // n.. Pair<T, BoundType> boundary = parseLowerBoundary(lower, parser); return Ranges.downTo(boundary.getLeft(), boundary.getRight()); } else { // n..m Pair<T, BoundType> down = parseLowerBoundary(lower, parser); Pair<T, BoundType> up = parseUpperBoundary(upper, parser); return Ranges.range(down.getLeft(), down.getRight(), up.getLeft(), up.getRight()); } } }
@Test public void isConnected() { assertThat(Ranges.closed(3, 5).isConnected(Ranges.open(5, 10)), is(true)); assertThat(Ranges.closed(0, 9).isConnected(Ranges.closed(3, 4)), is(true)); assertThat(Ranges.closed(0, 5).isConnected(Ranges.closed(3, 9)), is(true)); assertThat(Ranges.open(3, 5).isConnected(Ranges.open(5, 10)), is(false)); assertThat(Ranges.closed(1, 5).isConnected(Ranges.closed(6, 10)), is(false)); }
protected final ContiguousSet<Integer> checkedCreate(SortedSet<Integer> elementsSet) { List<Integer> elements = newArrayList(elementsSet); /* * A ContiguousSet can't have holes. If a test demands a hole, it should be changed so that it * doesn't need one, or it should be suppressed for ContiguousSet. */ for (int i = 0; i < elements.size() - 1; i++) { assertEquals(elements.get(i) + 1, (int) elements.get(i + 1)); } Range<Integer> range = (elements.isEmpty()) ? Ranges.closedOpen(0, 0) : Ranges.encloseAll(elements); return range.asSet(DiscreteDomains.integers()); }
@Test public void openClosed() { System.out.println("## openClosed ##"); Range<Integer> range = Ranges.openClosed(0, 10); System.out.println(range); printAllElements(range); }
@Test public void closedOpen() { System.out.println("## closedOpen ##"); Range<Integer> range = Ranges.closedOpen(0, 10); System.out.println(range); printAllElements(range); }
public static List<Range<Integer>> parseToRange(String value) { List<Range<Integer>> validRanges = new ArrayList<Range<Integer>>(); String[] codes = value.split(","); for (String code : codes) { String[] fromTo = code.trim().split(":"); checkArgument( fromTo.length <= 2, "Code %s should be an interval from:to or a single value", code); Integer from; try { from = Integer.parseInt(fromTo[0]); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("Invalid number " + fromTo[0]); } Integer to = from; if (fromTo.length != 1) { try { to = Integer.parseInt(fromTo[1]); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("Invalid number " + fromTo[1]); } } checkArgument(from <= to, "Interval %s should be FROM less than TO", code); validRanges.add(Ranges.closed(from, to)); } return validRanges; }
@Test public void filterUsingLessThan() { List<Double> scores = Lists.newArrayList(3.0, 3.5, 4.0, 4.5, 5.0); double median = getMedian(scores); List<Double> belowMedian = Lists.newArrayList(Iterables.filter(scores, Ranges.lessThan(median))); System.out.println(belowMedian); }
@Override protected SortedSet<Integer> create(Integer[] elements) { SortedSet<Integer> set = nullCheckedTreeSet(elements); if (set.isEmpty()) { /* * The (tooLow + 1, tooHigh) arguments below would be invalid because tooLow would be * greater than tooHigh. */ return Ranges.openClosed(0, 1).asSet(DiscreteDomains.integers()).subSet(0, 1); } int tooHigh = set.last() + 1; int tooLow = set.first() - 1; set.add(tooHigh); set.add(tooLow); return checkedCreate(set).subSet(tooLow + 1, tooHigh); }
private List<ScheduledCommand> createMoveCommands(final double length) { if (Math.abs(length) < 1) { return ImmutableList.<ScheduledCommand>of(); } final double currentAngle = workspace.getCurrentAngle(); final ScheduledCommand command = new ScheduledCommand() { @Override public void execute() { workspace.updatePosition(length > 0 ? 1.0 : -1.0, currentAngle); } }; final Function<Integer, ScheduledCommand> function = new Function<Integer, ScheduledCommand>() { @Override public ScheduledCommand apply(Integer input) { return command; } }; final List<Integer> range = Ranges.closed(1, (int) length).asSet(DiscreteDomains.integers()).asList(); return transform(range, function); }
@Test public void span() { assertThat(Ranges.closed(3, 5).span(Ranges.open(5, 10)), is(Ranges.closedOpen(3, 10))); assertThat(Ranges.closed(0, 9).span(Ranges.closed(3, 4)), is(Ranges.closed(0, 9))); assertThat(Ranges.closed(0, 5).span(Ranges.closed(3, 9)), is(Ranges.closed(0, 9))); assertThat(Ranges.open(3, 5).span(Ranges.open(5, 10)), is(Ranges.open(3, 10))); assertThat(Ranges.closed(1, 5).span(Ranges.closed(6, 10)), is(Ranges.closed(1, 10))); }
@Test(expected = IllegalArgumentException.class) public void intersectionWhenNoIntersectionFound() { Ranges.open(3, 5).intersection(Ranges.open(5, 10)); }
@Test public void intersection() { assertThat(Ranges.closed(3, 5).intersection(Ranges.open(5, 10)), is(Ranges.openClosed(5, 5))); assertThat(Ranges.closed(0, 9).intersection(Ranges.closed(3, 4)), is(Ranges.closed(3, 4))); assertThat(Ranges.closed(0, 5).intersection(Ranges.closed(3, 9)), is(Ranges.closed(3, 5))); }