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.set;
018    
019    import java.util.Collection;
020    import java.util.Iterator;
021    import java.util.Set;
022    
023    import org.apache.commons.collections.Unmodifiable;
024    import org.apache.commons.collections.iterators.UnmodifiableIterator;
025    
026    /**
027     * Decorates another <code>Set</code> to ensure it can't be altered.
028     * <p>
029     * This class is Serializable from Commons Collections 3.1.
030     *
031     * @since Commons Collections 3.0
032     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
033     * 
034     * @author Stephen Colebourne
035     */
036    public final class UnmodifiableSet
037            extends AbstractSerializableSetDecorator
038            implements Unmodifiable {
039    
040        /** Serialization version */
041        private static final long serialVersionUID = 6499119872185240161L;
042    
043        /**
044         * Factory method to create an unmodifiable set.
045         * 
046         * @param set  the set to decorate, must not be null
047         * @throws IllegalArgumentException if set is null
048         */
049        public static Set decorate(Set set) {
050            if (set instanceof Unmodifiable) {
051                return set;
052            }
053            return new UnmodifiableSet(set);
054        }
055    
056        //-----------------------------------------------------------------------
057        /**
058         * Constructor that wraps (not copies).
059         * 
060         * @param set  the set to decorate, must not be null
061         * @throws IllegalArgumentException if set is null
062         */
063        private UnmodifiableSet(Set set) {
064            super(set);
065        }
066    
067        //-----------------------------------------------------------------------
068        public Iterator iterator() {
069            return UnmodifiableIterator.decorate(getCollection().iterator());
070        }
071    
072        public boolean add(Object object) {
073            throw new UnsupportedOperationException();
074        }
075    
076        public boolean addAll(Collection coll) {
077            throw new UnsupportedOperationException();
078        }
079    
080        public void clear() {
081            throw new UnsupportedOperationException();
082        }
083    
084        public boolean remove(Object object) {
085            throw new UnsupportedOperationException();
086        }
087    
088        public boolean removeAll(Collection coll) {
089            throw new UnsupportedOperationException();
090        }
091    
092        public boolean retainAll(Collection coll) {
093            throw new UnsupportedOperationException();
094        }
095    
096    }