com.facebook.presto.jdbc.internal.common.predicate.Range Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.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 com.facebook.presto.jdbc.internal.common.predicate;
import com.facebook.presto.jdbc.internal.common.function.SqlFunctionProperties;
import com.facebook.presto.jdbc.internal.common.type.Type;
import com.facebook.presto.jdbc.internal.jackson.annotation.JsonCreator;
import com.facebook.presto.jdbc.internal.jackson.annotation.JsonProperty;
import java.util.Objects;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
/**
* A Range of values across the continuous space defined by the types of the Markers
*/
public final class Range
{
private final Marker low;
private final Marker high;
@JsonCreator
public Range(
@JsonProperty("low") Marker low,
@JsonProperty("high") Marker high)
{
this(low, high, () -> {
if (low.compareTo(high) > 0) {
throw new IllegalArgumentException("low must be less than or equal to high");
}
});
}
private Range(Marker low, Marker high, Runnable extraCheck)
{
requireNonNull(low, "value is null");
requireNonNull(high, "value is null");
if (!low.getType().equals(high.getType())) {
throw new IllegalArgumentException(String.format("Marker types do not match: %s vs %s", low.getType(), high.getType()));
}
if (low.getBound() == Marker.Bound.BELOW) {
throw new IllegalArgumentException("low bound must be EXACTLY or ABOVE");
}
if (high.getBound() == Marker.Bound.ABOVE) {
throw new IllegalArgumentException("high bound must be EXACTLY or BELOW");
}
extraCheck.run();
this.low = low;
this.high = high;
}
public static Range all(Type type)
{
return new Range(Marker.lowerUnbounded(type), Marker.upperUnbounded(type));
}
public static Range greaterThan(Type type, Object low)
{
return new Range(Marker.above(type, low), Marker.upperUnbounded(type));
}
public static Range greaterThanOrEqual(Type type, Object low)
{
return new Range(Marker.exactly(type, low), Marker.upperUnbounded(type));
}
public static Range lessThan(Type type, Object high)
{
return new Range(Marker.lowerUnbounded(type), Marker.below(type, high));
}
public static Range lessThanOrEqual(Type type, Object high)
{
return new Range(Marker.lowerUnbounded(type), Marker.exactly(type, high));
}
public static Range equal(Type type, Object value)
{
return new Range(Marker.exactly(type, value), Marker.exactly(type, value));
}
public static Range range(Type type, Object low, boolean lowInclusive, Object high, boolean highInclusive)
{
Marker lowMarker = lowInclusive ? Marker.exactly(type, low) : Marker.above(type, low);
Marker highMarker = highInclusive ? Marker.exactly(type, high) : Marker.below(type, high);
return new Range(lowMarker, highMarker);
}
public Type getType()
{
return low.getType();
}
@JsonProperty
public Marker getLow()
{
return low;
}
@JsonProperty
public Marker getHigh()
{
return high;
}
public boolean isLowInclusive()
{
return low.getBound() == Marker.Bound.EXACTLY;
}
public boolean isLowUnbounded()
{
return low.isLowerUnbounded();
}
public Object getLowBoundedValue()
{
return low.getValue();
}
public Optional
© 2015 - 2024 Weber Informatics LLC | Privacy Policy