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.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.ListIterator;
26  
27  /**
28   * <a href="UnmodifiableList.java.html"><b><i>View Source</i></b></a>
29   *
30   * <p>
31   * This is a read-only wrapper around any <code>java.util.List</code>. Query
32   * operations will "read through" to the specified list. Attempts to modify the
33   * list directly or via its iterator will result in a
34   * <code>java.lang.UnsupportedOperationException</code>.
35   * </p>
36   *
37   * @author Alexander Chow
38   *
39   */
40  public class UnmodifiableList<E> implements List<E> {
41  
42      public UnmodifiableList(List<? extends E> list) {
43          if (list == null) {
44              throw new NullPointerException();
45          }
46  
47          _list = list;
48      }
49  
50      public boolean add(E element) {
51          throw new UnsupportedOperationException(_MESSAGE);
52      }
53  
54      public void add(int index, E element) {
55          throw new UnsupportedOperationException(_MESSAGE);
56      }
57  
58      public boolean addAll(Collection<? extends E> collection) {
59          throw new UnsupportedOperationException(_MESSAGE);
60      }
61  
62      public boolean addAll(int index, Collection<? extends E> collection) {
63          throw new UnsupportedOperationException(_MESSAGE);
64      }
65  
66      public void clear() {
67          throw new UnsupportedOperationException(_MESSAGE);
68      }
69  
70      public boolean contains(Object object) {
71          return _list.contains(object);
72      }
73  
74      public boolean containsAll(Collection<?> collection) {
75          return _list.containsAll(collection);
76      }
77  
78      public boolean equals(Object object) {
79          return _list.equals(object);
80      }
81  
82      public E get(int index) {
83          return _list.get(index);
84      }
85  
86      public int hashCode() {
87          return _list.hashCode();
88      }
89  
90      public int indexOf(Object object) {
91          return _list.indexOf(object);
92      }
93  
94      public boolean isEmpty() {
95          return _list.isEmpty();
96      }
97  
98      public Iterator<E> iterator() {
99          return new Iterator<E>() {
100 
101             Iterator<? extends E> itr = _list.iterator();
102 
103             public boolean hasNext() {
104                 return itr.hasNext();
105             }
106 
107             public E next() {
108                 return itr.next();
109             }
110 
111             public void remove() {
112                 throw new UnsupportedOperationException(_MESSAGE);
113             }
114 
115         };
116     }
117 
118     public int lastIndexOf(Object o) {
119         return _list.lastIndexOf(o);
120     }
121 
122     public ListIterator<E> listIterator() {
123         return listIterator(0);
124     }
125 
126     public ListIterator<E> listIterator(final int index) {
127         return new ListIterator<E>() {
128 
129             ListIterator<? extends E> itr = _list.listIterator(index);
130 
131             public void add(E element) {
132                 throw new UnsupportedOperationException(_MESSAGE);
133             }
134 
135             public boolean hasNext() {
136                 return itr.hasNext();
137             }
138 
139             public E next() {
140                 return itr.next();
141             }
142 
143             public boolean hasPrevious() {
144                 return itr.hasPrevious();
145             }
146 
147             public E previous() {
148                 return itr.previous();
149             }
150 
151             public int nextIndex() {
152                 return itr.nextIndex();
153             }
154 
155             public int previousIndex() {
156                 return itr.previousIndex();
157             }
158 
159             public void remove() {
160                 throw new UnsupportedOperationException(_MESSAGE);
161             }
162 
163             public void set(E element) {
164                 throw new UnsupportedOperationException(_MESSAGE);
165             }
166 
167         };
168     }
169 
170     public E remove(int index) {
171         throw new UnsupportedOperationException(_MESSAGE);
172     }
173 
174     public boolean remove(Object object) {
175         throw new UnsupportedOperationException(_MESSAGE);
176     }
177 
178     public boolean removeAll(Collection<?> collection) {
179         throw new UnsupportedOperationException(_MESSAGE);
180     }
181 
182     public boolean retainAll(Collection<?> collection) {
183         throw new UnsupportedOperationException(_MESSAGE);
184     }
185 
186     public E set(int index, E element) {
187         throw new UnsupportedOperationException(_MESSAGE);
188     }
189 
190     public int size() {
191         return _list.size();
192     }
193 
194     public List<E> subList(int fromIndex, int toIndex) {
195         return new UnmodifiableList<E>(_list.subList(fromIndex, toIndex));
196     }
197 
198     public Object[] toArray() {
199         return _list.toArray();
200     }
201 
202     public <T> T[] toArray(T[] a) {
203         return _list.toArray(a);
204     }
205 
206     public String toString() {
207         return _list.toString();
208     }
209 
210     private static final String _MESSAGE =
211         "Please make a copy of this read-only list before modifying it.";
212 
213     private List<? extends E> _list;
214 
215 }