1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.Serializable;
18  
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.ListIterator;
23  
24  /**
25   * <a href="UnmodifiableList.java.html"><b><i>View Source</i></b></a>
26   *
27   * <p>
28   * This is a read-only wrapper around any <code>java.util.List</code>. Query
29   * operations will "read through" to the specified list. Attempts to modify the
30   * list directly or via its iterator will result in a
31   * <code>java.lang.UnsupportedOperationException</code>.
32   * </p>
33   *
34   * @author Alexander Chow
35   */
36  public class UnmodifiableList<E> implements List<E>, Serializable {
37  
38      public UnmodifiableList(List<? extends E> list) {
39          if (list == null) {
40              throw new NullPointerException();
41          }
42  
43          _list = list;
44      }
45  
46      public boolean add(E element) {
47          throw new UnsupportedOperationException(_MESSAGE);
48      }
49  
50      public void add(int index, E element) {
51          throw new UnsupportedOperationException(_MESSAGE);
52      }
53  
54      public boolean addAll(Collection<? extends E> collection) {
55          throw new UnsupportedOperationException(_MESSAGE);
56      }
57  
58      public boolean addAll(int index, Collection<? extends E> collection) {
59          throw new UnsupportedOperationException(_MESSAGE);
60      }
61  
62      public void clear() {
63          throw new UnsupportedOperationException(_MESSAGE);
64      }
65  
66      public boolean contains(Object object) {
67          return _list.contains(object);
68      }
69  
70      public boolean containsAll(Collection<?> collection) {
71          return _list.containsAll(collection);
72      }
73  
74      public boolean equals(Object object) {
75          return _list.equals(object);
76      }
77  
78      public E get(int index) {
79          return _list.get(index);
80      }
81  
82      public int hashCode() {
83          return _list.hashCode();
84      }
85  
86      public int indexOf(Object object) {
87          return _list.indexOf(object);
88      }
89  
90      public boolean isEmpty() {
91          return _list.isEmpty();
92      }
93  
94      public Iterator<E> iterator() {
95          return new Iterator<E>() {
96  
97              Iterator<? extends E> itr = _list.iterator();
98  
99              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     public String toString() {
203         return _list.toString();
204     }
205 
206     private static final String _MESSAGE =
207         "Please make a copy of this read-only list before modifying it.";
208 
209     private List<? extends E> _list;
210 
211 }