001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            @Override
073            public boolean equals(Object object) {
074                    return _list.equals(object);
075            }
076    
077            public E get(int index) {
078                    return _list.get(index);
079            }
080    
081            @Override
082            public int hashCode() {
083                    return _list.hashCode();
084            }
085    
086            public int indexOf(Object object) {
087                    return _list.indexOf(object);
088            }
089    
090            public boolean isEmpty() {
091                    return _list.isEmpty();
092            }
093    
094            public Iterator<E> iterator() {
095                    return new Iterator<E>() {
096    
097                            Iterator<? extends E> itr = _list.iterator();
098    
099                            public boolean hasNext() {
100                                    return itr.hasNext();
101                            }
102    
103                            public E next() {
104                                    return itr.next();
105                            }
106    
107                            public void remove() {
108                                    throw new UnsupportedOperationException(_MESSAGE);
109                            }
110    
111                    };
112            }
113    
114            public int lastIndexOf(Object o) {
115                    return _list.lastIndexOf(o);
116            }
117    
118            public ListIterator<E> listIterator() {
119                    return listIterator(0);
120            }
121    
122            public ListIterator<E> listIterator(final int index) {
123                    return new ListIterator<E>() {
124    
125                            ListIterator<? extends E> itr = _list.listIterator(index);
126    
127                            public void add(E element) {
128                                    throw new UnsupportedOperationException(_MESSAGE);
129                            }
130    
131                            public boolean hasNext() {
132                                    return itr.hasNext();
133                            }
134    
135                            public E next() {
136                                    return itr.next();
137                            }
138    
139                            public boolean hasPrevious() {
140                                    return itr.hasPrevious();
141                            }
142    
143                            public E previous() {
144                                    return itr.previous();
145                            }
146    
147                            public int nextIndex() {
148                                    return itr.nextIndex();
149                            }
150    
151                            public int previousIndex() {
152                                    return itr.previousIndex();
153                            }
154    
155                            public void remove() {
156                                    throw new UnsupportedOperationException(_MESSAGE);
157                            }
158    
159                            public void set(E element) {
160                                    throw new UnsupportedOperationException(_MESSAGE);
161                            }
162    
163                    };
164            }
165    
166            public E remove(int index) {
167                    throw new UnsupportedOperationException(_MESSAGE);
168            }
169    
170            public boolean remove(Object object) {
171                    throw new UnsupportedOperationException(_MESSAGE);
172            }
173    
174            public boolean removeAll(Collection<?> collection) {
175                    throw new UnsupportedOperationException(_MESSAGE);
176            }
177    
178            public boolean retainAll(Collection<?> collection) {
179                    throw new UnsupportedOperationException(_MESSAGE);
180            }
181    
182            public E set(int index, E element) {
183                    throw new UnsupportedOperationException(_MESSAGE);
184            }
185    
186            public int size() {
187                    return _list.size();
188            }
189    
190            public List<E> subList(int fromIndex, int toIndex) {
191                    return new UnmodifiableList<E>(_list.subList(fromIndex, toIndex));
192            }
193    
194            public Object[] toArray() {
195                    return _list.toArray();
196            }
197    
198            public <T> T[] toArray(T[] a) {
199                    return _list.toArray(a);
200            }
201    
202            @Override
203            public String toString() {
204                    return _list.toString();
205            }
206    
207            private static final String _MESSAGE =
208                    "Please make a copy of this read-only list before modifying it.";
209    
210            private List<? extends E> _list;
211    
212    }