@Test public void ofStringTest() { assertThat(integerRange("[1,3]").lower(), is(1)); assertThat(integerRange("[1,3]").upper(), is(3)); assertThat(integerRange("[1,3]").isUpperBoundClosed(), is(true)); assertThat(integerRange("[1,3]").isLowerBoundClosed(), is(true)); assertThat(integerRange("[,3]").lower(), is(nullValue())); assertThat(integerRange("[,3]").upper(), is(3)); assertThat(integerRange("[,3]").hasLowerBound(), is(false)); assertThat(integerRange("[,3]").hasUpperBound(), is(true)); assertThat(integerRange("[,3]").isUpperBoundClosed(), is(true)); assertThat(integerRange("[,3]").isLowerBoundClosed(), is(false)); assertThat(integerRange("[,]").lower(), is(nullValue())); assertThat(integerRange("[,]").upper(), is(nullValue())); assertThat(integerRange("[,]").hasLowerBound(), is(false)); assertThat(integerRange("[,]").hasUpperBound(), is(false)); assertThat(integerRange("[,]").isUpperBoundClosed(), is(false)); assertThat(integerRange("[,]").isLowerBoundClosed(), is(false)); assertThat(integerRange("(-5,5]").isUpperBoundClosed(), is(true)); assertThat(integerRange("(-5,5]").isLowerBoundClosed(), is(false)); }
@SuppressWarnings("unchecked") public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) { if(str.equals(EMPTY)) { return emptyRange(clazz); } int mask = str.charAt(0) == '[' ? LOWER_INCLUSIVE : LOWER_EXCLUSIVE; mask |= str.charAt(str.length() - 1) == ']' ? UPPER_INCLUSIVE : UPPER_EXCLUSIVE; int delim = str.indexOf(','); if (delim == -1) { throw new IllegalArgumentException("Cannot find comma character"); } String lowerStr = str.substring(1, delim); String upperStr = str.substring(delim + 1, str.length() - 1); if (lowerStr.length() == 0 || lowerStr.endsWith(INFINITY)) { mask |= LOWER_INFINITE; } if (upperStr.length() == 0 || upperStr.endsWith(INFINITY)) { mask |= UPPER_INFINITE; } T lower = null; T upper = null; if ((mask & LOWER_INFINITE) != LOWER_INFINITE) { lower = converter.apply(lowerStr); } if ((mask & UPPER_INFINITE) != UPPER_INFINITE) { upper = converter.apply(upperStr); } return new Range<>(lower, upper, mask, clazz); }
Range implements Serializable { @SuppressWarnings("unchecked") public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) { if(str.equals(EMPTY)) { return emptyRange(clazz); } int mask = str.charAt(0) == '[' ? LOWER_INCLUSIVE : LOWER_EXCLUSIVE; mask |= str.charAt(str.length() - 1) == ']' ? UPPER_INCLUSIVE : UPPER_EXCLUSIVE; int delim = str.indexOf(','); if (delim == -1) { throw new IllegalArgumentException("Cannot find comma character"); } String lowerStr = str.substring(1, delim); String upperStr = str.substring(delim + 1, str.length() - 1); if (lowerStr.length() == 0 || lowerStr.endsWith(INFINITY)) { mask |= LOWER_INFINITE; } if (upperStr.length() == 0 || upperStr.endsWith(INFINITY)) { mask |= UPPER_INFINITE; } T lower = null; T upper = null; if ((mask & LOWER_INFINITE) != LOWER_INFINITE) { lower = converter.apply(lowerStr); } if ((mask & UPPER_INFINITE) != UPPER_INFINITE) { upper = converter.apply(upperStr); } return new Range<>(lower, upper, mask, clazz); } }
Range implements Serializable { @SuppressWarnings("unchecked") public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) { if(str.equals(EMPTY)) { return emptyRange(clazz); } int mask = str.charAt(0) == '[' ? LOWER_INCLUSIVE : LOWER_EXCLUSIVE; mask |= str.charAt(str.length() - 1) == ']' ? UPPER_INCLUSIVE : UPPER_EXCLUSIVE; int delim = str.indexOf(','); if (delim == -1) { throw new IllegalArgumentException("Cannot find comma character"); } String lowerStr = str.substring(1, delim); String upperStr = str.substring(delim + 1, str.length() - 1); if (lowerStr.length() == 0 || lowerStr.endsWith(INFINITY)) { mask |= LOWER_INFINITE; } if (upperStr.length() == 0 || upperStr.endsWith(INFINITY)) { mask |= UPPER_INFINITE; } T lower = null; T upper = null; if ((mask & LOWER_INFINITE) != LOWER_INFINITE) { lower = converter.apply(lowerStr); } if ((mask & UPPER_INFINITE) != UPPER_INFINITE) { upper = converter.apply(upperStr); } return new Range<>(lower, upper, mask, clazz); } private  Range(T lower, T upper, int mask, Class<T> clazz); }
Range implements Serializable { @SuppressWarnings("unchecked") public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) { if(str.equals(EMPTY)) { return emptyRange(clazz); } int mask = str.charAt(0) == '[' ? LOWER_INCLUSIVE : LOWER_EXCLUSIVE; mask |= str.charAt(str.length() - 1) == ']' ? UPPER_INCLUSIVE : UPPER_EXCLUSIVE; int delim = str.indexOf(','); if (delim == -1) { throw new IllegalArgumentException("Cannot find comma character"); } String lowerStr = str.substring(1, delim); String upperStr = str.substring(delim + 1, str.length() - 1); if (lowerStr.length() == 0 || lowerStr.endsWith(INFINITY)) { mask |= LOWER_INFINITE; } if (upperStr.length() == 0 || upperStr.endsWith(INFINITY)) { mask |= UPPER_INFINITE; } T lower = null; T upper = null; if ((mask & LOWER_INFINITE) != LOWER_INFINITE) { lower = converter.apply(lowerStr); } if ((mask & UPPER_INFINITE) != UPPER_INFINITE) { upper = converter.apply(upperStr); } return new Range<>(lower, upper, mask, clazz); } private  Range(T lower, T upper, int mask, Class<T> clazz); @SuppressWarnings("unchecked") static Range<T> closed(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> open(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> openClosed(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> closedOpen(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> openInfinite(T lower); @SuppressWarnings("unchecked") static Range<T> closedInfinite(T lower); @SuppressWarnings("unchecked") static Range<T> infiniteOpen(T upper); @SuppressWarnings("unchecked") static Range<T> infiniteClosed(T upper); @SuppressWarnings("unchecked") static Range<T> infinite(Class<T> cls); @SuppressWarnings("unchecked") static Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz); static Range<BigDecimal> bigDecimalRange(String range); static Range<Integer> integerRange(String range); static Range<Long> longRange(String range); static Range<LocalDateTime> localDateTimeRange(String range); static Range<LocalDate> localDateRange(String range); static Range<ZonedDateTime> zonedDateTimeRange(String rangeStr); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); boolean hasMask(int flag); boolean isLowerBoundClosed(); boolean isUpperBoundClosed(); boolean hasLowerBound(); boolean hasUpperBound(); T lower(); T upper(); @SuppressWarnings("unchecked") boolean contains(T point); boolean contains(Range<T> range); String asString(); static Range<R> emptyRange(Class<R> clazz); }
Range implements Serializable { @SuppressWarnings("unchecked") public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) { if(str.equals(EMPTY)) { return emptyRange(clazz); } int mask = str.charAt(0) == '[' ? LOWER_INCLUSIVE : LOWER_EXCLUSIVE; mask |= str.charAt(str.length() - 1) == ']' ? UPPER_INCLUSIVE : UPPER_EXCLUSIVE; int delim = str.indexOf(','); if (delim == -1) { throw new IllegalArgumentException("Cannot find comma character"); } String lowerStr = str.substring(1, delim); String upperStr = str.substring(delim + 1, str.length() - 1); if (lowerStr.length() == 0 || lowerStr.endsWith(INFINITY)) { mask |= LOWER_INFINITE; } if (upperStr.length() == 0 || upperStr.endsWith(INFINITY)) { mask |= UPPER_INFINITE; } T lower = null; T upper = null; if ((mask & LOWER_INFINITE) != LOWER_INFINITE) { lower = converter.apply(lowerStr); } if ((mask & UPPER_INFINITE) != UPPER_INFINITE) { upper = converter.apply(upperStr); } return new Range<>(lower, upper, mask, clazz); } private  Range(T lower, T upper, int mask, Class<T> clazz); @SuppressWarnings("unchecked") static Range<T> closed(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> open(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> openClosed(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> closedOpen(T lower, T upper); @SuppressWarnings("unchecked") static Range<T> openInfinite(T lower); @SuppressWarnings("unchecked") static Range<T> closedInfinite(T lower); @SuppressWarnings("unchecked") static Range<T> infiniteOpen(T upper); @SuppressWarnings("unchecked") static Range<T> infiniteClosed(T upper); @SuppressWarnings("unchecked") static Range<T> infinite(Class<T> cls); @SuppressWarnings("unchecked") static Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz); static Range<BigDecimal> bigDecimalRange(String range); static Range<Integer> integerRange(String range); static Range<Long> longRange(String range); static Range<LocalDateTime> localDateTimeRange(String range); static Range<LocalDate> localDateRange(String range); static Range<ZonedDateTime> zonedDateTimeRange(String rangeStr); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); boolean hasMask(int flag); boolean isLowerBoundClosed(); boolean isUpperBoundClosed(); boolean hasLowerBound(); boolean hasUpperBound(); T lower(); T upper(); @SuppressWarnings("unchecked") boolean contains(T point); boolean contains(Range<T> range); String asString(); static Range<R> emptyRange(Class<R> clazz); static final int LOWER_INCLUSIVE; static final int LOWER_EXCLUSIVE; static final int UPPER_INCLUSIVE; static final int UPPER_EXCLUSIVE; static final int LOWER_INFINITE; static final int UPPER_INFINITE; static final String EMPTY; static final String INFINITY; }