/** * Determines whether the lower endpoint is greater than the upper endpoint. * @return true if the bounds are inverted, false otherwise. */ private boolean hasInvertedBounds() { return isBounded() && lowerEndpoint().get().compareTo(upperEndpoint().get()) > 0; }
public <T extends Comparable<? super T>> InvalidRangeException(final String message, final Range<T> range) { super(message + ": " + range.toString()); } }
public String serialize(final Function<T, String> serializer) { return String.format("(%s to %s)", lowerEndpoint().map(e -> serializer.apply(e)).orElse(UNBOUND), upperEndpoint().map(e -> serializer.apply(e)).orElse(UNBOUND)); }
Range(final Optional<Bound<T>> lowerBound, final Optional<Bound<T>> upperBound) { this.lowerBound = lowerBound; this.upperBound = upperBound; if (hasInvertedBounds()) { throw new InvertedBoundsException(this); } if (hasSameExclusiveBounds()) { throw new SameExclusiveBoundsException(this); } }
@Override public String toString() { final String lower = lowerBound().map(b -> (b.isExclusive() ? "(" : "[") + b.endpoint()).orElse("(*"); final String upper = upperBound().map(b -> b.endpoint() + (b.isExclusive() ? ")" : "]")).orElse("*)"); return lower + " to " + upper; }
/** * Determines whether the range is of the form (a, a). * @return true if the range is of the form (a, a), false otherwise. */ private boolean hasSameExclusiveBounds() { return isBounded() && lowerBound.equals(upperBound) && lowerBound.get().isExclusive(); } }
/** * Turns a group of ranges into an expression of the form "(e1 to e2),(e3 to e4),..." * @return the generated range expression. */ private String toRangeExpression(Iterable<? extends Range<V>> ranges) { return toStream(ranges).map(r -> r.serialize(serializer())).filter(r -> !r.isEmpty()).collect(joining(",")); } }
/** * Determines whether the range contains no values. A range is empty when it has the form (a, a] or [a, a). * @return true if the range cannot contain any value. */ public boolean isEmpty() { return isBounded() && lowerEndpoint().equals(upperEndpoint()) && lowerBound.get().isExclusive() != upperBound.get().isExclusive(); }