Set implementation based on ArrayList. This class should only be used for small set. Elements cannot be null. Note that remove(Object) will shift the rest elements to the end. TODO: if necessary, optimize remove(Object) and let add(Object) add element to empty hole of the array.
| 34 | * element to empty hole of the array. |
| 35 | */ |
| 36 | public class ArraySet<E> extends AbstractSetEx<E> |
| 37 | implements Serializable { |
| 38 | |
| 39 | public static final int DEFAULT_CAPACITY = 8; |
| 40 | |
| 41 | private static final String NULL_MESSAGE = "ArraySet does not permit null element"; |
| 42 | |
| 43 | private final ArrayList<E> elements; |
| 44 | |
| 45 | private final int initialCapacity; |
| 46 | |
| 47 | private final boolean fixedCapacity; |
| 48 | |
| 49 | public ArraySet() { |
| 50 | this(DEFAULT_CAPACITY, true); |
| 51 | } |
| 52 | |
| 53 | public ArraySet(int initialCapacity) { |
| 54 | this(initialCapacity, true); |
| 55 | } |
| 56 | |
| 57 | public ArraySet(int initialCapacity, boolean fixedCapacity) { |
| 58 | this.initialCapacity = initialCapacity; |
| 59 | this.fixedCapacity = fixedCapacity; |
| 60 | elements = new ArrayList<>(initialCapacity); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Takes given array list as elements. |
| 65 | * Note that the caller should ensure that {@code elements} contains |
| 66 | * no duplicate elements. |
| 67 | */ |
| 68 | public ArraySet(ArrayList<E> elements, boolean fixedCapacity) { |
| 69 | // assert new java.util.HashSet<>(elements).size() == elements.size(); |
| 70 | this.elements = elements; |
| 71 | this.initialCapacity = elements.size(); |
| 72 | this.fixedCapacity = fixedCapacity; |
| 73 | } |
| 74 | |
| 75 | public ArraySet(Collection<? extends E> coll) { |
| 76 | this(coll.size(), false); |
| 77 | addAll(coll); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean isEmpty() { |
| 82 | return elements.isEmpty(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public boolean contains(Object o) { |
| 87 | return elements.contains(o); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | @Nonnull |
| 92 | public Object[] toArray() { |
| 93 | return elements.toArray(); |
nothing calls this directly
no outgoing calls
no test coverage detected