001    /*--------------------------------------------------------------------------+
002    $Id: UnmodifiableCollection.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.io.Serializable;
021    import java.util.Collection;
022    import java.util.Collections;
023    
024    /**
025     * This is a wrapper for a {@link Collection} prohibiting all calls which would
026     * modify its contents. As the construction of this class is performed in
027     * constant time it is preferred over copying the collection (which takes linear
028     * time). Using this class is also preferred to using the
029     * <code>unmodifiableX()</code> in class {@link Collections} as they return
030     * the collection base type that does not signal, that the object is
031     * unmodifiable. Using the classes in this package makes unmodifiability more
032     * explicit.
033     * <p>
034     * All prohibited methods throw an {@link UnsupportedOperationException}. The
035     * class is nearly the same as the one returned by
036     * {@link Collections#unmodifiableCollection(Collection)}, but by making it a
037     * public class we can make the return value of some methods more explicit.
038     * <p>
039     * This collection is serializable if the wrapped collection is serializable.
040     * 
041     * @author Benjamin Hummel
042     * @author $Author: juergens $
043     * 
044     * @version $Revision: 26283 $
045     * @levd.rating GREEN Hash: E7479B41B1D5AFEB227EFF86689F0DAA
046     */
047    public class UnmodifiableCollection<E> implements Collection<E>, Serializable {
048    
049            /** The underlying collection. */
050            private final Collection<E> c;
051    
052            /**
053             * Creates a new unmodifiable collection from another collection. All
054             * modifications to the underlying collection will directly be visible in
055             * this wrapper.
056             */
057            public UnmodifiableCollection(Collection<E> c) {
058                    if (c == null) {
059                            throw new IllegalArgumentException(
060                                            "Underlying collection may not be null!");
061                    }
062                    this.c = c;
063            }
064    
065            /** {@inheritDoc} */
066            public boolean isEmpty() {
067                    return c.isEmpty();
068            }
069    
070            /** {@inheritDoc} */
071            public int size() {
072                    return c.size();
073            }
074    
075            /** {@inheritDoc} */
076            public boolean contains(Object o) {
077                    return c.contains(o);
078            }
079    
080            /** {@inheritDoc} */
081            public boolean containsAll(Collection<?> other) {
082                    return c.containsAll(other);
083            }
084    
085            /** {@inheritDoc} */
086            public UnmodifiableIterator<E> iterator() {
087                    return new UnmodifiableIterator<E>(c.iterator());
088            }
089    
090            /** {@inheritDoc} */
091            public Object[] toArray() {
092                    return c.toArray();
093            }
094    
095            /** {@inheritDoc} */
096            public <T> T[] toArray(T[] a) {
097                    return c.toArray(a);
098            }
099    
100            /**
101             * Operation is not supported.
102             * 
103             * @throws UnsupportedOperationException
104             */
105            public boolean add(E arg0) {
106                    throw new UnsupportedOperationException();
107            }
108    
109            /**
110             * Operation is not supported.
111             * 
112             * @throws UnsupportedOperationException
113             */
114            public boolean addAll(Collection<? extends E> arg0) {
115                    throw new UnsupportedOperationException();
116            }
117    
118            /**
119             * Operation is not supported.
120             * 
121             * @throws UnsupportedOperationException
122             */
123            public void clear() {
124                    throw new UnsupportedOperationException();
125            }
126    
127            /**
128             * Operation is not supported.
129             * 
130             * @throws UnsupportedOperationException
131             */
132            public boolean remove(Object arg0) {
133                    throw new UnsupportedOperationException();
134            }
135    
136            /**
137             * Operation is not supported.
138             * 
139             * @throws UnsupportedOperationException
140             */
141            public boolean removeAll(Collection<?> arg0) {
142                    throw new UnsupportedOperationException();
143            }
144    
145            /**
146             * Operation is not supported.
147             * 
148             * @throws UnsupportedOperationException
149             */
150            public boolean retainAll(Collection<?> arg0) {
151                    throw new UnsupportedOperationException();
152            }
153    
154            /**
155             * Returns a string representation of this collection.
156             */
157            @Override
158            public String toString() {
159                    return c.toString();
160            }
161    }