it.unimi.dsi.fastutil.objects.Object2DoubleSortedMaps Maven / Gradle / Ivy
Show all versions of fastutil Show documentation
/*
* Copyright (C) 2002-2017 Sebastiano Vigna
*
* 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 it.unimi.dsi.fastutil.objects;
import it.unimi.dsi.fastutil.objects.ObjectSortedSet;
import it.unimi.dsi.fastutil.objects.ObjectSortedSets;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
import java.util.NoSuchElementException;
/** A class providing static methods and objects that do useful things with type-specific sorted maps.
*
* @see java.util.Collections
*/
public class Object2DoubleSortedMaps {
private Object2DoubleSortedMaps() {}
/** Returns a comparator for entries based on a given comparator on keys.
*
* @param comparator a comparator on keys.
* @return the associated comparator on entries.
*/
public static Comparator super Map.Entry> entryComparator(final Comparator comparator) {
return new Comparator>() {
public int compare(Map.Entry x, Map.Entry y) {
return comparator.compare(x.getKey(), y.getKey());
}
};
}
/** An immutable class representing an empty type-specific sorted map.
*
* This class may be useful to implement your own in case you subclass
* a type-specific sorted map.
*/
public static class EmptySortedMap extends Object2DoubleMaps.EmptyMap implements Object2DoubleSortedMap , 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 > object2DoubleEntrySet() { return ObjectSortedSets.EMPTY_SET; }
/** {@inheritDoc}
* @deprecated Please use the corresponding type-specific method instead. */
@Deprecated
@Override
@SuppressWarnings("unchecked")
public ObjectSortedSet> entrySet() { return ObjectSortedSets.EMPTY_SET; }
@SuppressWarnings("unchecked")
@Override
public ObjectSortedSet keySet() { return ObjectSortedSets.EMPTY_SET; }
@SuppressWarnings("unchecked")
@Override
public Object2DoubleSortedMap subMap(final K from, final K to) { return EMPTY_MAP; }
@SuppressWarnings("unchecked")
@Override
public Object2DoubleSortedMap headMap(final K to) { return EMPTY_MAP; }
@SuppressWarnings("unchecked")
@Override
public Object2DoubleSortedMap tailMap(final K from) { return EMPTY_MAP; }
@Override
public K firstKey() { throw new NoSuchElementException(); }
@Override
public K lastKey() { throw new NoSuchElementException(); }
}
/** An empty sorted map (immutable). It is serializable and cloneable.
*/
@SuppressWarnings("rawtypes")
public static final EmptySortedMap EMPTY_MAP = new EmptySortedMap();
/** Returns an empty sorted map (immutable). It is serializable and cloneable.
*
* This method provides a typesafe access to {@link #EMPTY_MAP}.
* @return an empty sorted map (immutable).
*/
@SuppressWarnings("unchecked")
public static Object2DoubleSortedMap emptyMap() {
return EMPTY_MAP;
}
/** An immutable class representing a type-specific singleton sorted map.
*
* This class may be useful to implement your own in case you subclass
* a type-specific sorted map.
*/
public static class Singleton extends Object2DoubleMaps.Singleton implements Object2DoubleSortedMap , java.io.Serializable, Cloneable {
private static final long serialVersionUID = -7046029254386353129L;
protected final Comparator super K> comparator;
protected Singleton(final K key, final double value, Comparator super K> comparator) {
super(key, value);
this.comparator = comparator;
}
protected Singleton(final K key, final double value) {
this(key, value, null);
}
@SuppressWarnings("unchecked")
final int compare(final K k1, final K k2) {
return comparator == null ? ( ((Comparable)(k1)).compareTo(k2) ) : comparator.compare(k1, k2);
}
@Override
public Comparator super K> comparator() { return comparator; }
@SuppressWarnings("unchecked")
@Override
public ObjectSortedSet > object2DoubleEntrySet() { if (entries == null) entries = ObjectSortedSets.singleton((Object2DoubleMap.Entry )new AbstractObject2DoubleMap.BasicEntry (key, value), (Comparator super Object2DoubleMap.Entry >)entryComparator(comparator)); return (ObjectSortedSet >)entries; }
/** {@inheritDoc}
* @deprecated Please use the corresponding type-specific method instead. */
@Deprecated
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public ObjectSortedSet> entrySet() { return (ObjectSortedSet)object2DoubleEntrySet(); }
@Override
public ObjectSortedSet keySet() { if (keys == null) keys = ObjectSortedSets.singleton(key, comparator); return (ObjectSortedSet )keys; }
@SuppressWarnings("unchecked")
@Override
public Object2DoubleSortedMap 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 Object2DoubleSortedMap headMap(final K to) { if (compare(key, to) < 0) return this; return EMPTY_MAP; }
@SuppressWarnings("unchecked")
@Override
public Object2DoubleSortedMap 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; }
}
/** Returns a type-specific immutable sorted map containing only the specified pair. The returned sorted map is serializable and cloneable.
*
* Note that albeit the returned map is immutable, its default return value may be changed.
*
* @param key the only key of the returned sorted map.
* @param value the only value of the returned sorted map.
* @return a type-specific immutable sorted map containing just the pair <key,value>
.
*/
public static Object2DoubleSortedMap singleton(final K key, Double value) {
return new Singleton ((key), ((value).doubleValue()));
}
/** RETURNS a type-specific immutable sorted map containing only the specified pair. The returned sorted map is serializable and cloneable.
*
* Note that albeit the returned map is immutable, its default return value may be changed.
*
* @param key the only key of the returned sorted map.
* @param value the only value of the returned sorted map.
* @param comparator the comparator to use in the returned sorted map.
* @return a type-specific immutable sorted map containing just the pair <key,value>
.
*/
public static Object2DoubleSortedMap singleton(final K key, Double value, Comparator super K> comparator) {
return new Singleton ((key), ((value).doubleValue()), comparator);
}
/** Returns a type-specific immutable sorted map containing only the specified pair. The returned sorted map is serializable and cloneable.
*
* Note that albeit the returned map is immutable, its default return value may be changed.
*
* @param key the only key of the returned sorted map.
* @param value the only value of the returned sorted map.
* @return a type-specific immutable sorted map containing just the pair <key,value>
.
*/
public static Object2DoubleSortedMap singleton(final K key, final double value) {
return new Singleton (key, value);
}
/** Returns a type-specific immutable sorted map containing only the specified pair. The returned sorted map is serializable and cloneable.
*
* Note that albeit the returned map is immutable, its default return value may be changed.
*
* @param key the only key of the returned sorted map.
* @param value the only value of the returned sorted map.
* @param comparator the comparator to use in the returned sorted map.
* @return a type-specific immutable sorted map containing just the pair <key,value>
.
*/
public static Object2DoubleSortedMap singleton(final K key, final double value, Comparator super K> comparator) {
return new Singleton (key, value, comparator);
}
/** A synchronized wrapper class for sorted maps. */
public static class SynchronizedSortedMap extends Object2DoubleMaps.SynchronizedMap implements Object2DoubleSortedMap , java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final Object2DoubleSortedMap sortedMap;
protected SynchronizedSortedMap(final Object2DoubleSortedMap m, final Object sync) {
super(m, sync);
sortedMap = m;
}
protected SynchronizedSortedMap(final Object2DoubleSortedMap m) {
super(m);
sortedMap = m;
}
@Override
public Comparator super K> comparator() { synchronized(sync) { return sortedMap.comparator(); } }
@Override
public ObjectSortedSet > object2DoubleEntrySet() { if (entries == null) entries = ObjectSortedSets.synchronize(sortedMap.object2DoubleEntrySet(), sync); return (ObjectSortedSet >)entries; }
/** {@inheritDoc}
* @deprecated Please use the corresponding type-specific method instead. */
@Deprecated
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public ObjectSortedSet> entrySet() { return (ObjectSortedSet)object2DoubleEntrySet(); }
@Override
public ObjectSortedSet keySet() { if (keys == null) keys = ObjectSortedSets.synchronize(sortedMap.keySet(), sync); return (ObjectSortedSet )keys; }
@Override
public Object2DoubleSortedMap subMap(final K from, final K to) { return new SynchronizedSortedMap (sortedMap.subMap(from, to), sync); }
@Override
public Object2DoubleSortedMap headMap(final K to) { return new SynchronizedSortedMap (sortedMap.headMap(to), sync); }
@Override
public Object2DoubleSortedMap 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(); } }
}
/** Returns a synchronized type-specific sorted map backed by the given type-specific sorted map.
*
* @param m the sorted map to be wrapped in a synchronized sorted map.
* @return a synchronized view of the specified sorted map.
* @see java.util.Collections#synchronizedSortedMap(SortedMap)
*/
public static Object2DoubleSortedMap synchronize(final Object2DoubleSortedMap m) { return new SynchronizedSortedMap (m); }
/** Returns a synchronized type-specific sorted map backed by the given type-specific sorted map, using an assigned object to synchronize.
*
* @param m the sorted map to be wrapped in a synchronized sorted map.
* @param sync an object that will be used to synchronize the access to the sorted sorted map.
* @return a synchronized view of the specified sorted map.
* @see java.util.Collections#synchronizedSortedMap(SortedMap)
*/
public static Object2DoubleSortedMap synchronize(final Object2DoubleSortedMap m, final Object sync) { return new SynchronizedSortedMap (m, sync); }
/** An unmodifiable wrapper class for sorted maps. */
public static class UnmodifiableSortedMap extends Object2DoubleMaps.UnmodifiableMap implements Object2DoubleSortedMap , java.io.Serializable {
private static final long serialVersionUID = -7046029254386353129L;
protected final Object2DoubleSortedMap sortedMap;
protected UnmodifiableSortedMap(final Object2DoubleSortedMap m) {
super(m);
sortedMap = m;
}
@Override
public Comparator super K> comparator() { return sortedMap.comparator(); }
@Override
public ObjectSortedSet > object2DoubleEntrySet() { if (entries == null) entries = ObjectSortedSets.unmodifiable(sortedMap.object2DoubleEntrySet()); return (ObjectSortedSet >)entries; }
/** {@inheritDoc}
* @deprecated Please use the corresponding type-specific method instead. */
@Deprecated
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public ObjectSortedSet> entrySet() { return (ObjectSortedSet)object2DoubleEntrySet(); }
@Override
public ObjectSortedSet keySet() { if (keys == null) keys = ObjectSortedSets.unmodifiable(sortedMap.keySet()); return (ObjectSortedSet )keys; }
@Override
public Object2DoubleSortedMap subMap(final K from, final K to) { return new UnmodifiableSortedMap (sortedMap.subMap(from, to)); }
@Override
public Object2DoubleSortedMap headMap(final K to) { return new UnmodifiableSortedMap (sortedMap.headMap(to)); }
@Override
public Object2DoubleSortedMap tailMap(final K from) { return new UnmodifiableSortedMap (sortedMap.tailMap(from)); }
@Override
public K firstKey() { return sortedMap.firstKey(); }
@Override
public K lastKey() { return sortedMap.lastKey(); }
}
/** Returns an unmodifiable type-specific sorted map backed by the given type-specific sorted map.
*
* @param m the sorted map to be wrapped in an unmodifiable sorted map.
* @return an unmodifiable view of the specified sorted map.
* @see java.util.Collections#unmodifiableSortedMap(SortedMap)
*/
public static Object2DoubleSortedMap unmodifiable(final Object2DoubleSortedMap m) { return new UnmodifiableSortedMap (m); }
}