diff --git a/src/main/java/com/greplin/interval/LongInterval.java b/src/main/java/com/greplin/interval/LongInterval.java index 63dc3bd..890ceb6 100644 --- a/src/main/java/com/greplin/interval/LongInterval.java +++ b/src/main/java/com/greplin/interval/LongInterval.java @@ -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"); + } } diff --git a/src/test/java/com/greplin/interval/LongIntervalTest.java b/src/test/java/com/greplin/interval/LongIntervalTest.java index 5475043..9a629e8 100644 --- a/src/test/java/com/greplin/interval/LongIntervalTest.java +++ b/src/test/java/com/greplin/interval/LongIntervalTest.java @@ -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));