/** * {@inheritDoc} * * <p>This implementation iterates over the elements of this list, 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 * list. 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) { boolean result = set.retainAll(coll); if (result == false) { return false; } if (set.size() == 0) { super.clear(); } else { // use the set as parameter for the call to retainAll to improve performance super.retainAll(set); } return result; }
/** * Adds an element to a specific index in the list if it is not already present. * * <p><i>(Violation)</i> The <code>List</code> interface makes the assumption that the element is * always inserted. This may not happen with this implementation. * * @param index the index to insert at * @param object the object to add */ public void add(int index, Object object) { // adds element if it is not contained already if (set.contains(object) == false) { super.add(index, object); set.add(object); } }
@Override public boolean remove(final Object object) { final boolean result = set.remove(object); if (result) { super.remove(object); } return result; }
/** * Adds an element to a specific index in the list if it is not already present. * * <p><i>(Violation)</i> The <code>List</code> interface makes the assumption that the element is * always inserted. This may not happen with this implementation. * * @param index the index to insert at * @param object the object to add */ @Override public void add(final int index, final E object) { // adds element if it is not contained already if (set.contains(object) == false) { super.add(index, object); set.add(object); } }
/** * Sets the value at the specified index avoiding duplicates. * * <p>The object is set into the specified index. Afterwards, any previous duplicate is removed If * the object is not already in the list then a normal set occurs. If it is present, then the old * version is removed. * * @param index the index to insert at * @param object the object to set * @return the previous object */ public Object set(int index, Object object) { int pos = indexOf(object); Object removed = super.set(index, object); if (pos == -1 || pos == index) { return removed; } // the object is already in the uniq list // (and it hasn't been swapped with itself) super.remove(pos); // remove the duplicate by index set.remove(removed); // remove the item deleted by the set return removed; // return the item deleted by the set }
/** * Sets the value at the specified index avoiding duplicates. * * <p>The object is set into the specified index. Afterwards, any previous duplicate is removed. * If the object is not already in the list then a normal set occurs. If it is present, then the * old version is removed. * * @param index the index to insert at * @param object the object to set * @return the previous object */ @Override public E set(final int index, final E object) { final int pos = indexOf(object); final E removed = super.set(index, object); if (pos != -1 && pos != index) { // the object is already in the unique list // (and it hasn't been swapped with itself) super.remove(pos); // remove the duplicate by index } set.remove(removed); // remove the item deleted by the set set.add(object); // add the new item to the unique set return removed; // return the item deleted by the set }
public void clear() { super.clear(); set.clear(); }
@Override public void clear() { super.clear(); set.clear(); }