@Test public void testSubtract() { Range<Integer> r1 = Range.create(null, false, null, false); Range<Integer> r2 = Range.create(-5, false, 5, true); List<Range<Integer>> result = RangeUtils.subtract(r1, r2); assertEquals(0, result.size()); result = RangeUtils.subtract(r2, r1); assertEquals(2, result.size()); assertEquals(new Range<Integer>(null, false, -5, true), result.get(0)); assertEquals(new Range<Integer>(5, false, null, false), result.get(1)); }
public static <T extends Number & Comparable> List<Range<T>> subtract(Range<T> r1, Range<T> r2) { List<Range<T>> difference = new ArrayList<Range<T>>(); if (r1.equals(r2)) { return difference; } Range<T> common = intersection(r1, r2); if (common == null) { difference.add( new Range<T>(r2) ); return difference; } if (common.equals(r2)) { return difference; } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(common, r2); int minComp = result.getAt(RangeExtendedComparator.MIN_MIN); int maxComp = result.getAt(RangeExtendedComparator.MAX_MAX); if (minComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } else { if (maxComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); } else { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } } return difference; }
RangeUtils { public static <T extends Number & Comparable> List<Range<T>> subtract(Range<T> r1, Range<T> r2) { List<Range<T>> difference = new ArrayList<Range<T>>(); if (r1.equals(r2)) { return difference; } Range<T> common = intersection(r1, r2); if (common == null) { difference.add( new Range<T>(r2) ); return difference; } if (common.equals(r2)) { return difference; } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(common, r2); int minComp = result.getAt(RangeExtendedComparator.MIN_MIN); int maxComp = result.getAt(RangeExtendedComparator.MAX_MAX); if (minComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } else { if (maxComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); } else { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } } return difference; } }
RangeUtils { public static <T extends Number & Comparable> List<Range<T>> subtract(Range<T> r1, Range<T> r2) { List<Range<T>> difference = new ArrayList<Range<T>>(); if (r1.equals(r2)) { return difference; } Range<T> common = intersection(r1, r2); if (common == null) { difference.add( new Range<T>(r2) ); return difference; } if (common.equals(r2)) { return difference; } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(common, r2); int minComp = result.getAt(RangeExtendedComparator.MIN_MIN); int maxComp = result.getAt(RangeExtendedComparator.MAX_MAX); if (minComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } else { if (maxComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); } else { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } } return difference; }  }
RangeUtils { public static <T extends Number & Comparable> List<Range<T>> subtract(Range<T> r1, Range<T> r2) { List<Range<T>> difference = new ArrayList<Range<T>>(); if (r1.equals(r2)) { return difference; } Range<T> common = intersection(r1, r2); if (common == null) { difference.add( new Range<T>(r2) ); return difference; } if (common.equals(r2)) { return difference; } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(common, r2); int minComp = result.getAt(RangeExtendedComparator.MIN_MIN); int maxComp = result.getAt(RangeExtendedComparator.MAX_MAX); if (minComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } else { if (maxComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); } else { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } } return difference; }  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> List<Range<T>> subtract(Range<T> r1, Range<T> r2) { List<Range<T>> difference = new ArrayList<Range<T>>(); if (r1.equals(r2)) { return difference; } Range<T> common = intersection(r1, r2); if (common == null) { difference.add( new Range<T>(r2) ); return difference; } if (common.equals(r2)) { return difference; } RangeExtendedComparator<T> rc = new RangeExtendedComparator<T>(); RangeExtendedComparator.Result result = rc.compare(common, r2); int minComp = result.getAt(RangeExtendedComparator.MIN_MIN); int maxComp = result.getAt(RangeExtendedComparator.MAX_MAX); if (minComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } else { if (maxComp == RangeExtendedComparator.EQ) { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); } else { difference.add(new Range<T>(r2.getMin(), r2.isMinIncluded(), common.getMin(), !common.isMinIncluded())); difference.add(new Range<T>(common.getMax(), !common.isMaxIncluded(), r2.getMax(), r2.isMaxIncluded())); } } return difference; }  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);  }