Please wait. This can take some minutes ...
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.
gwtrpc.shaded.com.google.common.collect.ImmutableRangeMap Maven / Gradle / Ivy
package com.google.common.collect;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.SortedLists.KeyAbsentBehavior;
import com.google.common.collect.SortedLists.KeyPresentBehavior;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.function.Function;
import java.util.stream.Collector;
import javax.annotation.Nullable;
@Beta
@GwtIncompatible
public class ImmutableRangeMap , V > implements RangeMap , Serializable {
private static final ImmutableRangeMap, Object> EMPTY =
new ImmutableRangeMap<>(ImmutableList.>>of(), ImmutableList.of());
@Beta
public static , V>
Collector> toImmutableRangeMap(
Function> keyFunction,
Function valueFunction) {
return CollectCollectors.toImmutableRangeMap(keyFunction, valueFunction);
}
@SuppressWarnings ("unchecked" )
public static , V> ImmutableRangeMap of () {
return (ImmutableRangeMap) EMPTY;
}
public static , V> ImmutableRangeMap of (Range range, V value) {
return new ImmutableRangeMap<>(ImmutableList.of(range), ImmutableList.of(value));
}
@SuppressWarnings ("unchecked" )
public static , V> ImmutableRangeMap copyOf (
RangeMap rangeMap) {
if (rangeMap instanceof ImmutableRangeMap) {
return (ImmutableRangeMap) rangeMap;
}
Map, ? extends V> map = rangeMap.asMapOfRanges();
ImmutableList.Builder> rangesBuilder = new ImmutableList.Builder<>(map.size());
ImmutableList.Builder valuesBuilder = new ImmutableList.Builder(map.size());
for (Entry, ? extends V> entry : map.entrySet()) {
rangesBuilder.add(entry.getKey());
valuesBuilder.add(entry.getValue());
}
return new ImmutableRangeMap<>(rangesBuilder.build(), valuesBuilder.build());
}
public static , V> Builder builder () {
return new Builder<>();
}
public static final class Builder , V > {
private final List, V>> entries;
public Builder () {
this .entries = Lists.newArrayList();
}
@CanIgnoreReturnValue
public Builder put (Range range, V value) {
checkNotNull(range);
checkNotNull(value);
checkArgument(!range.isEmpty(), "Range must not be empty, but was %s" , range);
entries.add(Maps.immutableEntry(range, value));
return this ;
}
@CanIgnoreReturnValue
public Builder putAll (RangeMap rangeMap) {
for (Entry, ? extends V> entry : rangeMap.asMapOfRanges().entrySet()) {
put(entry.getKey(), entry.getValue());
}
return this ;
}
@CanIgnoreReturnValue
Builder combine (Builder builder) {
entries.addAll(builder.entries);
return this ;
}
public ImmutableRangeMap build () {
Collections.sort(entries, Range.rangeLexOrdering().onKeys());
ImmutableList.Builder> rangesBuilder = new ImmutableList.Builder<>(entries.size());
ImmutableList.Builder valuesBuilder = new ImmutableList.Builder(entries.size());
for (int i = 0 ; i < entries.size(); i++) {
Range range = entries.get(i).getKey();
if (i > 0 ) {
Range prevRange = entries.get(i - 1 ).getKey();
if (range.isConnected(prevRange) && !range.intersection(prevRange).isEmpty()) {
throw new IllegalArgumentException(
"Overlapping ranges: range " + prevRange + " overlaps with entry " + range);
}
}
rangesBuilder.add(range);
valuesBuilder.add(entries.get(i).getValue());
}
return new ImmutableRangeMap<>(rangesBuilder.build(), valuesBuilder.build());
}
}
private final transient ImmutableList> ranges;
private final transient ImmutableList values;
ImmutableRangeMap(ImmutableList> ranges, ImmutableList values) {
this .ranges = ranges;
this .values = values;
}
@Override
@Nullable
public V get (K key) {
int index =
SortedLists.binarySearch(
ranges,
Range.lowerBoundFn(),
Cut.belowValue(key),
KeyPresentBehavior.ANY_PRESENT,
KeyAbsentBehavior.NEXT_LOWER);
if (index == -1 ) {
return null ;
} else {
Range range = ranges.get(index);
return range.contains(key) ? values.get(index) : null ;
}
}
@Override
@Nullable
public Entry, V> getEntry(K key) {
int index =
SortedLists.binarySearch(
ranges,
Range.lowerBoundFn(),
Cut.belowValue(key),
KeyPresentBehavior.ANY_PRESENT,
KeyAbsentBehavior.NEXT_LOWER);
if (index == -1 ) {
return null ;
} else {
Range range = ranges.get(index);
return range.contains(key) ? Maps.immutableEntry(range, values.get(index)) : null ;
}
}
@Override
public Range span () {
if (ranges.isEmpty()) {
throw new NoSuchElementException();
}
Range firstRange = ranges.get(0 );
Range lastRange = ranges.get(ranges.size() - 1 );
return Range.create(firstRange.lowerBound, lastRange.upperBound);
}
@Deprecated
@Override
public void put (Range range, V value) {
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public void putCoalescing (Range range, V value) {
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public void putAll (RangeMap rangeMap) {
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public void clear () {
throw new UnsupportedOperationException();
}
@Deprecated
@Override
public void remove (Range range) {
throw new UnsupportedOperationException();
}
@Override
public ImmutableMap, V> asMapOfRanges() {
if (ranges.isEmpty()) {
return ImmutableMap.of();
}
RegularImmutableSortedSet> rangeSet =
new RegularImmutableSortedSet<>(ranges, Range.rangeLexOrdering());
return new ImmutableSortedMap<>(rangeSet, values);
}
@Override
public ImmutableMap, V> asDescendingMapOfRanges() {
if (ranges.isEmpty()) {
return ImmutableMap.of();
}
RegularImmutableSortedSet> rangeSet =
new RegularImmutableSortedSet<>(ranges.reverse(), Range.rangeLexOrdering().reverse());
return new ImmutableSortedMap<>(rangeSet, values.reverse());
}
@Override
public ImmutableRangeMap subRangeMap (final Range range) {
if (checkNotNull(range).isEmpty()) {
return ImmutableRangeMap.of();
} else if (ranges.isEmpty() || range.encloses(span())) {
return this ;
}
int lowerIndex =
SortedLists.binarySearch(
ranges,
Range.upperBoundFn(),
range.lowerBound,
KeyPresentBehavior.FIRST_AFTER,
KeyAbsentBehavior.NEXT_HIGHER);
int upperIndex =
SortedLists.binarySearch(
ranges,
Range.lowerBoundFn(),
range.upperBound,
KeyPresentBehavior.ANY_PRESENT,
KeyAbsentBehavior.NEXT_HIGHER);
if (lowerIndex >= upperIndex) {
return ImmutableRangeMap.of();
}
final int off = lowerIndex;
final int len = upperIndex - lowerIndex;
ImmutableList> subRanges =
new ImmutableList>() {
@Override
public int size () {
return len;
}
@Override
public Range get (int index) {
checkElementIndex(index, len);
if (index == 0 || index == len - 1 ) {
return ranges.get(index + off).intersection(range);
} else {
return ranges.get(index + off);
}
}
@Override
boolean isPartialView () {
return true ;
}
};
final ImmutableRangeMap outer = this ;
return new ImmutableRangeMap(subRanges, values.subList(lowerIndex, upperIndex)) {
@Override
public ImmutableRangeMap subRangeMap (Range subRange) {
if (range.isConnected(subRange)) {
return outer.subRangeMap(subRange.intersection(range));
} else {
return ImmutableRangeMap.of();
}
}
};
}
@Override
public int hashCode () {
return asMapOfRanges().hashCode();
}
@Override
public boolean equals (@Nullable Object o) {
if (o instanceof RangeMap) {
RangeMap rangeMap = (RangeMap) o;
return asMapOfRanges().equals(rangeMap.asMapOfRanges());
}
return false ;
}
@Override
public String toString () {
return asMapOfRanges().toString();
}
private static class SerializedForm , V > implements Serializable {
private final ImmutableMap, V> mapOfRanges;
SerializedForm(ImmutableMap, V> mapOfRanges) {
this .mapOfRanges = mapOfRanges;
}
Object readResolve () {
if (mapOfRanges.isEmpty()) {
return of();
} else {
return createRangeMap();
}
}
Object createRangeMap () {
Builder builder = new Builder<>();
for (Entry, V> entry : mapOfRanges.entrySet()) {
builder.put(entry.getKey(), entry.getValue());
}
return builder.build();
}
private static final long serialVersionUID = 0 ;
}
Object writeReplace () {
return new SerializedForm<>(asMapOfRanges());
}
private static final long serialVersionUID = 0 ;
}