@Override public void clear() { super.clear(); set.clear(); }
/** * Write the list out using a custom routine. * * @param out the output stream * @throws IOException if an error occurs while writing to the stream */ private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeObject(decorated()); }
@Override public E remove(final int index) { final E result = super.remove(index); set.remove(result); return result; }
/** * {@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) { final 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; }
/** * 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 }
@Override public ListIterator<E> listIterator() { return new SetListListIterator<>(super.listIterator(), set); }
/** * Read the list in using a custom routine. * * @param in the input stream * @throws IOException if an error occurs while reading from the stream * @throws ClassNotFoundException if an object read from the stream can not be loaded */ @SuppressWarnings("unchecked") private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); setCollection((Collection<E>) in.readObject()); }
/** * 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); } }
@Override public Iterator<E> iterator() { return new SetListIterator<>(super.iterator(), set); }
/** * Adds a collection of objects a specific index in the list avoiding * duplicates. * <p> * Only elements that are not already in this list will be added, and * duplicates from the specified collection will be ignored. * <p> * <i>(Violation)</i> The <code>List</code> interface makes the assumption * that the elements are always inserted. This may not happen with this * implementation. * * @param index the index to insert at * @param coll the collection to add in iterator order * @return true if this collection changed */ @Override public boolean addAll(final int index, final Collection<? extends E> coll) { final List<E> temp = new ArrayList<>(); for (final E e : coll) { if (set.add(e)) { temp.add(e); } } return super.addAll(index, temp); }
@Override public ListIterator<E> listIterator(final int index) { return new SetListListIterator<>(super.listIterator(index), set); }
@Override public boolean remove(final Object object) { final boolean result = set.remove(object); if (result) { super.remove(object); } return result; }