Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/com/greplin/interval/LongInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,13 @@ public boolean intersects(final LongInterval other) {
public static LongInterval valueOf(final String value) {
String trimmed = value.trim();
int middle = trimmed.indexOf('-', 1);
long start = Long.parseLong(trimmed.substring(0, middle).trim());
long end = Long.parseLong(trimmed.substring(middle + 1).trim());
return new LongInterval(start, end);
try {
long start = Long.parseLong(trimmed.substring(0, middle).trim());
long end = Long.parseLong(trimmed.substring(middle + 1).trim());
return new LongInterval(start, end);
} catch(NumberFormatException e) {
throw new NumberFormatException("Passed value does not contain two parsable long values");
}
}


Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/greplin/interval/LongIntervalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public void testNegativeNumbers() throws Exception {
assertParse("-2--1", -2, -1);
}

@Test(expected=NumberFormatException.class)
public void testEmptyNumbers() throws Exception {
LongInterval result = LongInterval.valueOf("null-null");
}

@Test
public void testEquals() throws Exception {
Assert.assertEquals(new LongInterval(1, 100), new LongInterval(1, 100));
Expand Down