@Test public void testIntersection() { Range<Integer> r1 = Range.create(null, false, 5, false); Range<Integer> r2 = Range.create(-5, false, null, false); Range<Integer> result = RangeUtils.intersection(r2, r1); Range<Integer> expected = Range.create(-5, false, 5, false); assertEquals(expected, result); Range<Integer> r3 = Range.create(5, true, null, false); result = RangeUtils.intersection(r3, r1); assertNull(result); }
public static <T extends Number & Comparable> Range<T> intersection(Range<T> r1, Range<T> r2) { if (r1.isPoint()) { if (r2.isPoint()) { if (r1.equals(r2)) { return new Range<T>(r1); } else { return null; } } else { if ((r1.isMinInf() && r2.isMaxOpen()) || (r1.isMinNegInf() && r2.isMinOpen()) || r2.contains(r1.getMin())) { return new Range<T>(r1); } else { return null; } } } else if (r2.isPoint()) { if ((r2.isMinInf() && r1.isMaxOpen()) || (r2.isMinNegInf() && r1.isMinOpen()) || r1.contains(r2.getMin())) { return new Range<T>(r2); } else { return null; } } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { T min; boolean minIncluded; switch (result.getAt(RangeExtendedComparator.MIN_MIN)) { case RangeExtendedComparator.LT: min = r2.getMin(); minIncluded = r2.isMinIncluded(); break; case RangeExtendedComparator.GT: min = r1.getMin(); minIncluded = r1.isMinIncluded(); break; default: min = r1.getMin(); minIncluded = r1.isMinIncluded() || r2.isMinIncluded(); break; } T max; boolean maxIncluded; switch (result.getAt(RangeExtendedComparator.MAX_MAX)) { case RangeExtendedComparator.LT: max = r1.getMax(); maxIncluded = r1.isMaxIncluded(); break; case RangeExtendedComparator.GT: max = r2.getMax(); maxIncluded = r2.isMaxIncluded(); break; default: max = r1.getMax(); maxIncluded = r1.isMaxIncluded() || r2.isMaxIncluded(); break; } return new Range<T>(min, minIncluded, max, maxIncluded); } return null; }
RangeUtils { public static <T extends Number & Comparable> Range<T> intersection(Range<T> r1, Range<T> r2) { if (r1.isPoint()) { if (r2.isPoint()) { if (r1.equals(r2)) { return new Range<T>(r1); } else { return null; } } else { if ((r1.isMinInf() && r2.isMaxOpen()) || (r1.isMinNegInf() && r2.isMinOpen()) || r2.contains(r1.getMin())) { return new Range<T>(r1); } else { return null; } } } else if (r2.isPoint()) { if ((r2.isMinInf() && r1.isMaxOpen()) || (r2.isMinNegInf() && r1.isMinOpen()) || r1.contains(r2.getMin())) { return new Range<T>(r2); } else { return null; } } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { T min; boolean minIncluded; switch (result.getAt(RangeExtendedComparator.MIN_MIN)) { case RangeExtendedComparator.LT: min = r2.getMin(); minIncluded = r2.isMinIncluded(); break; case RangeExtendedComparator.GT: min = r1.getMin(); minIncluded = r1.isMinIncluded(); break; default: min = r1.getMin(); minIncluded = r1.isMinIncluded() || r2.isMinIncluded(); break; } T max; boolean maxIncluded; switch (result.getAt(RangeExtendedComparator.MAX_MAX)) { case RangeExtendedComparator.LT: max = r1.getMax(); maxIncluded = r1.isMaxIncluded(); break; case RangeExtendedComparator.GT: max = r2.getMax(); maxIncluded = r2.isMaxIncluded(); break; default: max = r1.getMax(); maxIncluded = r1.isMaxIncluded() || r2.isMaxIncluded(); break; } return new Range<T>(min, minIncluded, max, maxIncluded); } return null; } }
RangeUtils { public static <T extends Number & Comparable> Range<T> intersection(Range<T> r1, Range<T> r2) { if (r1.isPoint()) { if (r2.isPoint()) { if (r1.equals(r2)) { return new Range<T>(r1); } else { return null; } } else { if ((r1.isMinInf() && r2.isMaxOpen()) || (r1.isMinNegInf() && r2.isMinOpen()) || r2.contains(r1.getMin())) { return new Range<T>(r1); } else { return null; } } } else if (r2.isPoint()) { if ((r2.isMinInf() && r1.isMaxOpen()) || (r2.isMinNegInf() && r1.isMinOpen()) || r1.contains(r2.getMin())) { return new Range<T>(r2); } else { return null; } } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { T min; boolean minIncluded; switch (result.getAt(RangeExtendedComparator.MIN_MIN)) { case RangeExtendedComparator.LT: min = r2.getMin(); minIncluded = r2.isMinIncluded(); break; case RangeExtendedComparator.GT: min = r1.getMin(); minIncluded = r1.isMinIncluded(); break; default: min = r1.getMin(); minIncluded = r1.isMinIncluded() || r2.isMinIncluded(); break; } T max; boolean maxIncluded; switch (result.getAt(RangeExtendedComparator.MAX_MAX)) { case RangeExtendedComparator.LT: max = r1.getMax(); maxIncluded = r1.isMaxIncluded(); break; case RangeExtendedComparator.GT: max = r2.getMax(); maxIncluded = r2.isMaxIncluded(); break; default: max = r1.getMax(); maxIncluded = r1.isMaxIncluded() || r2.isMaxIncluded(); break; } return new Range<T>(min, minIncluded, max, maxIncluded); } return null; }  }
RangeUtils { public static <T extends Number & Comparable> Range<T> intersection(Range<T> r1, Range<T> r2) { if (r1.isPoint()) { if (r2.isPoint()) { if (r1.equals(r2)) { return new Range<T>(r1); } else { return null; } } else { if ((r1.isMinInf() && r2.isMaxOpen()) || (r1.isMinNegInf() && r2.isMinOpen()) || r2.contains(r1.getMin())) { return new Range<T>(r1); } else { return null; } } } else if (r2.isPoint()) { if ((r2.isMinInf() && r1.isMaxOpen()) || (r2.isMinNegInf() && r1.isMinOpen()) || r1.contains(r2.getMin())) { return new Range<T>(r2); } else { return null; } } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { T min; boolean minIncluded; switch (result.getAt(RangeExtendedComparator.MIN_MIN)) { case RangeExtendedComparator.LT: min = r2.getMin(); minIncluded = r2.isMinIncluded(); break; case RangeExtendedComparator.GT: min = r1.getMin(); minIncluded = r1.isMinIncluded(); break; default: min = r1.getMin(); minIncluded = r1.isMinIncluded() || r2.isMinIncluded(); break; } T max; boolean maxIncluded; switch (result.getAt(RangeExtendedComparator.MAX_MAX)) { case RangeExtendedComparator.LT: max = r1.getMax(); maxIncluded = r1.isMaxIncluded(); break; case RangeExtendedComparator.GT: max = r2.getMax(); maxIncluded = r2.isMaxIncluded(); break; default: max = r1.getMax(); maxIncluded = r1.isMaxIncluded() || r2.isMaxIncluded(); break; } return new Range<T>(min, minIncluded, max, maxIncluded); } return null; }  static List<Range<T>> createComplement(Range<T> range); static List<Range<T>> createComplement(Collection<Range<T>> ranges); static List<Range<T>> sort(Collection<Range<T>> ranges); static void sortInPlace(List<Range<T>> ranges); static List<Range<T>> simplify(Collection<Range<T>> ranges); static Range<T> intersection(Range<T> r1, Range<T> r2); static List<Range<T>> subtract(Range<T> r1, Range<T> r2); }
RangeUtils { public static <T extends Number & Comparable> Range<T> intersection(Range<T> r1, Range<T> r2) { if (r1.isPoint()) { if (r2.isPoint()) { if (r1.equals(r2)) { return new Range<T>(r1); } else { return null; } } else { if ((r1.isMinInf() && r2.isMaxOpen()) || (r1.isMinNegInf() && r2.isMinOpen()) || r2.contains(r1.getMin())) { return new Range<T>(r1); } else { return null; } } } else if (r2.isPoint()) { if ((r2.isMinInf() && r1.isMaxOpen()) || (r2.isMinNegInf() && r1.isMinOpen()) || r1.contains(r2.getMin())) { return new Range<T>(r2); } else { return null; } } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(r1, r2); if (RangeExtendedComparator.isIntersection(result)) { T min; boolean minIncluded; switch (result.getAt(RangeExtendedComparator.MIN_MIN)) { case RangeExtendedComparator.LT: min = r2.getMin(); minIncluded = r2.isMinIncluded(); break; case RangeExtendedComparator.GT: min = r1.getMin(); minIncluded = r1.isMinIncluded(); break; default: min = r1.getMin(); minIncluded = r1.isMinIncluded() || r2.isMinIncluded(); break; } T max; boolean maxIncluded; switch (result.getAt(RangeExtendedComparator.MAX_MAX)) { case RangeExtendedComparator.LT: max = r1.getMax(); maxIncluded = r1.isMaxIncluded(); break; case RangeExtendedComparator.GT: max = r2.getMax(); maxIncluded = r2.isMaxIncluded(); break; default: max = r1.getMax(); maxIncluded = r1.isMaxIncluded() || r2.isMaxIncluded(); break; } return new Range<T>(min, minIncluded, max, maxIncluded); } return null; }  static List<Range<T>> createComplement(Range<T> range); static List<Range<T>> createComplement(Collection<Range<T>> ranges); static List<Range<T>> sort(Collection<Range<T>> ranges); static void sortInPlace(List<Range<T>> ranges); static List<Range<T>> simplify(Collection<Range<T>> ranges); static Range<T> intersection(Range<T> r1, Range<T> r2); static List<Range<T>> subtract(Range<T> r1, Range<T> r2);  }