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