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.io.IOException;
020    import java.io.ObjectInputStream;
021    import java.io.ObjectOutputStream;
022    import java.io.Serializable;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.SortedSet;
026    
027    import org.apache.commons.collections.Unmodifiable;
028    import org.apache.commons.collections.iterators.UnmodifiableIterator;
029    
030    /**
031     * Decorates another <code>SortedSet</code> to ensure it can't be altered.
032     * <p>
033     * This class is Serializable from Commons Collections 3.1.
034     *
035     * @since Commons Collections 3.0
036     * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
037     * 
038     * @author Stephen Colebourne
039     */
040    public final class UnmodifiableSortedSet
041            extends AbstractSortedSetDecorator
042            implements Unmodifiable, Serializable {
043    
044        /** Serialization version */
045        private static final long serialVersionUID = -725356885467962424L;
046    
047        /**
048         * Factory method to create an unmodifiable set.
049         * 
050         * @param set  the set to decorate, must not be null
051         * @throws IllegalArgumentException if set is null
052         */
053        public static SortedSet decorate(SortedSet set) {
054            if (set instanceof Unmodifiable) {
055                return set;
056            }
057            return new UnmodifiableSortedSet(set);
058        }
059    
060        //-----------------------------------------------------------------------
061        /**
062         * Write the collection out using a custom routine.
063         * 
064         * @param out  the output stream
065         * @throws IOException
066         */
067        private void writeObject(ObjectOutputStream out) throws IOException {
068            out.defaultWriteObject();
069            out.writeObject(collection);
070        }
071    
072        /**
073         * Read the collection in using a custom routine.
074         * 
075         * @param in  the input stream
076         * @throws IOException
077         * @throws ClassNotFoundException
078         */
079        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
080            in.defaultReadObject();
081            collection = (Collection) in.readObject();
082        }
083    
084        //-----------------------------------------------------------------------
085        /**
086         * Constructor that wraps (not copies).
087         * 
088         * @param set  the set to decorate, must not be null
089         * @throws IllegalArgumentException if set is null
090         */
091        private UnmodifiableSortedSet(SortedSet set) {
092            super(set);
093        }
094    
095        //-----------------------------------------------------------------------
096        public Iterator iterator() {
097            return UnmodifiableIterator.decorate(getCollection().iterator());
098        }
099    
100        public boolean add(Object object) {
101            throw new UnsupportedOperationException();
102        }
103    
104        public boolean addAll(Collection coll) {
105            throw new UnsupportedOperationException();
106        }
107    
108        public void clear() {
109            throw new UnsupportedOperationException();
110        }
111    
112        public boolean remove(Object object) {
113            throw new UnsupportedOperationException();
114        }
115    
116        public boolean removeAll(Collection coll) {
117            throw new UnsupportedOperationException();
118        }
119    
120        public boolean retainAll(Collection coll) {
121            throw new UnsupportedOperationException();
122        }
123    
124        //-----------------------------------------------------------------------
125        public SortedSet subSet(Object fromElement, Object toElement) {
126            SortedSet sub = getSortedSet().subSet(fromElement, toElement);
127            return new UnmodifiableSortedSet(sub);
128        }
129    
130        public SortedSet headSet(Object toElement) {
131            SortedSet sub = getSortedSet().headSet(toElement);
132            return new UnmodifiableSortedSet(sub);
133        }
134    
135        public SortedSet tailSet(Object fromElement) {
136            SortedSet sub = getSortedSet().tailSet(fromElement);
137            return new UnmodifiableSortedSet(sub);
138        }
139    
140    }