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 License.
004 *
005 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
006 * specific language governing permission and limitations under the License.
007 *
008 * When distributing Covered Software, include this CDDL Header Notice in each file and include
009 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
010 * Header, with the fields enclosed by brackets [] replaced by your own identifying
011 * information: "Portions copyright [year] [name of copyright owner]".
012 *
013 * Copyright 2015 ForgeRock AS.
014 */
015
016package org.forgerock.util;
017
018// Java SE
019import java.util.Collection;
020import java.util.Iterator;
021import java.util.List;
022import java.util.ListIterator;
023
024/**
025 * Wraps another map. All methods simply call through to the wrapped map.
026 * Subclasses are expected to override one or more of these methods.
027 *
028 * @param <E>
029 *            The type of element contained in this list.
030 */
031public class ListDecorator<E> implements List<E> {
032
033    /** The list wrapped by this decorator. */
034    protected final List<E> list;
035
036    /**
037     * Constructs a new list decorator, wrapping the specified list.
038     *
039     * @param list
040     *            the list to wrap with the decorator.
041     */
042    public ListDecorator(List<E> list) {
043        if (list == null) {
044            throw new NullPointerException();
045        }
046        this.list = list;
047    }
048
049    /**
050     * Returns the number of elements in this list.
051     */
052    @Override
053    public int size() {
054        return list.size();
055    }
056
057    /**
058     * Returns {@code true} if this list contains no elements.
059     */
060    @Override
061    public boolean isEmpty() {
062        return list.isEmpty();
063    }
064
065    /**
066     * Returns {@code true} if this list contains the specified element.
067     *
068     * @param o
069     *            the element whose presence in this list is to be tested.
070     * @return {@code true} if this list contains the specified element.
071     */
072    @Override
073    public boolean contains(Object o) {
074        return list.contains(o);
075    }
076
077    /**
078     * Returns an iterator over the elements in this list in proper sequence.
079     */
080    @Override
081    public Iterator<E> iterator() {
082        return list.iterator();
083    }
084
085    /**
086     * Returns an array containing all of the elements in this list in proper
087     * sequence (from first to last element).
088     */
089    @Override
090    public Object[] toArray() {
091        return list.toArray();
092    }
093
094    /**
095     * Returns an array containing all of the elements in this list in proper
096     * sequence (from first to last element); the runtime type of the returned
097     * array is that of the specified array. If the list fits in the specified
098     * array, it is returned therein. Otherwise, a new array is allocated with
099     * the runtime type of the specified array and the size of this list.
100     *
101     * @param a
102     *            the array into which the elements of this list are to be
103     *            stored.
104     * @return an array containing the elements of this list.
105     */
106    @Override
107    public <T> T[] toArray(T[] a) {
108        return list.toArray(a);
109    }
110
111    /**
112     * Appends the specified element to the end of this list.
113     *
114     * @param e
115     *            the element to be appended to this list.
116     * @return {@code true} if this list changed as a result of the call.
117     */
118    @Override
119    public boolean add(E e) {
120        return list.add(e);
121    }
122
123    /**
124     * Removes the first occurrence of the specified element from this list, if
125     * it is present.
126     *
127     * @param o
128     *            the element to be removed from this list, if present.
129     * @return true if this list contained the specified element.
130     */
131    @Override
132    public boolean remove(Object o) {
133        return list.remove(o);
134    }
135
136    /**
137     * Returns {@code true} if this list contains all of the elements of the
138     * specified collection.
139     *
140     * @param c
141     *            the collection to be checked for containment in this list.
142     * @return {@code true} if this list contains all of the elements of the
143     *         specified collection.
144     */
145    @Override
146    public boolean containsAll(Collection<?> c) {
147        return list.containsAll(c);
148    }
149
150    /**
151     * Appends all of the elements in the specified collection to the end of
152     * this list, in the order that they are returned by the specified
153     * collection's iterator.
154     *
155     * @param c
156     *            the collection containing elements to be added to this list.
157     * @return {@code true} if this list changed as a result of the call.
158     */
159    @Override
160    public boolean addAll(Collection<? extends E> c) {
161        return list.addAll(c);
162    }
163
164    /**
165     * Inserts all of the elements in the specified collection into this list at
166     * the specified position.
167     *
168     * @param index
169     *            the index at which to insert the first element from the
170     *            specified collection.
171     * @param c
172     *            the collection containing elements to be added to this list.
173     * @return {@code true} if this list changed as a result of the call.
174     */
175    @Override
176    public boolean addAll(int index, Collection<? extends E> c) {
177        return list.addAll(index, c);
178    }
179
180    /**
181     * Removes from this list all of its elements that are contained in the
182     * specified collection.
183     *
184     * @param c
185     *            the collection containing elements to be removed from this
186     *            list.
187     * @return {@code true} if this list changed as a result of the call.
188     */
189    @Override
190    public boolean removeAll(Collection<?> c) {
191        return list.removeAll(c);
192    }
193
194    /**
195     * Retains only the elements in this list that are contained in the
196     * specified collection.
197     *
198     * @param c
199     *            the collection containing elements to be retained in this
200     *            list.
201     * @return {@code true} if this list changed as a result of the call.
202     */
203    @Override
204    public boolean retainAll(Collection<?> c) {
205        return list.retainAll(c);
206    }
207
208    /**
209     * Removes all of the elements from this list.
210     */
211    @Override
212    public void clear() {
213        list.clear();
214    }
215
216    /**
217     * Compares the specified object with this list for equality.
218     *
219     * @param o
220     *            the object to be compared for equality with this list.
221     * @return {@code true} if the specified object is equal to this list.
222     */
223    @Override
224    public boolean equals(Object o) {
225        return list.equals(o);
226    }
227
228    /**
229     * Returns the hash code value for this list.
230     */
231    @Override
232    public int hashCode() {
233        return list.hashCode();
234    }
235
236    /**
237     * Returns the element at the specified position in this list.
238     *
239     * @param index
240     *            the index of the element to return.
241     * @return the element at the specified position in this list.
242     */
243    @Override
244    public E get(int index) {
245        return list.get(index);
246    }
247
248    /**
249     * Replaces the element at the specified position in this list with the
250     * specified element.
251     *
252     * @param index
253     *            the index of the element to replace.
254     * @param element
255     *            the element to be stored at the specified position.
256     * @return the element previously at the specified position.
257     */
258    @Override
259    public E set(int index, E element) {
260        return list.set(index, element);
261    }
262
263    /**
264     * Inserts the specified element at the specified position in this list.
265     *
266     * @param index
267     *            the index at which the specified element is to be inserted.
268     * @param element
269     *            the element to be inserted.
270     */
271    @Override
272    public void add(int index, E element) {
273        list.add(index, element);
274    }
275
276    /**
277     * Removes the element at the specified position in this list.
278     *
279     * @param index
280     *            the index of the element to be removed.
281     * @return the element previously at the specified position.
282     */
283    @Override
284    public E remove(int index) {
285        return list.remove(index);
286    }
287
288    /**
289     * Returns the index of the first occurrence of the specified element in
290     * this list, or {@code -1} if this list does not contain the element.
291     *
292     * @param o
293     *            element to search for.
294     * @return the index of the first occurrence, or {@code -1} if no such
295     *         element.
296     */
297    @Override
298    public int indexOf(Object o) {
299        return list.indexOf(o);
300    }
301
302    /**
303     * Returns the index of the last occurrence of the specified element in this
304     * list, or {@code -1} if this list does not contain the element.
305     *
306     * @param o
307     *            the element to search for.
308     * @return the index of the last occurrence, or {@code -1} if no such
309     *         element.
310     */
311    @Override
312    public int lastIndexOf(Object o) {
313        return list.lastIndexOf(o);
314    }
315
316    /**
317     * Returns a list iterator over the elements in this list (in proper
318     * sequence).
319     */
320    @Override
321    public ListIterator<E> listIterator() {
322        return list.listIterator();
323    }
324
325    /**
326     * Returns a list iterator over the elements in this list (in proper
327     * sequence), starting at the specified position in the list.
328     *
329     * @param index
330     *            the index of the first element to be returned from the list
331     *            iterator.
332     * @return a list iterator, starting at the specified position in the list.
333     */
334    @Override
335    public ListIterator<E> listIterator(int index) {
336        return list.listIterator(index);
337    }
338
339    /**
340     * Returns a view of the portion of this list between the specified
341     * fromIndex, inclusive, and toIndex, exclusive.
342     *
343     * @param fromIndex
344     *            low endpoint (inclusive) of the subList.
345     * @param toIndex
346     *            high endpoint (exclusive) of the subList.
347     * @return a view of the specified range within this list.
348     */
349    @Override
350    public List<E> subList(int fromIndex, int toIndex) {
351        return list.subList(fromIndex, toIndex);
352    }
353}