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