001    /*--------------------------------------------------------------------------+
002    $Id: UnmodifiableList.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | You may obtain a copy of the License at                                  |
009    |                                                                          |
010    |    http://www.apache.org/licenses/LICENSE-2.0                            |
011    |                                                                          |
012    | Unless required by applicable law or agreed to in writing, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.List;
023    
024    /**
025     * This is a wrapper for a {@link List} prohibiting all calls which would modify
026     * its contents. As the construction of this class is performed in constant time
027     * it is prefered over copying the list (which takes linear time). Using this
028     * class is also preferred to using the <code>unmodifiableX()</code> in class
029     * {@link Collections} as they return the collection base type that does not
030     * signal, that the object ist unmodifiable. Using the classes in this package
031     * makes unmodifiability more explicit.
032     * <p>
033     * All prohibited methods throw an {@link UnsupportedOperationException}. The
034     * class is nearly the same as the one returned by
035     * {@link Collections#unmodifiableList(List)}, but by making it a public class
036     * we can make the return value of some methods more explicit.
037     * <p>
038     * This list is serializable if the wrapped list is serializable.
039     * 
040     * @author Benjamin Hummel
041     * @author $Author: juergens $
042     * 
043     * @version $Revision: 26283 $
044     * @levd.rating GREEN Hash: 4B2E02522B37EA43AB9DDF9C28DCC2E1
045     */
046    public class UnmodifiableList<E> extends UnmodifiableCollection<E> implements
047                    List<E> {
048    
049            /** The underlying list. */
050            private final List<E> l;
051    
052            /**
053             * Creates a new unmodifiable list from another list. All modifications to
054             * the underlying list will directly be visible in this wrapper.
055             */
056            public UnmodifiableList(List<E> l) {
057                    super(l);
058                    this.l = l;
059            }
060    
061            /** {@inheritDoc} */
062            public E get(int index) {
063                    return l.get(index);
064            }
065    
066            /** {@inheritDoc} */
067            public int indexOf(Object o) {
068                    return l.indexOf(o);
069            }
070    
071            /** {@inheritDoc} */
072            public int lastIndexOf(Object o) {
073                    return l.lastIndexOf(o);
074            }
075    
076            /** {@inheritDoc} */
077            public UnmodifiableListIterator<E> listIterator() {
078                    return new UnmodifiableListIterator<E>(l.listIterator());
079            }
080    
081            /** {@inheritDoc} */
082            public UnmodifiableListIterator<E> listIterator(int index) {
083                    return new UnmodifiableListIterator<E>(l.listIterator(index));
084            }
085    
086            /** {@inheritDoc} */
087            public List<E> subList(int fromIndex, int toIndex) {
088                    return new UnmodifiableList<E>(l.subList(fromIndex, toIndex));
089            }
090    
091            /**
092             * Operation is not supported.
093             * 
094             * @throws UnsupportedOperationException
095             */
096            public void add(int arg0, E arg1) {
097                    throw new UnsupportedOperationException();
098            }
099    
100            /**
101             * Operation is not supported.
102             * 
103             * @throws UnsupportedOperationException
104             */
105            public boolean addAll(int arg0, Collection<? extends E> arg1) {
106                    throw new UnsupportedOperationException();
107            }
108    
109            /**
110             * Operation is not supported.
111             * 
112             * @throws UnsupportedOperationException
113             */
114            public E remove(int arg0) {
115                    throw new UnsupportedOperationException();
116            }
117    
118            /**
119             * Operation is not supported.
120             * 
121             * @throws UnsupportedOperationException
122             */
123            public E set(int arg0, E arg1) {
124                    throw new UnsupportedOperationException();
125            }
126    }