@Test public void testSimplify() { List<Range<Integer>> inputs = CollectionFactory.list(); inputs.add( Range.create(null, false, 5, false)); inputs.add( Range.create(0)); inputs.add( Range.create(0, true, 5, false)); inputs.add( Range.create(5, true, 10, true)); inputs.add( Range.create(20, true, 30, true)); List<Range<Integer>> result = RangeUtils.simplify(inputs); assertEquals(2, result.size()); assertEquals(new Range<Integer>(null, false, 10, true), result.get(0)); assertEquals(new Range<Integer>(20, true, 30, true), result.get(1)); }
public static <T extends Number & Comparable> List<Range<T>> simplify(Collection<Range<T>> ranges) { List<Range<T>> inputs = new ArrayList<Range<T>>(ranges); RangeExtendedComparator<T> comparator = new RangeExtendedComparator<T>(); boolean changed; do { changed = false; for (int i = 0; i < inputs.size()-1 && !changed; i++) { Range<T> r1 = inputs.get(i); 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: case EEGG: case LEEG: case LEGG: case LLEE: case LLEG: case LLGG: inputs.remove(j); break; case EGEG: case LELE: case LELG: case LGEG: case LGLG: inputs.remove(i); break; case EGGG: case LGGG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r2.getMin(), r2.isMinIncluded(), r1.getMax(), r1.isMaxIncluded())); break; case LLLE: case LLLG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); break; } changed = true; } } } } while (changed); Collections.sort(inputs, new RangeComparator(comparator)); do { changed = false; for (int i = 0; i < inputs.size() - 1 && !changed; i++) { Range<T> r1 = inputs.get(i); if (r1.isMaxClosed()) { for (int j = i + 1; j < inputs.size() && !changed; j++) { Range<T> r2 = inputs.get(j); if (r2.isMinClosed()) { if (r1.getMax().compareTo(r2.getMin()) == 0) { inputs.remove(j); inputs.remove(i); inputs.add(i, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); changed = true; } } } } } } while (changed); return inputs; }
RangeUtils { public static <T extends Number & Comparable> List<Range<T>> simplify(Collection<Range<T>> ranges) { List<Range<T>> inputs = new ArrayList<Range<T>>(ranges); RangeExtendedComparator<T> comparator = new RangeExtendedComparator<T>(); boolean changed; do { changed = false; for (int i = 0; i < inputs.size()-1 && !changed; i++) { Range<T> r1 = inputs.get(i); 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: case EEGG: case LEEG: case LEGG: case LLEE: case LLEG: case LLGG: inputs.remove(j); break; case EGEG: case LELE: case LELG: case LGEG: case LGLG: inputs.remove(i); break; case EGGG: case LGGG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r2.getMin(), r2.isMinIncluded(), r1.getMax(), r1.isMaxIncluded())); break; case LLLE: case LLLG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); break; } changed = true; } } } } while (changed); Collections.sort(inputs, new RangeComparator(comparator)); do { changed = false; for (int i = 0; i < inputs.size() - 1 && !changed; i++) { Range<T> r1 = inputs.get(i); if (r1.isMaxClosed()) { for (int j = i + 1; j < inputs.size() && !changed; j++) { Range<T> r2 = inputs.get(j); if (r2.isMinClosed()) { if (r1.getMax().compareTo(r2.getMin()) == 0) { inputs.remove(j); inputs.remove(i); inputs.add(i, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); changed = true; } } } } } } while (changed); return inputs; } }
RangeUtils { public static <T extends Number & Comparable> List<Range<T>> simplify(Collection<Range<T>> ranges) { List<Range<T>> inputs = new ArrayList<Range<T>>(ranges); RangeExtendedComparator<T> comparator = new RangeExtendedComparator<T>(); boolean changed; do { changed = false; for (int i = 0; i < inputs.size()-1 && !changed; i++) { Range<T> r1 = inputs.get(i); 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: case EEGG: case LEEG: case LEGG: case LLEE: case LLEG: case LLGG: inputs.remove(j); break; case EGEG: case LELE: case LELG: case LGEG: case LGLG: inputs.remove(i); break; case EGGG: case LGGG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r2.getMin(), r2.isMinIncluded(), r1.getMax(), r1.isMaxIncluded())); break; case LLLE: case LLLG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); break; } changed = true; } } } } while (changed); Collections.sort(inputs, new RangeComparator(comparator)); do { changed = false; for (int i = 0; i < inputs.size() - 1 && !changed; i++) { Range<T> r1 = inputs.get(i); if (r1.isMaxClosed()) { for (int j = i + 1; j < inputs.size() && !changed; j++) { Range<T> r2 = inputs.get(j); if (r2.isMinClosed()) { if (r1.getMax().compareTo(r2.getMin()) == 0) { inputs.remove(j); inputs.remove(i); inputs.add(i, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); changed = true; } } } } } } while (changed); return inputs; }  }
RangeUtils { public static <T extends Number & Comparable> List<Range<T>> simplify(Collection<Range<T>> ranges) { List<Range<T>> inputs = new ArrayList<Range<T>>(ranges); RangeExtendedComparator<T> comparator = new RangeExtendedComparator<T>(); boolean changed; do { changed = false; for (int i = 0; i < inputs.size()-1 && !changed; i++) { Range<T> r1 = inputs.get(i); 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: case EEGG: case LEEG: case LEGG: case LLEE: case LLEG: case LLGG: inputs.remove(j); break; case EGEG: case LELE: case LELG: case LGEG: case LGLG: inputs.remove(i); break; case EGGG: case LGGG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r2.getMin(), r2.isMinIncluded(), r1.getMax(), r1.isMaxIncluded())); break; case LLLE: case LLLG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); break; } changed = true; } } } } while (changed); Collections.sort(inputs, new RangeComparator(comparator)); do { changed = false; for (int i = 0; i < inputs.size() - 1 && !changed; i++) { Range<T> r1 = inputs.get(i); if (r1.isMaxClosed()) { for (int j = i + 1; j < inputs.size() && !changed; j++) { Range<T> r2 = inputs.get(j); if (r2.isMinClosed()) { if (r1.getMax().compareTo(r2.getMin()) == 0) { inputs.remove(j); inputs.remove(i); inputs.add(i, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); changed = true; } } } } } } while (changed); return inputs; }  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>> simplify(Collection<Range<T>> ranges) { List<Range<T>> inputs = new ArrayList<Range<T>>(ranges); RangeExtendedComparator<T> comparator = new RangeExtendedComparator<T>(); boolean changed; do { changed = false; for (int i = 0; i < inputs.size()-1 && !changed; i++) { Range<T> r1 = inputs.get(i); 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: case EEGG: case LEEG: case LEGG: case LLEE: case LLEG: case LLGG: inputs.remove(j); break; case EGEG: case LELE: case LELG: case LGEG: case LGLG: inputs.remove(i); break; case EGGG: case LGGG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r2.getMin(), r2.isMinIncluded(), r1.getMax(), r1.isMaxIncluded())); break; case LLLE: case LLLG: inputs.remove(j); inputs.remove(i); inputs.add(0, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); break; } changed = true; } } } } while (changed); Collections.sort(inputs, new RangeComparator(comparator)); do { changed = false; for (int i = 0; i < inputs.size() - 1 && !changed; i++) { Range<T> r1 = inputs.get(i); if (r1.isMaxClosed()) { for (int j = i + 1; j < inputs.size() && !changed; j++) { Range<T> r2 = inputs.get(j); if (r2.isMinClosed()) { if (r1.getMax().compareTo(r2.getMin()) == 0) { inputs.remove(j); inputs.remove(i); inputs.add(i, new Range<T>(r1.getMin(), r1.isMinIncluded(), r2.getMax(), r2.isMaxIncluded())); changed = true; } } } } } } while (changed); return inputs; }  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);  }