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.set; 17 18 import java.util.Collection; 19 import java.util.Iterator; 20 import java.util.Set; 21 22 import org.apache.commons.collections.Unmodifiable; 23 import org.apache.commons.collections.iterators.UnmodifiableIterator; 24 25 /** 26 * Decorates another <code>Set</code> to ensure it can't be altered. 27 * <p> 28 * This class is Serializable from Commons Collections 3.1. 29 * 30 * @since Commons Collections 3.0 31 * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $ 32 * 33 * @author Stephen Colebourne 34 */ 35 public final class UnmodifiableSet 36 extends AbstractSerializableSetDecorator 37 implements Unmodifiable { 38 39 /** Serialization version */ 40 private static final long serialVersionUID = 6499119872185240161L; 41 42 /** 43 * Factory method to create an unmodifiable set. 44 * 45 * @param set the set to decorate, must not be null 46 * @throws IllegalArgumentException if set is null 47 */ 48 public static Set decorate(Set set) { 49 if (set instanceof Unmodifiable) { 50 return set; 51 } 52 return new UnmodifiableSet(set); 53 } 54 55 //----------------------------------------------------------------------- 56 /** 57 * Constructor that wraps (not copies). 58 * 59 * @param set the set to decorate, must not be null 60 * @throws IllegalArgumentException if set is null 61 */ 62 private UnmodifiableSet(Set set) { 63 super(set); 64 } 65 66 //----------------------------------------------------------------------- 67 public Iterator iterator() { 68 return UnmodifiableIterator.decorate(getCollection().iterator()); 69 } 70 71 public boolean add(Object object) { 72 throw new UnsupportedOperationException(); 73 } 74 75 public boolean addAll(Collection coll) { 76 throw new UnsupportedOperationException(); 77 } 78 79 public void clear() { 80 throw new UnsupportedOperationException(); 81 } 82 83 public boolean remove(Object object) { 84 throw new UnsupportedOperationException(); 85 } 86 87 public boolean removeAll(Collection coll) { 88 throw new UnsupportedOperationException(); 89 } 90 91 public boolean retainAll(Collection coll) { 92 throw new UnsupportedOperationException(); 93 } 94 95 } 96