nl.topicus.jdbc.shaded.com.google.common.collect.GeneralRange Maven / Gradle / Ivy
Show all versions of spanner-jdbc Show documentation
/*
* Copyright (C) 2011 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in nl.topicus.jdbc.shaded.com.liance with the License. You may obtain a copy of the License at
*
* http://www.apache.nl.topicus.jdbc.shaded.org.licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkArgument;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkNotNull;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.BoundType.CLOSED;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.BoundType.OPEN;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.annotations.GwtCompatible;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Objects;
import java.nl.topicus.jdbc.shaded.io.Serializable;
import java.util.Comparator;
import nl.topicus.jdbc.shaded.javax.annotation.Nullable;
/**
* A generalized interval on any ordering, for internal use. Supports {@code null}. Unlike
* {@link Range}, this allows the use of an arbitrary nl.topicus.jdbc.shaded.com.arator. This is designed for use in the
* implementation of subcollections of sorted collection types.
*
* Whenever possible, use {@code Range} instead, which is better supported.
*
* @author Louis Wasserman
*/
@GwtCompatible(serializable = true)
final class GeneralRange implements Serializable {
/**
* Converts a Range to a GeneralRange.
*/
static GeneralRange from(Range range) {
@Nullable T lowerEndpoint = range.hasLowerBound() ? range.lowerEndpoint() : null;
BoundType lowerBoundType = range.hasLowerBound() ? range.lowerBoundType() : OPEN;
@Nullable T upperEndpoint = range.hasUpperBound() ? range.upperEndpoint() : null;
BoundType upperBoundType = range.hasUpperBound() ? range.upperBoundType() : OPEN;
return new GeneralRange(
Ordering.natural(),
range.hasLowerBound(),
lowerEndpoint,
lowerBoundType,
range.hasUpperBound(),
upperEndpoint,
upperBoundType);
}
/**
* Returns the whole range relative to the specified nl.topicus.jdbc.shaded.com.arator.
*/
static GeneralRange all(Comparator super T> nl.topicus.jdbc.shaded.com.arator) {
return new GeneralRange(nl.topicus.jdbc.shaded.com.arator, false, null, OPEN, false, null, OPEN);
}
/**
* Returns everything above the endpoint relative to the specified nl.topicus.jdbc.shaded.com.arator, with the specified
* endpoint behavior.
*/
static GeneralRange downTo(
Comparator super T> nl.topicus.jdbc.shaded.com.arator, @Nullable T endpoint, BoundType boundType) {
return new GeneralRange(nl.topicus.jdbc.shaded.com.arator, true, endpoint, boundType, false, null, OPEN);
}
/**
* Returns everything below the endpoint relative to the specified nl.topicus.jdbc.shaded.com.arator, with the specified
* endpoint behavior.
*/
static GeneralRange upTo(
Comparator super T> nl.topicus.jdbc.shaded.com.arator, @Nullable T endpoint, BoundType boundType) {
return new GeneralRange(nl.topicus.jdbc.shaded.com.arator, false, null, OPEN, true, endpoint, boundType);
}
/**
* Returns everything between the endpoints relative to the specified nl.topicus.jdbc.shaded.com.arator, with the
* specified endpoint behavior.
*/
static GeneralRange range(
Comparator super T> nl.topicus.jdbc.shaded.com.arator,
@Nullable T lower,
BoundType lowerType,
@Nullable T upper,
BoundType upperType) {
return new GeneralRange(nl.topicus.jdbc.shaded.com.arator, true, lower, lowerType, true, upper, upperType);
}
private final Comparator super T> nl.topicus.jdbc.shaded.com.arator;
private final boolean hasLowerBound;
@Nullable private final T lowerEndpoint;
private final BoundType lowerBoundType;
private final boolean hasUpperBound;
@Nullable private final T upperEndpoint;
private final BoundType upperBoundType;
private GeneralRange(
Comparator super T> nl.topicus.jdbc.shaded.com.arator,
boolean hasLowerBound,
@Nullable T lowerEndpoint,
BoundType lowerBoundType,
boolean hasUpperBound,
@Nullable T upperEndpoint,
BoundType upperBoundType) {
this.nl.topicus.jdbc.shaded.com.arator = checkNotNull(nl.topicus.jdbc.shaded.com.arator);
this.hasLowerBound = hasLowerBound;
this.hasUpperBound = hasUpperBound;
this.lowerEndpoint = lowerEndpoint;
this.lowerBoundType = checkNotNull(lowerBoundType);
this.upperEndpoint = upperEndpoint;
this.upperBoundType = checkNotNull(upperBoundType);
if (hasLowerBound) {
nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(lowerEndpoint, lowerEndpoint);
}
if (hasUpperBound) {
nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(upperEndpoint, upperEndpoint);
}
if (hasLowerBound && hasUpperBound) {
int cmp = nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(lowerEndpoint, upperEndpoint);
// be consistent with Range
checkArgument(
cmp <= 0, "lowerEndpoint (%s) > upperEndpoint (%s)", lowerEndpoint, upperEndpoint);
if (cmp == 0) {
checkArgument(lowerBoundType != OPEN | upperBoundType != OPEN);
}
}
}
Comparator super T> nl.topicus.jdbc.shaded.com.arator() {
return nl.topicus.jdbc.shaded.com.arator;
}
boolean hasLowerBound() {
return hasLowerBound;
}
boolean hasUpperBound() {
return hasUpperBound;
}
boolean isEmpty() {
return (hasUpperBound() && tooLow(getUpperEndpoint()))
|| (hasLowerBound() && tooHigh(getLowerEndpoint()));
}
boolean tooLow(@Nullable T t) {
if (!hasLowerBound()) {
return false;
}
T lbound = getLowerEndpoint();
int cmp = nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(t, lbound);
return cmp < 0 | (cmp == 0 & getLowerBoundType() == OPEN);
}
boolean tooHigh(@Nullable T t) {
if (!hasUpperBound()) {
return false;
}
T ubound = getUpperEndpoint();
int cmp = nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(t, ubound);
return cmp > 0 | (cmp == 0 & getUpperBoundType() == OPEN);
}
boolean contains(@Nullable T t) {
return !tooLow(t) && !tooHigh(t);
}
/**
* Returns the intersection of the two ranges, or an empty range if their intersection is empty.
*/
GeneralRange intersect(GeneralRange other) {
checkNotNull(other);
checkArgument(nl.topicus.jdbc.shaded.com.arator.equals(other.nl.topicus.jdbc.shaded.com.arator));
boolean hasLowBound = this.hasLowerBound;
@Nullable T lowEnd = getLowerEndpoint();
BoundType lowType = getLowerBoundType();
if (!hasLowerBound()) {
hasLowBound = other.hasLowerBound;
lowEnd = other.getLowerEndpoint();
lowType = other.getLowerBoundType();
} else if (other.hasLowerBound()) {
int cmp = nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(getLowerEndpoint(), other.getLowerEndpoint());
if (cmp < 0 || (cmp == 0 && other.getLowerBoundType() == OPEN)) {
lowEnd = other.getLowerEndpoint();
lowType = other.getLowerBoundType();
}
}
boolean hasUpBound = this.hasUpperBound;
@Nullable T upEnd = getUpperEndpoint();
BoundType upType = getUpperBoundType();
if (!hasUpperBound()) {
hasUpBound = other.hasUpperBound;
upEnd = other.getUpperEndpoint();
upType = other.getUpperBoundType();
} else if (other.hasUpperBound()) {
int cmp = nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(getUpperEndpoint(), other.getUpperEndpoint());
if (cmp > 0 || (cmp == 0 && other.getUpperBoundType() == OPEN)) {
upEnd = other.getUpperEndpoint();
upType = other.getUpperBoundType();
}
}
if (hasLowBound && hasUpBound) {
int cmp = nl.topicus.jdbc.shaded.com.arator.nl.topicus.jdbc.shaded.com.are(lowEnd, upEnd);
if (cmp > 0 || (cmp == 0 && lowType == OPEN && upType == OPEN)) {
// force allowed empty range
lowEnd = upEnd;
lowType = OPEN;
upType = CLOSED;
}
}
return new GeneralRange(nl.topicus.jdbc.shaded.com.arator, hasLowBound, lowEnd, lowType, hasUpBound, upEnd, upType);
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof GeneralRange) {
GeneralRange> r = (GeneralRange>) obj;
return nl.topicus.jdbc.shaded.com.arator.equals(r.nl.topicus.jdbc.shaded.com.arator)
&& hasLowerBound == r.hasLowerBound
&& hasUpperBound == r.hasUpperBound
&& getLowerBoundType().equals(r.getLowerBoundType())
&& getUpperBoundType().equals(r.getUpperBoundType())
&& Objects.equal(getLowerEndpoint(), r.getLowerEndpoint())
&& Objects.equal(getUpperEndpoint(), r.getUpperEndpoint());
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(
nl.topicus.jdbc.shaded.com.arator,
getLowerEndpoint(),
getLowerBoundType(),
getUpperEndpoint(),
getUpperBoundType());
}
private transient GeneralRange reverse;
/**
* Returns the same range relative to the reversed nl.topicus.jdbc.shaded.com.arator.
*/
GeneralRange reverse() {
GeneralRange result = reverse;
if (result == null) {
result =
new GeneralRange(
Ordering.from(nl.topicus.jdbc.shaded.com.arator).reverse(),
hasUpperBound,
getUpperEndpoint(),
getUpperBoundType(),
hasLowerBound,
getLowerEndpoint(),
getLowerBoundType());
result.reverse = this;
return this.reverse = result;
}
return result;
}
@Override
public String toString() {
return nl.topicus.jdbc.shaded.com.arator
+ ":"
+ (lowerBoundType == CLOSED ? '[' : '(')
+ (hasLowerBound ? lowerEndpoint : "-\u221e")
+ ','
+ (hasUpperBound ? upperEndpoint : "\u221e")
+ (upperBoundType == CLOSED ? ']' : ')');
}
T getLowerEndpoint() {
return lowerEndpoint;
}
BoundType getLowerBoundType() {
return lowerBoundType;
}
T getUpperEndpoint() {
return upperEndpoint;
}
BoundType getUpperBoundType() {
return upperBoundType;
}
}