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 2010–2011 ApexIdentity Inc.
015 * Portions Copyright 2011-2014 ForgeRock AS.
016 */
017
018package org.forgerock.openig.util;
019
020import java.util.Collection;
021import java.util.Iterator;
022import java.util.Set;
023
024/**
025 * Contains another set, which is uses as its basic source of data, possibly transforming the
026 * data along the way. This class itself simply overrides all methods of {@link Set} with
027 * versions that pass all requests to the contained set. Subclasses may further override
028 * some of these methods and may also provide additional methods and fields.
029 *
030 * @param <E>
031 *            The type of the set decorator.
032 */
033public class SetDecorator<E> implements Set<E> {
034
035    /** The set wrapped by this decorator. */
036    protected final Set<E> set;
037
038    /**
039     * Constructs a new set decorator, wrapping the specified set.
040     *
041     * @param set the set to wrap with the decorator.
042     */
043    public SetDecorator(Set<E> set) {
044        if (set == null) {
045            throw new NullPointerException();
046        }
047        this.set = set;
048    }
049
050    @Override
051    public int size() {
052        return set.size();
053    }
054
055    @Override
056    public boolean isEmpty() {
057        return set.isEmpty();
058    }
059
060    /**
061     * Returns {@code true} if the set contains the specified element.
062     *
063     * @param o element whose presence in the set is to be tested.
064     * @return {@code true} if the set contains the specified element.
065     * @throws ClassCastException if the type of the specified element is incompatible with the set (optional).
066     * @throws NullPointerException if the specified element is {@code null} and the set does not permit null
067     * elements (optional).
068     */
069    @Override
070    public boolean contains(Object o) {
071        return set.contains(o);
072    }
073
074    @Override
075    public Iterator<E> iterator() {
076        return set.iterator();
077    }
078
079    @Override
080    public Object[] toArray() {
081        return set.toArray();
082    }
083
084    /**
085     * Returns an array containing all of the elements in the set; the runtime type of the
086     * returned array is that of the specified array.
087     *
088     * @param a the array into which the elements of the set are to be stored, if it is big enough; otherwise, a new
089     * array of the same runtime type is allocated for this purpose.
090     * @param <T> runtime type of the array
091     * @return an array containing all the elements in the set.
092     * @throws ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type
093     * of every element in the set.
094     * @throws NullPointerException if the specified array is {@code null}.
095     */
096    @Override
097    public <T> T[] toArray(T[] a) {
098        return set.toArray(a);
099    }
100
101    /**
102     * Adds the specified element to the set if it is not already present (optional
103     * operation).
104     *
105     * @param e element to be added to the set.
106     * @return {@code true} if the set did not already contain the specified element.
107     * @throws UnsupportedOperationException if the {@code add} operation is not supported by the set.
108     * @throws ClassCastException if the class of the specified element prevents it from being added to the set.
109     * @throws NullPointerException if the specified element is {@code null} and the set does not permit null elements.
110     * @throws IllegalArgumentException if some property of the specified element prevents it from being added to
111     * the set.
112     */
113    @Override
114    public boolean add(E e) {
115        return set.add(e);
116    }
117
118    /**
119     * Removes the specified element from the set if it is present (optional operation).
120     *
121     * @param o object to be removed from the set, if present.
122     * @return {@code true} if the set contained the specified element.
123     * @throws ClassCastException if the type of the specified element is incompatible with the set (optional).
124     * @throws NullPointerException if the specified element is {@code null} and the set does not permit null
125     * elements (optional).
126     * @throws UnsupportedOperationException if the {@code remove} operation is not supported by the set.
127     */
128    @Override
129    public boolean remove(Object o) {
130        return set.remove(o);
131    }
132
133    /**
134     * Returns {@code true} if the set contains all of the elements of the specified
135     * collection.
136     *
137     * @param c collection to be checked for containment in the set.
138     * @return {@code true} if the set contains all of the elements of the specified collection.
139     * @throws ClassCastException if the types of one or more elements in the specified collection are incompatible
140     * with the set (optional).
141     * @throws NullPointerException if the specified collection contains one or more {@code null} elements and the
142     * set does not permit null elements (optional), or if the specified collection is {@code null}.
143     */
144    @Override
145    public boolean containsAll(Collection<?> c) {
146        return set.containsAll(c);
147    }
148
149    /**
150     * Adds all of the elements in the specified collection to the set if they're not already
151     * present (optional operation).
152     *
153     * @param c collection containing elements to be added to the set.
154     * @return {@code true} if the set changed as a result of the call.
155     * @throws UnsupportedOperationException if the {@code addAll} operation is not supported by the set.
156     * @throws ClassCastException if the class of an element of the specified collection prevents it from being
157     * added to the set.
158     * @throws NullPointerException if the specified collection contains one or more {@code null} elements and the
159     * set does not permit null elements, or if the specified collection is {@code null}.
160     * @throws IllegalArgumentException if some property of an element of the specified collection prevents it from
161     * being added to the set.
162     */
163    @Override
164    public boolean addAll(Collection<? extends E> c) {
165        return set.addAll(c);
166    }
167
168    /**
169     * Retains only the elements in the set that are contained in the specified collection
170     * (optional operation).
171     *
172     * @param c collection containing elements to be retained in the set.
173     * @return {@code true} if the set changed as a result of the call.
174     * @throws UnsupportedOperationException if the retainAll operation is not supported by the set.
175     * @throws ClassCastException if the class of an element of the set is incompatible with the specified
176     * collection (optional).
177     * @throws NullPointerException if the set contains a {@code null} element and the specified collection does
178     * not permit null elements (optional), or if the specified collection is {@code null}.
179     */
180    @Override
181    public boolean retainAll(Collection<?> c) {
182        return set.retainAll(c);
183    }
184
185    /**
186     * Removes from the set all of its elements that are contained in the specified collection
187     * (optional operation).
188     *
189     * @param c collection containing elements to be removed from the set.
190     * @return {@code true} if the set changed as a result of the call.
191     * @throws UnsupportedOperationException if the {@code removeAll} operation is not supported by the set.
192     * @throws ClassCastException if the class of an element of the set is incompatible with the specified
193     * collection (optional).
194     * @throws NullPointerException if the set contains a {@code null} element and the specified collection does
195     * not permit null elements (optional), or if the specified collection is {@code null}.
196     */
197    @Override
198    public boolean removeAll(Collection<?> c) {
199        return set.removeAll(c);
200    }
201
202    /**
203     * Removes all of the elements from the set (optional operation).
204     *
205     * @throws UnsupportedOperationException if the {@code clear} method is not supported by the set.
206     */
207    @Override
208    public void clear() {
209        set.clear();
210    }
211
212    /**
213     * Compares the specified object with the set for equality.
214     *
215     * @param o object to be compared for equality with the set.
216     * @return {@code true} if the specified object is equal to the set.
217     */
218    @Override
219    public boolean equals(Object o) {
220        return set.equals(o);
221    }
222
223    @Override
224    public int hashCode() {
225        return set.hashCode();
226    }
227}