package it.unimi.dsi.fastutil.objects;
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterable;
import it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator;
import it.unimi.dsi.fastutil.objects.ObjectSortedSet;
import it.unimi.dsi.fastutil.objects.ObjectSortedSets;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceSortedMap.FastSortedEntrySet;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
import java.util.NoSuchElementException;
public final class Reference2ReferenceSortedMaps {
private Reference2ReferenceSortedMaps() {
}
public static <K> Comparator<? super Map.Entry<K, ?>> entryComparator(final Comparator<? super K> comparator) {
return (Comparator<Map.Entry<K, ?>>) (x, y) -> comparator.compare((x.getKey()), (y.getKey()));
}
@SuppressWarnings("unchecked")
public static <K, V> ObjectBidirectionalIterator<Reference2ReferenceMap.Entry<K, V>> fastIterator(
Reference2ReferenceSortedMap<K, V> map) {
final ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>> entries = map.reference2ReferenceEntrySet();
return entries instanceof Reference2ReferenceSortedMap.FastSortedEntrySet
? ((Reference2ReferenceSortedMap.FastSortedEntrySet<K, V>) entries).fastIterator()
: entries.iterator();
}
@SuppressWarnings("unchecked")
public static <K, V> ObjectBidirectionalIterable<Reference2ReferenceMap.Entry<K, V>> fastIterable(
Reference2ReferenceSortedMap<K, V> map) {
final ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>> entries = map.reference2ReferenceEntrySet();
return entries instanceof Reference2ReferenceSortedMap.FastSortedEntrySet
? ((Reference2ReferenceSortedMap.FastSortedEntrySet<K, V>) entries)::fastIterator
: entries;
}
public static class EmptySortedMap<K, V> extends Reference2ReferenceMaps.EmptyMap<K, V>
implements
Reference2ReferenceSortedMap<K, V>,
java.io.Serializable,
Cloneable {
private static final long serialVersionUID = -7046029254386353129L;
protected EmptySortedMap() {
}
@Override
public Comparator<? super K> comparator() {
return null;
}
@SuppressWarnings("unchecked")
@Override
public ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>> reference2ReferenceEntrySet() {
return ObjectSortedSets.EMPTY_SET;
}
@Override
@SuppressWarnings("unchecked")
public ObjectSortedSet<Map.Entry<K, V>> entrySet() {
return ObjectSortedSets.EMPTY_SET;
}
@SuppressWarnings("unchecked")
@Override
public ReferenceSortedSet<K> keySet() {
return ReferenceSortedSets.EMPTY_SET;
}
@SuppressWarnings("unchecked")
@Override
public Reference2ReferenceSortedMap<K, V> subMap(final K from, final K to) {
return EMPTY_MAP;
}
@SuppressWarnings("unchecked")
@Override
public Reference2ReferenceSortedMap<K, V> headMap(final K to) {
return EMPTY_MAP;
}
@SuppressWarnings("unchecked")
@Override
public Reference2ReferenceSortedMap<K, V> tailMap(final K from) {
return EMPTY_MAP;
}
@Override
public K firstKey() {
throw new NoSuchElementException();
}
@Override
public K lastKey() {
throw new NoSuchElementException();
}
}
@SuppressWarnings("rawtypes")
public static final EmptySortedMap EMPTY_MAP = new EmptySortedMap();
@SuppressWarnings("unchecked")
public static <K, V> Reference2ReferenceSortedMap<K, V> emptyMap() {
return EMPTY_MAP;
}
public static class Singleton<K, V> extends Reference2ReferenceMaps.Singleton<K, V>
implements
Reference2ReferenceSortedMap<K, V>,
java.io.Serializable,
Cloneable {
private static final long serialVersionUID = -7046029254386353129L;
protected final Comparator<? super K> comparator;
protected Singleton(final K key, final V value, Comparator<? super K> comparator) {
super(key, value);
this.comparator = comparator;
}
protected Singleton(final K key, final V value) {
this(key, value, null);
}
@SuppressWarnings("unchecked")
final int compare(final K k1, final K k2) {
return comparator == null ? (((Comparable<K>) (k1)).compareTo(k2)) : comparator.compare(k1, k2);
}
@Override
public Comparator<? super K> comparator() {
return comparator;
}
@SuppressWarnings("unchecked")
@Override
public ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>> reference2ReferenceEntrySet() {
if (entries == null)
entries = ObjectSortedSets.singleton(new AbstractReference2ReferenceMap.BasicEntry<>(key, value),
entryComparator(comparator));
return (ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>>) entries;
}
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public ObjectSortedSet<Map.Entry<K, V>> entrySet() {
return (ObjectSortedSet) reference2ReferenceEntrySet();
}
@Override
public ReferenceSortedSet<K> keySet() {
if (keys == null)
keys = ReferenceSortedSets.singleton(key, comparator);
return (ReferenceSortedSet<K>) keys;
}
@SuppressWarnings("unchecked")
@Override
public Reference2ReferenceSortedMap<K, V> subMap(final K from, final K to) {
if (compare(from, key) <= 0 && compare(key, to) < 0)
return this;
return EMPTY_MAP;
}
@SuppressWarnings("unchecked")
@Override
public Reference2ReferenceSortedMap<K, V> headMap(final K to) {
if (compare(key, to) < 0)
return this;
return EMPTY_MAP;
}
@SuppressWarnings("unchecked")
@Override
public Reference2ReferenceSortedMap<K, V> tailMap(final K from) {
if (compare(from, key) <= 0)
return this;
return EMPTY_MAP;
}
@Override
public K firstKey() {
return key;
}
@Override
public K lastKey() {
return key;
}
}
public static <K, V> Reference2ReferenceSortedMap<K, V> singleton(final K key, V value) {
return new Singleton<>((key), (value));
}
public static <K, V> Reference2ReferenceSortedMap<K, V> singleton(final K key, V value,
Comparator<? super K> comparator) {
return new Singleton<>((key), (value), comparator);
}
public static class SynchronizedSortedMap<K, V> extends Reference2ReferenceMaps.SynchronizedMap<K, V>
implements
Reference2ReferenceSortedMap<K, V>,
java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final Reference2ReferenceSortedMap<K, V> sortedMap;
protected SynchronizedSortedMap(final Reference2ReferenceSortedMap<K, V> m, final Object sync) {
super(m, sync);
sortedMap = m;
}
protected SynchronizedSortedMap(final Reference2ReferenceSortedMap<K, V> m) {
super(m);
sortedMap = m;
}
@Override
public Comparator<? super K> comparator() {
synchronized (sync) {
return sortedMap.comparator();
}
}
@Override
public ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>> reference2ReferenceEntrySet() {
if (entries == null)
entries = ObjectSortedSets.synchronize(sortedMap.reference2ReferenceEntrySet(), sync);
return (ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>>) entries;
}
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public ObjectSortedSet<Map.Entry<K, V>> entrySet() {
return (ObjectSortedSet) reference2ReferenceEntrySet();
}
@Override
public ReferenceSortedSet<K> keySet() {
if (keys == null)
keys = ReferenceSortedSets.synchronize(sortedMap.keySet(), sync);
return (ReferenceSortedSet<K>) keys;
}
@Override
public Reference2ReferenceSortedMap<K, V> subMap(final K from, final K to) {
return new SynchronizedSortedMap<>(sortedMap.subMap(from, to), sync);
}
@Override
public Reference2ReferenceSortedMap<K, V> headMap(final K to) {
return new SynchronizedSortedMap<>(sortedMap.headMap(to), sync);
}
@Override
public Reference2ReferenceSortedMap<K, V> tailMap(final K from) {
return new SynchronizedSortedMap<>(sortedMap.tailMap(from), sync);
}
@Override
public K firstKey() {
synchronized (sync) {
return sortedMap.firstKey();
}
}
@Override
public K lastKey() {
synchronized (sync) {
return sortedMap.lastKey();
}
}
}
public static <K, V> Reference2ReferenceSortedMap<K, V> synchronize(final Reference2ReferenceSortedMap<K, V> m) {
return new SynchronizedSortedMap<>(m);
}
public static <K, V> Reference2ReferenceSortedMap<K, V> synchronize(final Reference2ReferenceSortedMap<K, V> m,
final Object sync) {
return new SynchronizedSortedMap<>(m, sync);
}
public static class UnmodifiableSortedMap<K, V> extends Reference2ReferenceMaps.UnmodifiableMap<K, V>
implements
Reference2ReferenceSortedMap<K, V>,
java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final Reference2ReferenceSortedMap<K, V> sortedMap;
protected UnmodifiableSortedMap(final Reference2ReferenceSortedMap<K, V> m) {
super(m);
sortedMap = m;
}
@Override
public Comparator<? super K> comparator() {
return sortedMap.comparator();
}
@Override
public ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>> reference2ReferenceEntrySet() {
if (entries == null)
entries = ObjectSortedSets.unmodifiable(sortedMap.reference2ReferenceEntrySet());
return (ObjectSortedSet<Reference2ReferenceMap.Entry<K, V>>) entries;
}
@Override
@SuppressWarnings({"rawtypes", "unchecked"})
public ObjectSortedSet<Map.Entry<K, V>> entrySet() {
return (ObjectSortedSet) reference2ReferenceEntrySet();
}
@Override
public ReferenceSortedSet<K> keySet() {
if (keys == null)
keys = ReferenceSortedSets.unmodifiable(sortedMap.keySet());
return (ReferenceSortedSet<K>) keys;
}
@Override
public Reference2ReferenceSortedMap<K, V> subMap(final K from, final K to) {
return new UnmodifiableSortedMap<>(sortedMap.subMap(from, to));
}
@Override
public Reference2ReferenceSortedMap<K, V> headMap(final K to) {
return new UnmodifiableSortedMap<>(sortedMap.headMap(to));
}
@Override
public Reference2ReferenceSortedMap<K, V> tailMap(final K from) {
return new UnmodifiableSortedMap<>(sortedMap.tailMap(from));
}
@Override
public K firstKey() {
return sortedMap.firstKey();
}
@Override
public K lastKey() {
return sortedMap.lastKey();
}
}
public static <K, V> Reference2ReferenceSortedMap<K, V> unmodifiable(final Reference2ReferenceSortedMap<K, V> m) {
return new UnmodifiableSortedMap<>(m);
}
}