Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 io.prestosql.spi.predicate;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.type.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import static io.prestosql.spi.type.TypeUtils.isFloatingPointNaN;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toUnmodifiableList;
/**
* A set containing zero or more Ranges of the same type over a continuous space of possible values.
* Ranges are coalesced into the most compact representation of non-overlapping Ranges. This structure
* allows iteration across these compacted Ranges in increasing order, as well as other common
* set-related operation.
*/
public final class SortedRangeSet
implements ValueSet
{
private final Type type;
private final NavigableMap lowIndexedRanges;
private SortedRangeSet(Type type, NavigableMap lowIndexedRanges)
{
requireNonNull(type, "type is null");
requireNonNull(lowIndexedRanges, "lowIndexedRanges is null");
if (!type.isOrderable()) {
throw new IllegalArgumentException("Type is not orderable: " + type);
}
this.type = type;
this.lowIndexedRanges = lowIndexedRanges;
}
static SortedRangeSet none(Type type)
{
return copyOf(type, Collections.emptyList());
}
static SortedRangeSet all(Type type)
{
return copyOf(type, Collections.singletonList(Range.all(type)));
}
/**
* Provided discrete values that are unioned together to form the SortedRangeSet
*/
static SortedRangeSet of(Type type, Object first, Object... rest)
{
List ranges = new ArrayList<>(rest.length + 1);
ranges.add(Range.equal(type, first));
for (Object value : rest) {
ranges.add(Range.equal(type, value));
}
return copyOf(type, ranges);
}
/**
* Provided Ranges are unioned together to form the SortedRangeSet
*/
static SortedRangeSet of(Range first, Range... rest)
{
List rangeList = new ArrayList<>(rest.length + 1);
rangeList.add(first);
rangeList.addAll(asList(rest));
return copyOf(first.getType(), rangeList);
}
/**
* Provided Ranges are unioned together to form the SortedRangeSet
*/
static SortedRangeSet copyOf(Type type, Iterable ranges)
{
return new Builder(type).addAll(ranges).build();
}
@JsonCreator
public static SortedRangeSet copyOf(
@JsonProperty("type") Type type,
@JsonProperty("ranges") List ranges)
{
return copyOf(type, (Iterable) ranges);
}
@Override
@JsonProperty
public Type getType()
{
return type;
}
@JsonProperty("ranges")
public List getOrderedRanges()
{
return new ArrayList<>(lowIndexedRanges.values());
}
public int getRangeCount()
{
return lowIndexedRanges.size();
}
@Override
public boolean isNone()
{
return lowIndexedRanges.isEmpty();
}
@Override
public boolean isAll()
{
return lowIndexedRanges.size() == 1 && lowIndexedRanges.values().iterator().next().isAll();
}
@Override
public boolean isSingleValue()
{
return lowIndexedRanges.size() == 1 && lowIndexedRanges.values().iterator().next().isSingleValue();
}
@Override
public Object getSingleValue()
{
if (!isSingleValue()) {
throw new IllegalStateException("SortedRangeSet does not have just a single value");
}
return lowIndexedRanges.values().iterator().next().getSingleValue();
}
@Override
public boolean isDiscreteSet()
{
for (Range range : lowIndexedRanges.values()) {
if (!range.isSingleValue()) {
return false;
}
}
return !isNone();
}
@Override
public List