javolution.util.FastSortedSet Maven / Gradle / Ivy
/*
* 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.Collection;
import java.util.SortedSet;
import javolution.lang.Realtime;
import javolution.util.function.Equalities;
import javolution.util.function.Equality;
import javolution.util.internal.map.sorted.FastSortedMapImpl;
import javolution.util.internal.set.sorted.AtomicSortedSetImpl;
import javolution.util.internal.set.sorted.SharedSortedSetImpl;
import javolution.util.internal.set.sorted.UnmodifiableSortedSetImpl;
import javolution.util.service.SortedSetService;
/**
* A high-performance sorted set with {@link Realtime real-time} behavior.
*
* @author Jean-Marie Dautelle
* @version 6.0, July 21, 2013
*/
public class FastSortedSet extends FastSet implements SortedSet {
private static final long serialVersionUID = 0x600L; // Version.
/**
* Returns a new sorted set holding the specified elements
* (convenience method).
*/
public static FastSortedSet of(E... elements) {
FastSortedSet set = new FastSortedSet();
for (E e : elements) set.add(e);
return set;
}
/**
* Returns a new sorted set holding the same elements as the specified
* collection (convenience method).
*/
public static FastSortedSet of(Collection extends E> that) {
FastSortedSet set = new FastSortedSet();
set.addAll(that);
return set;
}
/**
* Creates an empty sorted set ordered on elements natural order.
*/
public FastSortedSet() {
this(Equalities.STANDARD);
}
/**
* Creates an empty sorted set ordered using the specified comparator.
*/
public FastSortedSet(Equality super E> comparator) {
super(new FastSortedMapImpl(comparator, Equalities.IDENTITY)
.keySet());
}
/**
* Creates a sorted set backed up by the specified service implementation.
*/
protected FastSortedSet(SortedSetService service) {
super(service);
}
////////////////////////////////////////////////////////////////////////////
// Views.
//
@Override
public FastSortedSet atomic() {
return new FastSortedSet(new AtomicSortedSetImpl(service()));
}
@Override
public FastSortedSet shared() {
return new FastSortedSet(new SharedSortedSetImpl(service()));
}
@Override
public FastSortedSet unmodifiable() {
return new FastSortedSet(new UnmodifiableSortedSetImpl(service()));
}
////////////////////////////////////////////////////////////////////////////
// Change in time limit behavior.
//
@Override
@Realtime(limit = LOG_N)
public boolean add(E e) {
return super.add(e);
}
@Override
@Realtime(limit = LOG_N)
public boolean contains(Object obj) {
return super.contains(obj);
}
@Override
@Realtime(limit = LOG_N)
public boolean remove(Object obj) {
return super.remove(obj);
}
////////////////////////////////////////////////////////////////////////////
// SortedSet Interface.
//
/** Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive. */
@Override
@Realtime(limit = LOG_N)
public FastSortedSet subSet(E fromElement, E toElement) {
return new FastSortedSet(service().subSet(fromElement, toElement));
}
/** Returns a view of the portion of this set whose elements are strictly less than toElement. */
@Override
@Realtime(limit = LOG_N)
public FastSortedSet headSet(E toElement) {
return subSet(first(), toElement);
}
/** Returns a view of the portion of this set whose elements are greater than or equal to fromElement. */
@Override
@Realtime(limit = LOG_N)
public FastSortedSet tailSet(E fromElement) {
return subSet(fromElement, last());
}
/** Returns the first (lowest) element currently in this set. */
@Override
public E first() {
return service().first();
}
/** Returns the last (highest) element currently in this set. */
@Override
public E last() {
return service().last();
}
////////////////////////////////////////////////////////////////////////////
// Misc.
//
@Override
protected SortedSetService service() {
return (SortedSetService) super.service();
}
}