javolution.util.FastSet Maven / Gradle / Ivy
The newest version!
/*
* 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.CONSTANT;
import java.util.Set;
import javolution.lang.Realtime;
import javolution.util.function.Equalities;
import javolution.util.function.Equality;
import javolution.util.function.Predicate;
import javolution.util.internal.map.FastMapImpl;
import javolution.util.internal.set.AtomicSetImpl;
import javolution.util.internal.set.FilteredSetImpl;
import javolution.util.internal.set.SharedSetImpl;
import javolution.util.internal.set.UnmodifiableSetImpl;
import javolution.util.service.SetService;
/**
* A high-performance hash set with {@link Realtime real-time} behavior.
*
* The iteration order over the set elements is deterministic
* (unlike {@link java.util.HashSet}).It is either the insertion order (default)
* or the key order for the {@link FastSortedSet} subclass.
* This class permits {@code null} elements.
*
* @author Jean-Marie Dautelle
* @version 6.0, July 21, 2013
*/
public class FastSet extends FastCollection implements Set {
private static final long serialVersionUID = 0x600L; // Version.
/**
* Holds the actual service implementation.
*/
private final SetService service;
/**
* Creates an empty set backed up by a {@link FastMap} and having
* the same real-time characteristics.
*/
public FastSet() {
this(Equalities.STANDARD);
}
/**
* Creates an empty set backed up by a {@link FastMap} and using the
* specified comparator for key equality.
*/
public FastSet(Equality super E> comparator) {
service = new FastMapImpl(comparator, Equalities.IDENTITY)
.keySet();
}
/**
* Creates a fast set backed up by the specified service implementation.
*/
protected FastSet(SetService service) {
this.service = service;
}
////////////////////////////////////////////////////////////////////////////
// Views.
//
@Override
public FastSet atomic() {
return new FastSet(new AtomicSetImpl(service()));
}
@Override
public FastSet filtered(final Predicate super E> filter) {
return new FastSet(new FilteredSetImpl(service(), filter));
}
@Override
public FastSet shared() {
return new FastSet(new SharedSetImpl(service()));
}
@Override
public FastSet unmodifiable() {
return new FastSet(new UnmodifiableSetImpl(service()));
}
////////////////////////////////////////////////////////////////////////////
// Set operations new annotations.
//
@Override
@Realtime(limit = CONSTANT)
public boolean isEmpty() {
return size() == 0;
}
@Override
@Realtime(limit = CONSTANT)
public int size() {
return service.size();
}
@Override
@Realtime(limit = CONSTANT)
public void clear() {
service.clear();
}
@Override
@Realtime(limit = CONSTANT)
public boolean contains(Object obj) {
return service.contains(obj);
}
@Override
@Realtime(limit = CONSTANT)
public boolean remove(Object obj) {
return service.remove(obj);
}
////////////////////////////////////////////////////////////////////////////
// Misc.
//
@Override
public FastSet addAll(E... elements) {
return (FastSet) super.addAll(elements);
}
@Override
public FastSet addAll(FastCollection extends E> that) {
return (FastSet) super.addAll(that);
}
@Override
protected SetService service() {
return service;
}
}