1 /* 2 * Copyright 2003-2004 The Apache Software Foundation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.apache.commons.collections.collection; 17 18 import java.util.Collection; 19 import java.util.Iterator; 20 21 import org.apache.commons.collections.Unmodifiable; 22 import org.apache.commons.collections.iterators.UnmodifiableIterator; 23 24 /** 25 * Decorates another <code>Collection</code> to ensure it can't be altered. 26 * <p> 27 * This class is Serializable from Commons Collections 3.1. 28 * 29 * @since Commons Collections 3.0 30 * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:13 $ 31 * 32 * @author Stephen Colebourne 33 */ 34 public final class UnmodifiableCollection 35 extends AbstractSerializableCollectionDecorator 36 implements Unmodifiable { 37 38 /** Serialization version */ 39 private static final long serialVersionUID = -239892006883819945L; 40 41 /** 42 * Factory method to create an unmodifiable collection. 43 * <p> 44 * If the collection passed in is already unmodifiable, it is returned. 45 * 46 * @param coll the collection to decorate, must not be null 47 * @return an unmodifiable collection 48 * @throws IllegalArgumentException if collection is null 49 */ 50 public static Collection decorate(Collection coll) { 51 if (coll instanceof Unmodifiable) { 52 return coll; 53 } 54 return new UnmodifiableCollection(coll); 55 } 56 57 //----------------------------------------------------------------------- 58 /** 59 * Constructor that wraps (not copies). 60 * 61 * @param coll the collection to decorate, must not be null 62 * @throws IllegalArgumentException if collection is null 63 */ 64 private UnmodifiableCollection(Collection coll) { 65 super(coll); 66 } 67 68 //----------------------------------------------------------------------- 69 public Iterator iterator() { 70 return UnmodifiableIterator.decorate(getCollection().iterator()); 71 } 72 73 public boolean add(Object object) { 74 throw new UnsupportedOperationException(); 75 } 76 77 public boolean addAll(Collection coll) { 78 throw new UnsupportedOperationException(); 79 } 80 81 public void clear() { 82 throw new UnsupportedOperationException(); 83 } 84 85 public boolean remove(Object object) { 86 throw new UnsupportedOperationException(); 87 } 88 89 public boolean removeAll(Collection coll) { 90 throw new UnsupportedOperationException(); 91 } 92 93 public boolean retainAll(Collection coll) { 94 throw new UnsupportedOperationException(); 95 } 96 97 } 98