IntervalsParser { public static <T> IntervalMap<T> parseIntervalMap(Class<T> typeClass, String input, DateTimeZone timeZone) throws IllegalArgumentException { if (typeClass == null) { throw new IllegalArgumentException("typeClass required"); } if (input == null) { return null; } List<IntervalWithValue<T>> intervals; try { intervals = parseIntervals(typeClass, input, timeZone); } catch (IOException ex) { throw new RuntimeException("Unexpected expection while parsing intervals", ex); } int capacity = intervals.size(); IntervalMap result; typeClass = AttributeUtils.getStandardizedType(typeClass); if (typeClass.equals(String.class)) { result = new IntervalStringMap(capacity); } else if (typeClass.equals(Byte.class)) { result = new IntervalByteMap(capacity); } else if (typeClass.equals(Short.class)) { result = new IntervalShortMap(capacity); } else if (typeClass.equals(Integer.class)) { result = new IntervalIntegerMap(capacity); } else if (typeClass.equals(Long.class)) { result = new IntervalLongMap(capacity); } else if (typeClass.equals(Float.class)) { result = new IntervalFloatMap(capacity); } else if (typeClass.equals(Double.class)) { result = new IntervalDoubleMap(capacity); } else if (typeClass.equals(Boolean.class)) { result = new IntervalBooleanMap(capacity); } else if (typeClass.equals(Character.class)) { result = new IntervalCharMap(capacity); } else { throw new IllegalArgumentException("Unsupported type " + typeClass.getClass().getCanonicalName()); } for (IntervalWithValue<T> interval : intervals) { T value = interval.getValue(); if (value == null) { throw new IllegalArgumentException("A value must be provided for each interval"); } result.put(interval.getInterval(), interval.getValue()); } return result; }  static IntervalSet parseIntervalSet(String input, DateTimeZone timeZone); static IntervalSet parseIntervalSet(String input); static IntervalMap<T> parseIntervalMap(Class<T> typeClass, String input, DateTimeZone timeZone); static IntervalMap<T> parseIntervalMap(Class<T> typeClass, String input);  }
@Test public void testParseIntervalMapString() { IntervalStringMap expected = new IntervalStringMap(); expected.put(new Interval(1, 2), "Value1"); expected.put(new Interval(3, 5), "Value2"); expected.put(new Interval(5, 6), "Value 3"); expected.put(new Interval(6, 7), " Value 4 "); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(String.class, "[1, 2, Value1]; [3, 5, 'Value2']; [5, 6, Value 3]; [6, 7, \" Value 4 \"]")); }
@Test public void testParseIntervalMapByte() { IntervalByteMap expected = new IntervalByteMap(); expected.put(new Interval(1, 2), (byte) 1); expected.put(new Interval(3, 5), (byte) 2); expected.put(new Interval(5, 6), (byte) 3); expected.put(new Interval(6, 7), (byte) 4); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Byte.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(byte.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); }
@Test public void testParseIntervalMapShort() { IntervalShortMap expected = new IntervalShortMap(); expected.put(new Interval(1, 2), (short) 1); expected.put(new Interval(3, 5), (short) 2); expected.put(new Interval(5, 6), (short) 3); expected.put(new Interval(6, 7), (short) 4); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Short.class, "[1, 2, 1.1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(short.class, "[1, 2, 1.1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); }
@Test public void testParseIntervalMapInteger() { IntervalIntegerMap expected = new IntervalIntegerMap(); expected.put(new Interval(1, 2), 1); expected.put(new Interval(3, 5), 2); expected.put(new Interval(5, 6), 3); expected.put(new Interval(6, 7), 4); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Integer.class, "[1, 2, 1.]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(int.class, "[1, 2, 1.]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); }
@Test public void testParseIntervalMapLong() { IntervalLongMap expected = new IntervalLongMap(); expected.put(new Interval(1, 2), 1l); expected.put(new Interval(3, 5), 2l); expected.put(new Interval(5, 6), 3l); expected.put(new Interval(6, 7), 4l); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Long.class, "[1, 2, 1.0]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(long.class, "[1, 2, 1.0]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); }
@Test public void testParseIntervalMapFloat() { IntervalFloatMap expected = new IntervalFloatMap(); expected.put(new Interval(1, 2), 1f); expected.put(new Interval(3, 5), 2f); expected.put(new Interval(5, 6), 3f); expected.put(new Interval(6, 7), 4f); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Float.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(float.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); }
@Test public void testParseIntervalMapDouble() { IntervalDoubleMap expected = new IntervalDoubleMap(); expected.put(new Interval(1, 2), 1d); expected.put(new Interval(3, 5), 2d); expected.put(new Interval(5, 6), 3d); expected.put(new Interval(6, 7), 4d); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Double.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(double.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]")); }
@Test public void testParseIntervalMapBoolean() { IntervalBooleanMap expected = new IntervalBooleanMap(); expected.put(new Interval(1, 2), true); expected.put(new Interval(3, 5), false); expected.put(new Interval(5, 6), false); expected.put(new Interval(6, 7), true); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Boolean.class, "[1, 2, true]; [3, 5, false]; [5, 6, '0']; [6, 7, \"1\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(boolean.class, "[1, 2, true]; [3, 5, false]; [5, 6, 0]; [6, 7, 1]")); }
@Test public void testParseIntervalMapChar() { IntervalCharMap expected = new IntervalCharMap(); expected.put(new Interval(1, 2), 'a'); expected.put(new Interval(3, 5), 'b'); expected.put(new Interval(5, 6), 'c'); expected.put(new Interval(6, 7), 'd'); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(Character.class, "[1, 2, a]; [3, 5, b]; [5, 6, 'c']; [6, 7, \"d\"]")); assertEqualIntervalMaps(expected, IntervalsParser.parseIntervalMap(char.class, "[1, 2, a]; [3, 5, b]; [5, 6, 'c']; [6, 7, \"d\"]")); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapByteBadFormat() { IntervalsParser.parseIntervalMap(Byte.class, "[1, 2, a]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapShortBadFormat() { IntervalsParser.parseIntervalMap(Short.class, "[1, 2, a]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapIntegerBadFormat() { IntervalsParser.parseIntervalMap(Integer.class, "[1, 2, a]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapLongBadFormat() { IntervalsParser.parseIntervalMap(Long.class, "[1, 2, a]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapFloatBadFormat() { IntervalsParser.parseIntervalMap(Float.class, "[1, 2, 1..4]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapDoubleBadFormat() { IntervalsParser.parseIntervalMap(Double.class, "[1, 2, 4oe]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalMapCharBadFormat() { IntervalsParser.parseIntervalMap(Character.class, "[1, 2, abc]"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testParseIntervalUnsupportedType() { IntervalsParser.parseIntervalMap(Date.class, "[1, 2, 1]; [3, 5, 2]; [5, 6, '3']; [6, 7, \"4\"]"); }