Range { public static List<Range> parse(String rangeSpec) throws IllegalArgumentException { if (rangeSpec.length() == 0) { return Collections.emptyList(); } List<Range> ranges = new ArrayList<Range>(); String[] specs = rangeSpec.split(","); for (String spec : specs) { String[] split = spec.split("-"); try { ranges.add(new Range(Integer.parseInt(split[0]), Integer .parseInt(split[1]))); } catch (NumberFormatException e) { throw new IllegalArgumentException(e.getMessage()); } } return ranges; } Range(int start, int end); int getStart(); int getEnd(); String getSubstring(String line); @Override int hashCode(); @Override boolean equals(Object obj); static List<Range> parse(String rangeSpec);  }
@Test public void parsesEmptyRangeSpec() { assertThat(Range.parse("").size(), is(0)); }
@Test public void parsesSingleRangeSpec() { List<Range> ranges = Range.parse("1-3"); assertThat(ranges.size(), is(1)); assertThat(ranges.get(0), is(new Range(1, 3))); }
@Test public void parsesMultipleRangeSpec() { List<Range> ranges = Range.parse("1-3,5-10"); assertThat(ranges.size(), is(2)); assertThat(ranges.get(0), is(new Range(1, 3))); assertThat(ranges.get(1), is(new Range(5, 10))); }
@Test(expected = IllegalArgumentException.class) public void failsOnInvalidSpec() { Range.parse("1-n"); }