@Override public boolean add(final E object) { if (decorated().add(object)) { setOrder.add(object); return true; } return false; }
@Override public void clear() { decorated().clear(); setOrder.clear(); }
@Override public boolean remove(final Object object) { final boolean result = decorated().remove(object); if (result) { setOrder.remove(object); } return result; }
@Override public OrderedIterator<E> iterator() { return new OrderedSetIterator<>(setOrder.listIterator(), decorated()); }
/** * {@inheritDoc} * <p> * This implementation iterates over the elements of this set, checking * each element in turn to see if it's contained in <code>coll</code>. * If it's not contained, it's removed from this set. As a consequence, * it is advised to use a collection type for <code>coll</code> that provides * a fast (e.g. O(1)) implementation of {@link Collection#contains(Object)}. */ @Override public boolean retainAll(final Collection<?> coll) { final boolean result = decorated().retainAll(coll); if (result == false) { return false; } if (decorated().size() == 0) { setOrder.clear(); } else { for (final Iterator<E> it = setOrder.iterator(); it.hasNext();) { if (!decorated().contains(it.next())) { it.remove(); } } } return result; }
/** * Inserts the specified element at the specified position if it is not yet * contained in this ordered set (optional operation). Shifts the element * currently at this position and any subsequent elements to the right. * * @param index the index at which the element is to be inserted * @param object the element to be inserted * @see List#add(int, Object) */ public void add(final int index, final E object) { if (!contains(object)) { decorated().add(object); setOrder.add(index, object); } }
/** * Inserts all elements in the specified collection not yet contained in the * ordered set at the specified position (optional operation). Shifts the * element currently at the position and all subsequent elements to the * right. * * @param index the position to insert the elements * @param coll the collection containing the elements to be inserted * @return {@code true} if this ordered set changed as a result of the call * @see List#addAll(int, Collection) */ public boolean addAll(final int index, final Collection<? extends E> coll) { boolean changed = false; // collect all elements to be added for performance reasons final List<E> toAdd = new ArrayList<>(); for (final E e : coll) { if (contains(e)) { continue; } decorated().add(e); toAdd.add(e); changed = true; } if (changed) { setOrder.addAll(index, toAdd); } return changed; }