001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014-2015 ForgeRock AS.
015 */
016
017package org.forgerock.openig.jwt.dirty;
018
019import java.util.Collection;
020import java.util.Iterator;
021
022/**
023 * A {@link Collection} decorator that notifies the provided {@link DirtyListener} when one ore more elements are
024 * removed.
025 * @param <E> type of the collection
026 */
027public class DirtyCollection<E> implements Collection<E> {
028    private final Collection<E> delegate;
029    private final DirtyListener listener;
030
031    /**
032     * Builds a new DirtyCollection delegating to the given {@literal Collection} and notifying the provided observer.
033     *
034     * @param delegate
035     *         Collection delegate
036     * @param listener
037     *         change observer
038     */
039    public DirtyCollection(final Collection<E> delegate, final DirtyListener listener) {
040        this.delegate = delegate;
041        this.listener = listener;
042    }
043
044    @Override
045    public int size() {
046        return delegate.size();
047    }
048
049    @Override
050    public boolean isEmpty() {
051        return delegate.isEmpty();
052    }
053
054    @Override
055    public boolean contains(final Object o) {
056        return delegate.contains(o);
057    }
058
059    @Override
060    public Iterator<E> iterator() {
061        return new DirtyIterator<>(delegate.iterator(), listener);
062    }
063
064    @Override
065    public Object[] toArray() {
066        return delegate.toArray();
067    }
068
069    @Override
070    public <T> T[] toArray(final T[] a) {
071        return delegate.toArray(a);
072    }
073
074    @Override
075    public boolean add(final E e) {
076        return delegate.add(e);
077    }
078
079    @Override
080    public boolean remove(final Object o) {
081        if (delegate.remove(o)) {
082            listener.onElementsRemoved();
083            return true;
084        }
085        return false;
086    }
087
088    @Override
089    public boolean containsAll(final Collection<?> c) {
090        return delegate.containsAll(c);
091    }
092
093    @Override
094    public boolean addAll(final Collection<? extends E> c) {
095        return delegate.addAll(c);
096    }
097
098    @Override
099    public boolean removeAll(final Collection<?> c) {
100        if (delegate.removeAll(c)) {
101            listener.onElementsRemoved();
102            return true;
103        }
104        return false;
105    }
106
107    @Override
108    public boolean retainAll(final Collection<?> c) {
109        if (delegate.retainAll(c)) {
110            listener.onElementsRemoved();
111            return true;
112        }
113        return false;
114    }
115
116    @Override
117    public void clear() {
118        delegate.clear();
119        listener.onElementsRemoved();
120    }
121
122    @Override
123    public boolean equals(final Object o) {
124        return delegate.equals(o);
125    }
126
127    @Override
128    public int hashCode() {
129        return delegate.hashCode();
130    }
131}