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