/** * Given an array of slices (potentially overlapping and in any order) and return an equivalent * array of non-overlapping slices in clustering order. * * @param slices an array of slices. This may be modified by this method. * @return the smallest possible array of non-overlapping slices in clustering order. If the * original slices are already non-overlapping and in comparator order, this may or may not * return the provided slices directly. */ private List<Slice> normalize(List<Slice> slices) { if (slices.size() <= 1) return slices; Collections.sort( slices, new Comparator<Slice>() { @Override public int compare(Slice s1, Slice s2) { int c = comparator.compare(s1.start(), s2.start()); if (c != 0) return c; return comparator.compare(s1.end(), s2.end()); } }); List<Slice> slicesCopy = new ArrayList<>(slices.size()); Slice last = slices.get(0); for (int i = 1; i < slices.size(); i++) { Slice s2 = slices.get(i); boolean includesStart = last.includes(comparator, s2.start()); boolean includesFinish = last.includes(comparator, s2.end()); if (includesStart && includesFinish) continue; if (!includesStart && !includesFinish) { slicesCopy.add(last); last = s2; continue; } if (includesStart) { last = Slice.make(last.start(), s2.end()); continue; } assert !includesFinish; } slicesCopy.add(last); return slicesCopy; }
public Builder add(Slice.Bound start, Slice.Bound end) { return add(Slice.make(start, end)); }