001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.util.Collection;
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.ListIterator;
023    
024    /**
025     * <p>
026     * This is a read-only wrapper around any <code>List</code>. Query operations
027     * will "read through" to the specified list. Attempts to modify the list
028     * directly or via its iterator will result in a
029     * <code>java.lang.UnsupportedOperationException</code>.
030     * </p>
031     *
032     * @author     Alexander Chow
033     * @deprecated As of 7.0.0, replaced by {@link
034     *             java.util.Collections#unmodifiableList(List)}
035     */
036    @Deprecated
037    public class UnmodifiableList<E> implements List<E>, Serializable {
038    
039            public UnmodifiableList(List<? extends E> list) {
040                    if (list == null) {
041                            throw new NullPointerException();
042                    }
043    
044                    _list = list;
045            }
046    
047            @Override
048            public boolean add(E element) {
049                    throw new UnsupportedOperationException(_MESSAGE);
050            }
051    
052            @Override
053            public void add(int index, E element) {
054                    throw new UnsupportedOperationException(_MESSAGE);
055            }
056    
057            @Override
058            public boolean addAll(Collection<? extends E> collection) {
059                    throw new UnsupportedOperationException(_MESSAGE);
060            }
061    
062            @Override
063            public boolean addAll(int index, Collection<? extends E> collection) {
064                    throw new UnsupportedOperationException(_MESSAGE);
065            }
066    
067            @Override
068            public void clear() {
069                    throw new UnsupportedOperationException(_MESSAGE);
070            }
071    
072            @Override
073            public boolean contains(Object object) {
074                    return _list.contains(object);
075            }
076    
077            @Override
078            public boolean containsAll(Collection<?> collection) {
079                    return _list.containsAll(collection);
080            }
081    
082            @Override
083            public boolean equals(Object object) {
084                    return _list.equals(object);
085            }
086    
087            @Override
088            public E get(int index) {
089                    return _list.get(index);
090            }
091    
092            @Override
093            public int hashCode() {
094                    return _list.hashCode();
095            }
096    
097            @Override
098            public int indexOf(Object object) {
099                    return _list.indexOf(object);
100            }
101    
102            @Override
103            public boolean isEmpty() {
104                    return _list.isEmpty();
105            }
106    
107            @Override
108            public Iterator<E> iterator() {
109                    return new Iterator<E>() {
110    
111                            Iterator<? extends E> itr = _list.iterator();
112    
113                            @Override
114                            public boolean hasNext() {
115                                    return itr.hasNext();
116                            }
117    
118                            @Override
119                            public E next() {
120                                    return itr.next();
121                            }
122    
123                            @Override
124                            public void remove() {
125                                    throw new UnsupportedOperationException(_MESSAGE);
126                            }
127    
128                    };
129            }
130    
131            @Override
132            public int lastIndexOf(Object o) {
133                    return _list.lastIndexOf(o);
134            }
135    
136            @Override
137            public ListIterator<E> listIterator() {
138                    return listIterator(0);
139            }
140    
141            @Override
142            public ListIterator<E> listIterator(final int index) {
143                    return new ListIterator<E>() {
144    
145                            ListIterator<? extends E> itr = _list.listIterator(index);
146    
147                            @Override
148                            public void add(E element) {
149                                    throw new UnsupportedOperationException(_MESSAGE);
150                            }
151    
152                            @Override
153                            public boolean hasNext() {
154                                    return itr.hasNext();
155                            }
156    
157                            @Override
158                            public E next() {
159                                    return itr.next();
160                            }
161    
162                            @Override
163                            public boolean hasPrevious() {
164                                    return itr.hasPrevious();
165                            }
166    
167                            @Override
168                            public E previous() {
169                                    return itr.previous();
170                            }
171    
172                            @Override
173                            public int nextIndex() {
174                                    return itr.nextIndex();
175                            }
176    
177                            @Override
178                            public int previousIndex() {
179                                    return itr.previousIndex();
180                            }
181    
182                            @Override
183                            public void remove() {
184                                    throw new UnsupportedOperationException(_MESSAGE);
185                            }
186    
187                            @Override
188                            public void set(E element) {
189                                    throw new UnsupportedOperationException(_MESSAGE);
190                            }
191    
192                    };
193            }
194    
195            @Override
196            public E remove(int index) {
197                    throw new UnsupportedOperationException(_MESSAGE);
198            }
199    
200            @Override
201            public boolean remove(Object object) {
202                    throw new UnsupportedOperationException(_MESSAGE);
203            }
204    
205            @Override
206            public boolean removeAll(Collection<?> collection) {
207                    throw new UnsupportedOperationException(_MESSAGE);
208            }
209    
210            @Override
211            public boolean retainAll(Collection<?> collection) {
212                    throw new UnsupportedOperationException(_MESSAGE);
213            }
214    
215            @Override
216            public E set(int index, E element) {
217                    throw new UnsupportedOperationException(_MESSAGE);
218            }
219    
220            @Override
221            public int size() {
222                    return _list.size();
223            }
224    
225            @Override
226            public List<E> subList(int fromIndex, int toIndex) {
227                    return new UnmodifiableList<>(_list.subList(fromIndex, toIndex));
228            }
229    
230            @Override
231            public Object[] toArray() {
232                    return _list.toArray();
233            }
234    
235            @Override
236            public <T> T[] toArray(T[] a) {
237                    return _list.toArray(a);
238            }
239    
240            @Override
241            public String toString() {
242                    return _list.toString();
243            }
244    
245            private static final String _MESSAGE =
246                    "Please make a copy of this read-only list before modifying it.";
247    
248            private final List<? extends E> _list;
249    
250    }