RangeOption implements Comparable<RangeOption> { public static RangeOption parse(String option) { Matcher rangeMatcher = RANGE_PATTERN.matcher(option); rangeMatcher.find(); if (!rangeMatcher.matches()) { return null; } String initialStr = rangeMatcher.group(1); int initial = Integer.parseInt(initialStr); int terminal = TERMINAL_MISSING; if (rangeMatcher.group(2) != null) { String terminalStr = rangeMatcher.group(3); if ("*".equals(terminalStr)) { terminal = TERMINAL_END_OF_RANGE; } else { terminal = Integer.parseInt(terminalStr); } } return new RangeOption(initial, terminal); } RangeOption(int initial);  RangeOption(int initial, int terminal); boolean isTerminalEndOfRange(); boolean isTerminalMissing(); int getInitial(); int getTerminal(); boolean isFullRange(); String toString(); void appendTo(StringBuilder rangeBuilder); static RangeOption parse(String option); int compareTo(RangeOption that); @Override boolean equals(Object o); @Override int hashCode(); RangeOption nextRange(int pageSize); static final int TERMINAL_END_OF_RANGE; static final int TERMINAL_MISSING; }
@Test public void testParse() throws Exception { RangeOption range = RangeOption.parse("Range=0-100"); assertThat(range.getInitial()).isEqualTo(0); assertThat(range.getTerminal()).isEqualTo(100); range = RangeOption.parse("range=0-100"); assertThat(range.getInitial()).isEqualTo(0); assertThat(range.getTerminal()).isEqualTo(100); range = RangeOption.parse("RANGE=0-100"); assertThat(range.getInitial()).isEqualTo(0); assertThat(range.getTerminal()).isEqualTo(100); range = RangeOption.parse("Range=0-*"); assertThat(range.getInitial()).isEqualTo(0); assertThat(range.getTerminal()).isEqualTo(RangeOption.TERMINAL_END_OF_RANGE); range = RangeOption.parse("Range=10"); assertThat(range.getInitial()).isEqualTo(10); assertThat(range.getTerminal()).isEqualTo(RangeOption.TERMINAL_MISSING); }
@Test public void testParseInvalid() { assertThat(RangeOption.parse("Range=10-")).isNull(); assertThat(RangeOption.parse("Range=10-a")).isNull(); assertThat(RangeOption.parse("lang-en")).isNull(); assertThat(RangeOption.parse("member;Range=10-100")).isNull(); assertThat(RangeOption.parse(";Range=10-100")).isNull(); assertThat(RangeOption.parse("Range=10-100;")).isNull(); assertThat(RangeOption.parse("Range=10-100;lang-de")).isNull(); }