javolution.util.FastSortedMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javolution-core-java-msftbx Show documentation
Show all versions of javolution-core-java-msftbx Show documentation
Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.util;
import static javolution.lang.Realtime.Limit.LOG_N;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
import javolution.lang.Realtime;
import javolution.util.function.Equalities;
import javolution.util.function.Equality;
import javolution.util.internal.map.sorted.AtomicSortedMapImpl;
import javolution.util.internal.map.sorted.FastSortedMapImpl;
import javolution.util.internal.map.sorted.SharedSortedMapImpl;
import javolution.util.internal.map.sorted.UnmodifiableSortedMapImpl;
import javolution.util.service.SortedMapService;
/**
* A high-performance sorted map with {@link Realtime real-time} behavior.
*
* This map provides a total ordering based on the keys natural order or
* using custom {@link #FastSortedMap(Equality) comparators}.
*
* @author Jean-Marie Dautelle
* @version 6.0, July 21, 2013
*/
public class FastSortedMap extends FastMap implements
SortedMap {
private static final long serialVersionUID = 0x600L; // Version.
/**
* Returns a new sorted map holding the same entries as the specified
* map (convenience method).
*/
public static FastSortedMap of(Map extends K, ? extends V> that) {
FastSortedMap map = new FastSortedMap();
map.putAll(that);
return map;
}
/**
* Creates an empty sorted map ordered on keys natural order.
*/
public FastSortedMap() {
this(Equalities.STANDARD);
}
/**
* Creates an empty sorted map ordered using the specified comparator
* for order.
*/
public FastSortedMap(Equality super K> keyComparator) {
this(keyComparator, Equalities.STANDARD);
}
/**
* Creates an empty sorted map ordered using the specified key comparator
* for order and value comparator for values equality.
*/
public FastSortedMap(Equality super K> keyComparator,
Equality super V> valueComparator) {
super(new FastSortedMapImpl(keyComparator, valueComparator));
}
/**
* Creates a sorted map backed up by the specified service implementation.
*/
protected FastSortedMap(SortedMapService service) {
super(service);
}
////////////////////////////////////////////////////////////////////////////
// Views.
//
@Override
public FastSortedMap atomic() {
return new FastSortedMap(new AtomicSortedMapImpl(service()));
}
@Override
public FastSortedMap shared() {
return new FastSortedMap(new SharedSortedMapImpl(service()));
}
@Override
public FastSortedMap unmodifiable() {
return new FastSortedMap(new UnmodifiableSortedMapImpl(
service()));
}
@Override
public FastSortedSet> entrySet() {
return new FastSortedSet>(service().entrySet());
}
@Override
public FastSortedSet keySet() {
return new FastSortedSet(service().keySet());
}
/** Returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. */
@Override
public FastSortedMap subMap(K fromKey, K toKey) {
return new FastSortedMap(service().subMap(fromKey, toKey));
}
/** Returns a view of the portion of this map whose keys are strictly less than toKey. */
@Override
public FastSortedMap headMap(K toKey) {
return new FastSortedMap(service().subMap(firstKey(), toKey));
}
/** Returns a view of the portion of this map whose keys are greater than or equal to fromKey. */
@Override
public FastSortedMap tailMap(K fromKey) {
return new FastSortedMap(service().subMap(fromKey, lastKey()));
}
////////////////////////////////////////////////////////////////////////////
// Change in time limit behavior.
//
@Override
@Realtime(limit = LOG_N)
public boolean containsKey(Object key) {
return super.containsKey(key);
}
@Override
@Realtime(limit = LOG_N)
public V get(Object key) {
return super.get(key);
}
@Override
@Realtime(limit = LOG_N)
public V put(K key, V value) {
return super.put(key, value);
}
@Override
@Realtime(limit = LOG_N)
public V remove(Object key) {
return super.remove(key);
}
@Override
@Realtime(limit = LOG_N)
public V putIfAbsent(K key, V value) {
return super.putIfAbsent(key, value);
}
@Override
@Realtime(limit = LOG_N)
public boolean remove(Object key, Object value) {
return super.remove(key, value);
}
@Override
@Realtime(limit = LOG_N)
public boolean replace(K key, V oldValue, V newValue) {
return super.replace(key, oldValue, newValue);
}
@Override
@Realtime(limit = LOG_N)
public V replace(K key, V value) {
return super.replace(key, value);
}
////////////////////////////////////////////////////////////////////////////
// SortedMap Interface.
//
/** Returns the first (lowest) key currently in this map. */
@Override
public K firstKey() {
return service().firstKey();
}
/** Returns the last (highest) key currently in this map. */
@Override
public K lastKey() {
return service().lastKey();
}
/** Returns the comparator used to order the keys in this map (never null). */
@Override
public Comparator super K> comparator() {
return keySet().comparator();
}
////////////////////////////////////////////////////////////////////////////
// Misc.
//
@Override
public FastSortedMap add(K key, V value) {
return (FastSortedMap) super.add(key, value);
}
@Override
protected SortedMapService service() {
return (SortedMapService) super.service();
}
}