001: /*
002: * Copyright 2005-2007 Noelios Consulting.
003: *
004: * The contents of this file are subject to the terms of the Common Development
005: * and Distribution License (the "License"). You may not use this file except in
006: * compliance with the License.
007: *
008: * You can obtain a copy of the license at
009: * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010: * language governing permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL HEADER in each file and
013: * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014: * applicable, add the following below this CDDL HEADER, with the fields
015: * enclosed by brackets "[]" replaced with your own identifying information:
016: * Portions Copyright [yyyy] [name of copyright owner]
017: */
018:
019: package org.restlet.util;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.ListIterator;
026:
027: /**
028: * List wrapper. Modifiable list that delegates all methods to a wrapped list.
029: * This allows an easy subclassing.
030: *
031: * @author Jerome Louvel (contact@noelios.com)
032: * @see java.util.Collections
033: * @see java.util.List
034: */
035: public class WrapperList<E> implements List<E> {
036: /** The delegate list. */
037: private List<E> delegate;
038:
039: /**
040: * Constructor.
041: */
042: public WrapperList() {
043: this .delegate = null;
044: }
045:
046: /**
047: * Constructor.
048: *
049: * @param initialCapacity
050: * The initial list capacity.
051: */
052: public WrapperList(int initialCapacity) {
053: this (new ArrayList<E>(initialCapacity));
054: }
055:
056: /**
057: * Constructor.
058: *
059: * @param delegate
060: * The delegate list.
061: */
062: public WrapperList(List<E> delegate) {
063: this .delegate = delegate;
064: }
065:
066: /**
067: * Adds a element at the end of the list.
068: *
069: * @return True (as per the general contract of the Collection.add method).
070: */
071: public boolean add(E element) {
072: return getDelegate().add(element);
073: }
074:
075: /**
076: * Inserts the specified element at the specified position in this list.
077: *
078: * @param index
079: * The insertion position.
080: * @param element
081: * The element to insert.
082: */
083: public void add(int index, E element) {
084: getDelegate().add(index, element);
085: }
086:
087: /**
088: * Appends all of the elements in the specified collection to the end of
089: * this list.
090: *
091: * @param elements
092: * The collection of elements to append.
093: */
094: public boolean addAll(Collection<? extends E> elements) {
095: return getDelegate().addAll(elements);
096: }
097:
098: /**
099: * Inserts all of the elements in the specified collection into this list at
100: * the specified position.
101: *
102: * @param index
103: * The insertion position.
104: * @param elements
105: * The collection of elements to insert.
106: */
107: public boolean addAll(int index, Collection<? extends E> elements) {
108: return getDelegate().addAll(index, elements);
109: }
110:
111: /**
112: * Removes all of the elements from this list.
113: */
114: public void clear() {
115: if (this .delegate != null) {
116: getDelegate().clear();
117: this .delegate = null;
118: }
119: }
120:
121: /**
122: * Returns true if this list contains the specified element.
123: *
124: * @param element
125: * The element to find.
126: * @return True if this list contains the specified element.
127: */
128: public boolean contains(Object element) {
129: if (this .delegate != null) {
130: return this .delegate.contains(element);
131: } else {
132: return false;
133: }
134: }
135:
136: /**
137: * Returns true if this list contains all of the elements of the specified
138: * collection.
139: *
140: * @param elements
141: * The collection of elements to find.
142: * @return True if this list contains all of the elements of the specified
143: * collection.
144: */
145: public boolean containsAll(Collection<?> elements) {
146: if (this .delegate != null) {
147: return this .delegate.containsAll(elements);
148: } else {
149: return false;
150: }
151: }
152:
153: /**
154: * Compares the specified object with this list for equality.
155: *
156: * @param o
157: * The object to be compared for equality with this list.
158: * @return True if the specified object is equal to this list.
159: */
160: public boolean equals(Object o) {
161: return getDelegate().equals(o);
162: }
163:
164: /**
165: * Returns the element at the specified position in this list.
166: *
167: * @param index
168: * The element position.
169: * @return The element at the specified position in this list.
170: */
171: public E get(int index) {
172: if (this .delegate != null) {
173: return this .delegate.get(index);
174: } else {
175: throw new IndexOutOfBoundsException();
176: }
177: }
178:
179: /**
180: * Returns the delegate list.
181: *
182: * @return The delegate list.
183: */
184: protected List<E> getDelegate() {
185: if (this .delegate == null) {
186: this .delegate = new ArrayList<E>();
187: }
188:
189: return this .delegate;
190: }
191:
192: /**
193: * Returns the hash code value for this list.
194: *
195: * @return The hash code value for this list.
196: */
197: public int hashCode() {
198: return getDelegate().hashCode();
199: }
200:
201: /**
202: * Returns the index in this list of the first occurrence of the specified
203: * element, or -1 if this list does not contain this element.
204: *
205: * @param element
206: * The element to find.
207: * @return The index of the first occurrence.
208: */
209: public int indexOf(Object element) {
210: if (this .delegate != null) {
211: return this .delegate.indexOf(element);
212: } else {
213: return -1;
214: }
215: }
216:
217: /**
218: * Returns true if this list contains no elements.
219: */
220: public boolean isEmpty() {
221: if (this .delegate != null) {
222: return this .delegate.isEmpty();
223: } else {
224: return true;
225: }
226: }
227:
228: /**
229: * Returns an iterator over the elements in this list in proper sequence.
230: *
231: * @return An iterator over the elements in this list in proper sequence.
232: */
233: public Iterator<E> iterator() {
234: return getDelegate().iterator();
235: }
236:
237: /**
238: * Returns the index in this list of the last occurrence of the specified
239: * element, or -1 if this list does not contain this element.
240: */
241: public int lastIndexOf(Object element) {
242: if (this .delegate != null) {
243: return this .delegate.lastIndexOf(element);
244: } else {
245: return -1;
246: }
247: }
248:
249: /**
250: * Returns a list iterator of the elements in this list (in proper
251: * sequence).
252: *
253: * @return A list iterator of the elements in this list (in proper
254: * sequence).
255: */
256: public ListIterator<E> listIterator() {
257: return getDelegate().listIterator();
258: }
259:
260: /**
261: * Returns a list iterator of the elements in this list (in proper
262: * sequence), starting at the specified position in this list.
263: *
264: * @param index
265: * The starting position.
266: */
267: public ListIterator<E> listIterator(int index) {
268: return getDelegate().listIterator(index);
269: }
270:
271: /**
272: * Removes the element at the specified position in this list.
273: *
274: * @return The removed element.
275: */
276: public E remove(int index) {
277: if (this .delegate != null) {
278: return this .delegate.remove(index);
279: } else {
280: return null;
281: }
282: }
283:
284: /**
285: * Removes the first occurrence in this list of the specified element.
286: *
287: * @return True if the list was changed.
288: */
289: public boolean remove(Object element) {
290: if (this .delegate != null) {
291: return this .delegate.remove(element);
292: } else {
293: return false;
294: }
295: }
296:
297: /**
298: * Removes from this list all the elements that are contained in the
299: * specified collection.
300: *
301: * @param elements
302: * The collection of element to remove.
303: * @return True if the list changed.
304: */
305: public boolean removeAll(Collection<?> elements) {
306: if (this .delegate != null) {
307: return this .delegate.removeAll(elements);
308: } else {
309: return false;
310: }
311: }
312:
313: /**
314: * RemovesRetains only the elements in this list that are contained in the
315: * specified collection.
316: *
317: * @param elements
318: * The collection of element to retain.
319: * @return True if the list changed.
320: */
321: public boolean retainAll(Collection<?> elements) {
322: if (this .delegate != null) {
323: return this .delegate.retainAll(elements);
324: } else {
325: return false;
326: }
327: }
328:
329: /**
330: * Replaces the element at the specified position in this list with the
331: * specified element.
332: *
333: * @param index
334: * The position of the element to replace.
335: * @param element
336: * The new element.
337: */
338: public E set(int index, E element) {
339: if (this .delegate != null) {
340: return this .delegate.set(index, element);
341: } else {
342: throw new IndexOutOfBoundsException();
343: }
344: }
345:
346: /**
347: * Returns the number of elements in this list.
348: *
349: * @return The number of elements in this list.
350: */
351: public int size() {
352: if (this .delegate != null) {
353: return this .delegate.size();
354: } else {
355: return 0;
356: }
357: }
358:
359: /**
360: * Returns a view of the portion of this list between the specified
361: * fromIndex, inclusive, and toIndex, exclusive.
362: *
363: * @param fromIndex
364: * The start position.
365: * @param toIndex
366: * The end position (exclusive).
367: * @return The sub-list.
368: */
369: public List<E> subList(int fromIndex, int toIndex) {
370: return new WrapperList<E>(getDelegate().subList(fromIndex,
371: toIndex));
372: }
373:
374: /**
375: * Returns an array containing all of the elements in this list in proper
376: * sequence.
377: *
378: * @return An array containing all of the elements in this list in proper
379: * sequence.
380: */
381: @SuppressWarnings("unchecked")
382: public E[] toArray() {
383: return (E[]) getDelegate().toArray();
384: }
385:
386: /**
387: * Returns an array containing all of the elements in this list in proper
388: * sequence; the runtime type of the returned array is that of the specified
389: * array.
390: *
391: * @param a
392: * The sample array.
393: */
394: public <T> T[] toArray(T[] a) {
395: return getDelegate().toArray(a);
396: }
397:
398: }
|