001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.collections.collection;
018
019 import java.util.Collection;
020 import java.util.Iterator;
021
022 import org.apache.commons.collections.Unmodifiable;
023 import org.apache.commons.collections.iterators.UnmodifiableIterator;
024
025 /**
026 * Decorates another <code>Collection</code> to ensure it can't be altered.
027 * <p>
028 * This class is Serializable from Commons Collections 3.1.
029 *
030 * @since Commons Collections 3.0
031 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
032 *
033 * @author Stephen Colebourne
034 */
035 public final class UnmodifiableCollection
036 extends AbstractSerializableCollectionDecorator
037 implements Unmodifiable {
038
039 /** Serialization version */
040 private static final long serialVersionUID = -239892006883819945L;
041
042 /**
043 * Factory method to create an unmodifiable collection.
044 * <p>
045 * If the collection passed in is already unmodifiable, it is returned.
046 *
047 * @param coll the collection to decorate, must not be null
048 * @return an unmodifiable collection
049 * @throws IllegalArgumentException if collection is null
050 */
051 public static Collection decorate(Collection coll) {
052 if (coll instanceof Unmodifiable) {
053 return coll;
054 }
055 return new UnmodifiableCollection(coll);
056 }
057
058 //-----------------------------------------------------------------------
059 /**
060 * Constructor that wraps (not copies).
061 *
062 * @param coll the collection to decorate, must not be null
063 * @throws IllegalArgumentException if collection is null
064 */
065 private UnmodifiableCollection(Collection coll) {
066 super(coll);
067 }
068
069 //-----------------------------------------------------------------------
070 public Iterator iterator() {
071 return UnmodifiableIterator.decorate(getCollection().iterator());
072 }
073
074 public boolean add(Object object) {
075 throw new UnsupportedOperationException();
076 }
077
078 public boolean addAll(Collection coll) {
079 throw new UnsupportedOperationException();
080 }
081
082 public void clear() {
083 throw new UnsupportedOperationException();
084 }
085
086 public boolean remove(Object object) {
087 throw new UnsupportedOperationException();
088 }
089
090 public boolean removeAll(Collection coll) {
091 throw new UnsupportedOperationException();
092 }
093
094 public boolean retainAll(Collection coll) {
095 throw new UnsupportedOperationException();
096 }
097
098 }