com.bbn.bue.common.collections.RangeUtils Maven / Gradle / Ivy
The newest version!
package com.bbn.bue.common.collections;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.BoundType;
import com.google.common.collect.Range;
import java.util.Iterator;
/**
* Utility methods for dealing with Guava {@link Range}s.
*/
public final class RangeUtils {
private RangeUtils() {
throw new UnsupportedOperationException();
}
/**
* Returns true iff {@code range} is bounded and closed on both sides. {@code range}
* may not be null.
*/
public static boolean isClosed(final Range> range) {
return range.hasUpperBound() && BoundType.CLOSED.equals(range.upperBoundType())
&& range.hasLowerBound() && BoundType.CLOSED.equals(range.lowerBoundType());
}
/**
* Returns the minimal range that {@link Range#encloseAll(Iterable)} {@code ranges}.
*
* You will want to do something smarter if you have many ranges. Will return {@link
* Optional#absent()} if {@code ranges} is empty.
*
* Warning: the current implementation is targeted for small numbers of ranges only. See
* https://github.com/google/guava/issues/2088
*/
public static > Optional> span(Iterable> ranges) {
final Iterator> it = ranges.iterator();
if (it.hasNext()) {
Range ret = it.next();
while (it.hasNext()) {
ret = ret.span(it.next());
}
return Optional.of(ret);
} else {
return Optional.absent();
}
}
public static > Function, T> lowerEndPointFunction() {
return new Function, T>() {
@Override
public T apply(final Range input) {
return input.lowerEndpoint();
}
};
}
public static > Function, T> upperEndPointFunction() {
return new Function, T>() {
@Override
public T apply(final Range input) {
return input.upperEndpoint();
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy