001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.util.Collection;
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.ListIterator;
023    
024    /**
025     * <p>
026     * This is a read-only wrapper around any <code>java.util.List</code>. Query
027     * operations will "read through" to the specified list. Attempts to modify the
028     * list directly or via its iterator will result in a
029     * <code>java.lang.UnsupportedOperationException</code>.
030     * </p>
031     *
032     * @author Alexander Chow
033     */
034    public class UnmodifiableList<E> implements List<E>, Serializable {
035    
036            public UnmodifiableList(List<? extends E> list) {
037                    if (list == null) {
038                            throw new NullPointerException();
039                    }
040    
041                    _list = list;
042            }
043    
044            public boolean add(E element) {
045                    throw new UnsupportedOperationException(_MESSAGE);
046            }
047    
048            public void add(int index, E element) {
049                    throw new UnsupportedOperationException(_MESSAGE);
050            }
051    
052            public boolean addAll(Collection<? extends E> collection) {
053                    throw new UnsupportedOperationException(_MESSAGE);
054            }
055    
056            public boolean addAll(int index, Collection<? extends E> collection) {
057                    throw new UnsupportedOperationException(_MESSAGE);
058            }
059    
060            public void clear() {
061                    throw new UnsupportedOperationException(_MESSAGE);
062            }
063    
064            public boolean contains(Object object) {
065                    return _list.contains(object);
066            }
067    
068            public boolean containsAll(Collection<?> collection) {
069                    return _list.containsAll(collection);
070            }
071    
072            public boolean equals(Object object) {
073                    return _list.equals(object);
074            }
075    
076            public E get(int index) {
077                    return _list.get(index);
078            }
079    
080            public int hashCode() {
081                    return _list.hashCode();
082            }
083    
084            public int indexOf(Object object) {
085                    return _list.indexOf(object);
086            }
087    
088            public boolean isEmpty() {
089                    return _list.isEmpty();
090            }
091    
092            public Iterator<E> iterator() {
093                    return new Iterator<E>() {
094    
095                            Iterator<? extends E> itr = _list.iterator();
096    
097                            public boolean hasNext() {
098                                    return itr.hasNext();
099                            }
100    
101                            public E next() {
102                                    return itr.next();
103                            }
104    
105                            public void remove() {
106                                    throw new UnsupportedOperationException(_MESSAGE);
107                            }
108    
109                    };
110            }
111    
112            public int lastIndexOf(Object o) {
113                    return _list.lastIndexOf(o);
114            }
115    
116            public ListIterator<E> listIterator() {
117                    return listIterator(0);
118            }
119    
120            public ListIterator<E> listIterator(final int index) {
121                    return new ListIterator<E>() {
122    
123                            ListIterator<? extends E> itr = _list.listIterator(index);
124    
125                            public void add(E element) {
126                                    throw new UnsupportedOperationException(_MESSAGE);
127                            }
128    
129                            public boolean hasNext() {
130                                    return itr.hasNext();
131                            }
132    
133                            public E next() {
134                                    return itr.next();
135                            }
136    
137                            public boolean hasPrevious() {
138                                    return itr.hasPrevious();
139                            }
140    
141                            public E previous() {
142                                    return itr.previous();
143                            }
144    
145                            public int nextIndex() {
146                                    return itr.nextIndex();
147                            }
148    
149                            public int previousIndex() {
150                                    return itr.previousIndex();
151                            }
152    
153                            public void remove() {
154                                    throw new UnsupportedOperationException(_MESSAGE);
155                            }
156    
157                            public void set(E element) {
158                                    throw new UnsupportedOperationException(_MESSAGE);
159                            }
160    
161                    };
162            }
163    
164            public E remove(int index) {
165                    throw new UnsupportedOperationException(_MESSAGE);
166            }
167    
168            public boolean remove(Object object) {
169                    throw new UnsupportedOperationException(_MESSAGE);
170            }
171    
172            public boolean removeAll(Collection<?> collection) {
173                    throw new UnsupportedOperationException(_MESSAGE);
174            }
175    
176            public boolean retainAll(Collection<?> collection) {
177                    throw new UnsupportedOperationException(_MESSAGE);
178            }
179    
180            public E set(int index, E element) {
181                    throw new UnsupportedOperationException(_MESSAGE);
182            }
183    
184            public int size() {
185                    return _list.size();
186            }
187    
188            public List<E> subList(int fromIndex, int toIndex) {
189                    return new UnmodifiableList<E>(_list.subList(fromIndex, toIndex));
190            }
191    
192            public Object[] toArray() {
193                    return _list.toArray();
194            }
195    
196            public <T> T[] toArray(T[] a) {
197                    return _list.toArray(a);
198            }
199    
200            public String toString() {
201                    return _list.toString();
202            }
203    
204            private static final String _MESSAGE =
205                    "Please make a copy of this read-only list before modifying it.";
206    
207            private List<? extends E> _list;
208    
209    }