1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
45  public class UnmodifiableList<E> implements List<E>, Serializable {
46  
47      public UnmodifiableList(List<? extends E> list) {
48          if (list == null) {
49              throw new NullPointerException();
50          }
51  
52          _list = list;
53      }
54  
55      public boolean add(E element) {
56          throw new UnsupportedOperationException(_MESSAGE);
57      }
58  
59      public void add(int index, E element) {
60          throw new UnsupportedOperationException(_MESSAGE);
61      }
62  
63      public boolean addAll(Collection<? extends E> collection) {
64          throw new UnsupportedOperationException(_MESSAGE);
65      }
66  
67      public boolean addAll(int index, Collection<? extends E> collection) {
68          throw new UnsupportedOperationException(_MESSAGE);
69      }
70  
71      public void clear() {
72          throw new UnsupportedOperationException(_MESSAGE);
73      }
74  
75      public boolean contains(Object object) {
76          return _list.contains(object);
77      }
78  
79      public boolean containsAll(Collection<?> collection) {
80          return _list.containsAll(collection);
81      }
82  
83      public boolean equals(Object object) {
84          return _list.equals(object);
85      }
86  
87      public E get(int index) {
88          return _list.get(index);
89      }
90  
91      public int hashCode() {
92          return _list.hashCode();
93      }
94  
95      public int indexOf(Object object) {
96          return _list.indexOf(object);
97      }
98  
99      public boolean isEmpty() {
100         return _list.isEmpty();
101     }
102 
103     public Iterator<E> iterator() {
104         return new Iterator<E>() {
105 
106             Iterator<? extends E> itr = _list.iterator();
107 
108             public boolean hasNext() {
109                 return itr.hasNext();
110             }
111 
112             public E next() {
113                 return itr.next();
114             }
115 
116             public void remove() {
117                 throw new UnsupportedOperationException(_MESSAGE);
118             }
119 
120         };
121     }
122 
123     public int lastIndexOf(Object o) {
124         return _list.lastIndexOf(o);
125     }
126 
127     public ListIterator<E> listIterator() {
128         return listIterator(0);
129     }
130 
131     public ListIterator<E> listIterator(final int index) {
132         return new ListIterator<E>() {
133 
134             ListIterator<? extends E> itr = _list.listIterator(index);
135 
136             public void add(E element) {
137                 throw new UnsupportedOperationException(_MESSAGE);
138             }
139 
140             public boolean hasNext() {
141                 return itr.hasNext();
142             }
143 
144             public E next() {
145                 return itr.next();
146             }
147 
148             public boolean hasPrevious() {
149                 return itr.hasPrevious();
150             }
151 
152             public E previous() {
153                 return itr.previous();
154             }
155 
156             public int nextIndex() {
157                 return itr.nextIndex();
158             }
159 
160             public int previousIndex() {
161                 return itr.previousIndex();
162             }
163 
164             public void remove() {
165                 throw new UnsupportedOperationException(_MESSAGE);
166             }
167 
168             public void set(E element) {
169                 throw new UnsupportedOperationException(_MESSAGE);
170             }
171 
172         };
173     }
174 
175     public E remove(int index) {
176         throw new UnsupportedOperationException(_MESSAGE);
177     }
178 
179     public boolean remove(Object object) {
180         throw new UnsupportedOperationException(_MESSAGE);
181     }
182 
183     public boolean removeAll(Collection<?> collection) {
184         throw new UnsupportedOperationException(_MESSAGE);
185     }
186 
187     public boolean retainAll(Collection<?> collection) {
188         throw new UnsupportedOperationException(_MESSAGE);
189     }
190 
191     public E set(int index, E element) {
192         throw new UnsupportedOperationException(_MESSAGE);
193     }
194 
195     public int size() {
196         return _list.size();
197     }
198 
199     public List<E> subList(int fromIndex, int toIndex) {
200         return new UnmodifiableList<E>(_list.subList(fromIndex, toIndex));
201     }
202 
203     public Object[] toArray() {
204         return _list.toArray();
205     }
206 
207     public <T> T[] toArray(T[] a) {
208         return _list.toArray(a);
209     }
210 
211     public String toString() {
212         return _list.toString();
213     }
214 
215     private static final String _MESSAGE =
216         "Please make a copy of this read-only list before modifying it.";
217 
218     private List<? extends E> _list;
219 
220 }