org.aksw.commons.collection.rangeset.RangeSetUnion Maven / Gradle / Ivy
package org.aksw.commons.collection.rangeset;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Set;
import org.aksw.commons.collections.SetUtils;
import org.aksw.commons.util.range.RangeUtils;
import com.google.common.collect.Iterables;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
public class RangeSetUnion>
implements RangeSet, Serializable
{
private static final long serialVersionUID = 1L;
protected RangeSet first;
protected RangeSet second;
protected transient RangeSet complement = null;
protected transient Set> asRanges = null;
protected transient Set> asDescendingSetOfRanges = null;
public RangeSetUnion(RangeSet first, RangeSet second) {
super();
this.first = first;
this.second = second;
}
/** Prefer {@link RangeSetOps#union(RangeSet, RangeSet)} */
public static > RangeSetUnion create(RangeSet first, RangeSet second) {
return new RangeSetUnion<>(first, second);
}
public RangeSet getFirst() {
return first;
}
public RangeSet getSecond() {
return second;
}
@Override
public boolean contains(T value) {
boolean result = first.contains(value) || second.contains(value);
return result;
}
@Override
public Range rangeContaining(T value) {
Range result = RangeSetUtils.getRangesBeforeAndAfter(this, value).stream().filter(range -> range.contains(value)).findFirst().orElse(null);
return result;
}
// // Get the ranges before and after the value (if any)
// Range before = Iterables.getFirst(subRangeSet(Range.atMost(value)).asDescendingSetOfRanges(), null);
// Range after = Iterables.getFirst(subRangeSet(Range.atLeast(value)).asRanges(), null);
//
// // If the value is contained in both ranges then those ranges are connected and we create the span
// Range result = null;
// if (before != null && before.contains(value)) {
// result = before;
// }
//
// if (after != null && after.contains(value)) {
// result = result == null ? after : result.span(after);
// }
// return result;
@Override
public boolean intersects(Range otherRange) {
// boolean result = !subRangeSet(otherRange).isEmpty();
boolean result = first.intersects(otherRange) || second.intersects(otherRange);
return result;
}
@Override
public boolean encloses(Range otherRange) {
T endpoint = otherRange.hasLowerBound()
? otherRange.lowerEndpoint()
: otherRange.hasUpperBound()
? otherRange.upperEndpoint()
: null;
boolean result;
if (endpoint == null) {
// Get the first range (if it exists) and check whether it covers everything
Range onlyRange = Iterables.getFirst(asRanges(), null);
result = onlyRange != null && onlyRange.encloses(otherRange);
} else {
result = RangeSetUtils.getRangesBeforeAndAfter(this, endpoint).stream().anyMatch(range -> range.encloses(otherRange));
}
return result;
}
@Override
public boolean enclosesAll(RangeSet other) {
throw new UnsupportedOperationException();
}
@Override
public boolean isEmpty() {
boolean result = first.isEmpty() && second.isEmpty();
return result;
}
@Override
public Range span() {
Range result = first.span().span(second.span());
return result;
}
@Override
public Set> asRanges() {
if (asRanges == null) {
asRanges = new AsRangesBase(
SetUtils.newForwardingSet(first::asRanges),
SetUtils.newForwardingSet(second::asRanges),
RangeUtils::compareToLowerBound);
}
return asRanges;
}
@Override
public Set> asDescendingSetOfRanges() {
if (asDescendingSetOfRanges == null) {
Comparator> cmp = RangeUtils::compareToUpperBound;
cmp = cmp.reversed();
asDescendingSetOfRanges = new AsRangesBase(
SetUtils.newForwardingSet(first::asDescendingSetOfRanges),
SetUtils.newForwardingSet(second::asDescendingSetOfRanges),
cmp);
}
return asDescendingSetOfRanges;
}
@Override
public RangeSet complement() {
if (complement == null) {
complement = new RangeSetComplement<>(this, Range.all());
}
return complement;
}
@Override
public RangeSet subRangeSet(Range view) {
return new RangeSetUnion<>(
first.subRangeSet(view),
second.subRangeSet(view));
}
@Override
public void add(Range range) {
first.add(range);
}
@Override
public void remove(Range range) {
first.remove(range);
}
@Override
public void clear() {
first.clear();
}
@Override
public void addAll(RangeSet other) {
first.addAll(other);
}
@Override
public void removeAll(RangeSet other) {
first.removeAll(other);
}
@Override
public String toString() {
return asRanges().toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy