1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.util;
21  
22  import java.io.Serializable;
23  
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.ListIterator;
28  
29  /**
30   * <a href="UnmodifiableList.java.html"><b><i>View Source</i></b></a>
31   *
32   * <p>
33   * This is a read-only wrapper around any <code>java.util.List</code>. Query
34   * operations will "read through" to the specified list. Attempts to modify the
35   * list directly or via its iterator will result in a
36   * <code>java.lang.UnsupportedOperationException</code>.
37   * </p>
38   *
39   * @author Alexander Chow
40   *
41   */
42  public class UnmodifiableList<E> implements List<E>, Serializable {
43  
44      public UnmodifiableList(List<? extends E> list) {
45          if (list == null) {
46              throw new NullPointerException();
47          }
48  
49          _list = list;
50      }
51  
52      public boolean add(E element) {
53          throw new UnsupportedOperationException(_MESSAGE);
54      }
55  
56      public void add(int index, E element) {
57          throw new UnsupportedOperationException(_MESSAGE);
58      }
59  
60      public boolean addAll(Collection<? extends E> collection) {
61          throw new UnsupportedOperationException(_MESSAGE);
62      }
63  
64      public boolean addAll(int index, Collection<? extends E> collection) {
65          throw new UnsupportedOperationException(_MESSAGE);
66      }
67  
68      public void clear() {
69          throw new UnsupportedOperationException(_MESSAGE);
70      }
71  
72      public boolean contains(Object object) {
73          return _list.contains(object);
74      }
75  
76      public boolean containsAll(Collection<?> collection) {
77          return _list.containsAll(collection);
78      }
79  
80      public boolean equals(Object object) {
81          return _list.equals(object);
82      }
83  
84      public E get(int index) {
85          return _list.get(index);
86      }
87  
88      public int hashCode() {
89          return _list.hashCode();
90      }
91  
92      public int indexOf(Object object) {
93          return _list.indexOf(object);
94      }
95  
96      public boolean isEmpty() {
97          return _list.isEmpty();
98      }
99  
100     public Iterator<E> iterator() {
101         return new Iterator<E>() {
102 
103             Iterator<? extends E> itr = _list.iterator();
104 
105             public boolean hasNext() {
106                 return itr.hasNext();
107             }
108 
109             public E next() {
110                 return itr.next();
111             }
112 
113             public void remove() {
114                 throw new UnsupportedOperationException(_MESSAGE);
115             }
116 
117         };
118     }
119 
120     public int lastIndexOf(Object o) {
121         return _list.lastIndexOf(o);
122     }
123 
124     public ListIterator<E> listIterator() {
125         return listIterator(0);
126     }
127 
128     public ListIterator<E> listIterator(final int index) {
129         return new ListIterator<E>() {
130 
131             ListIterator<? extends E> itr = _list.listIterator(index);
132 
133             public void add(E element) {
134                 throw new UnsupportedOperationException(_MESSAGE);
135             }
136 
137             public boolean hasNext() {
138                 return itr.hasNext();
139             }
140 
141             public E next() {
142                 return itr.next();
143             }
144 
145             public boolean hasPrevious() {
146                 return itr.hasPrevious();
147             }
148 
149             public E previous() {
150                 return itr.previous();
151             }
152 
153             public int nextIndex() {
154                 return itr.nextIndex();
155             }
156 
157             public int previousIndex() {
158                 return itr.previousIndex();
159             }
160 
161             public void remove() {
162                 throw new UnsupportedOperationException(_MESSAGE);
163             }
164 
165             public void set(E element) {
166                 throw new UnsupportedOperationException(_MESSAGE);
167             }
168 
169         };
170     }
171 
172     public E remove(int index) {
173         throw new UnsupportedOperationException(_MESSAGE);
174     }
175 
176     public boolean remove(Object object) {
177         throw new UnsupportedOperationException(_MESSAGE);
178     }
179 
180     public boolean removeAll(Collection<?> collection) {
181         throw new UnsupportedOperationException(_MESSAGE);
182     }
183 
184     public boolean retainAll(Collection<?> collection) {
185         throw new UnsupportedOperationException(_MESSAGE);
186     }
187 
188     public E set(int index, E element) {
189         throw new UnsupportedOperationException(_MESSAGE);
190     }
191 
192     public int size() {
193         return _list.size();
194     }
195 
196     public List<E> subList(int fromIndex, int toIndex) {
197         return new UnmodifiableList<E>(_list.subList(fromIndex, toIndex));
198     }
199 
200     public Object[] toArray() {
201         return _list.toArray();
202     }
203 
204     public <T> T[] toArray(T[] a) {
205         return _list.toArray(a);
206     }
207 
208     public String toString() {
209         return _list.toString();
210     }
211 
212     private static final String _MESSAGE =
213         "Please make a copy of this read-only list before modifying it.";
214 
215     private List<? extends E> _list;
216 
217 }