/** * Compares this Range to another Range. Range comparisons are more involved * than comparisons between point values. There are 18 distinct comparison * results as described by Hayes (2003). See {@linkplain RangeComparator} for * more details. * * @param other the other Range * @return a RangeComparator.Result enum identifying the relationship from the * point of view of this Range */ RangeExtendedComparator.Result compareTo(Range<T> other) { RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); return rc.compare(this, other); } }
/** * Tests if this range intersects another range. Two ranges intersect if * there is at least one value, x, for which contains(x) returns true * for both ranges * * @param other the range to check for intersection * @return {@code true} if the ranges intersect; {@code false} otherwise */ public boolean intersects(Range<T> other) { RangeExtendedComparator.Result comp = this.compareTo(other); return RangeExtendedComparator.isIntersection(comp); }
RangeExtendedComparator<T> comparator = new RangeExtendedComparator<T>(); for (int j = i+1; j < inputs.size() && !changed; j++) { Range<T> r2 = inputs.get(j); RangeExtendedComparator.Result result = comparator.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { switch (result) { case EEEE: // r1 and r2 are equal points
/** * Creates a new comparator that will use the supplied extended comparator. * * @param ec extended comparator (may be null) */ public RangeComparator(RangeExtendedComparator<T> ec) { this.delegate = ec == null ? new RangeExtendedComparator<T>() : ec; }
/** * Compares two ranges. * Returns a negative value if r1 min is less than r2 min OR r1 max is less * than r2 max; a positive value if r1 min is greater than r2 min OR r2 * max is greater than r2 max; or zero if the ranges are identical. * * @param r1 first range * @param r2 second range * * @returns an integer value indicating r1 compared to r2 */ public int compare(Range<T> r1, Range<T> r2) { RangeExtendedComparator.Result result = delegate.compare(r1, r2); switch (result.getAt(RangeExtendedComparator.MIN_MIN)) { case RangeExtendedComparator.LT: return -1; case RangeExtendedComparator.GT: return 1; default: switch (result.getAt(RangeExtendedComparator.MAX_MAX)) { case RangeExtendedComparator.LT: return -1; case RangeExtendedComparator.GT: return 1; default: return 0; } } }
return pointCompare(r1, r2); if (r1.isMinClosed()) { if (r2.isMaxClosed()) { int flag = validFlag(r1.getMin().compareTo(r2.getMax())); if (flag == EQ) { if (r1.isMinIncluded()) { if (r1.isMinClosed()) { if (r2.isMinClosed()) { int flag = validFlag(r1.getMin().compareTo(r2.getMin())); if (flag == EQ) { if (r1.isMinIncluded()) { if (r1.isMaxClosed()) { if (r2.isMaxClosed()) { int flag = validFlag(r1.getMax().compareTo(r2.getMax())); if (flag == EQ) { if (r1.isMaxIncluded()) { if (r1.isMaxClosed()) { if (r2.isMinClosed()) { int flag = validFlag(r1.getMax().compareTo(r2.getMin())); if (flag == EQ) { if (r1.isMaxIncluded()) {
} else { compFlags[0] = compFlags[1] = compFlags[2] = compFlags[3] = validFlag(r1.getMin().compareTo(r2.getMin())); compFlags[0] = compFlags[2] = LT; } else { int flag = validFlag(r1.getMin().compareTo(r2.getMax())); if (flag == EQ) { if (r2.isMaxIncluded()) { compFlags[1] = compFlags[3] = GT; } else { int flag = validFlag(r1.getMin().compareTo(r2.getMin())); if (flag == EQ) { if (r2.isMinIncluded()) { compFlags[0] = compFlags[1] = LT; } else { int flag = validFlag(r1.getMin().compareTo(r2.getMin())); if (flag == EQ) { if (r1.isMinIncluded()) { compFlags[2] = compFlags[3] = GT; } else { int flag = validFlag(r1.getMax().compareTo(r2.getMin())); if (flag == EQ) { if (r1.isMaxIncluded()) {
final int elements = sortedRanges.size(); if (elements > 1) { RangeExtendedComparator rc = new RangeExtendedComparator(); List<Range> rr = (List<Range>) sortedRanges; for (int i = 0; i < elements - 1; i++) { Range r1 = rr.get(i); Range r2 = rr.get(i + 1); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { ok = false; msg.append(paramNames[RANGES_ARG]).append(" arg can't contain intersecting ranges");
/** * Sorts a list of ranges into ascending order of min value, then max value. * * @param <T> the value type * @param ranges the ranges to sort */ public static <T extends Number & Comparable> void sortInPlace(List<Range<T>> ranges) { Collections.sort(ranges, new RangeComparator(new RangeExtendedComparator<T>())); }
RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { T min; boolean minIncluded;
RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(common, r2);
/** * Sorts a collection of ranges into ascending order of min value, then max value. * Returns a new List of sorted ranges, leaving the input collection unmodified. * * @param <T> the value type * @param ranges the ranges to sort * * @return sorted ranges as a {@code List} */ public static <T extends Number & Comparable> List<Range<T>> sort(Collection<Range<T>> ranges) { List<Range<T>> copy = new ArrayList<Range<T>>(ranges); Collections.sort(copy, new RangeComparator(new RangeExtendedComparator<T>())); return copy; }