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 */
017package org.apache.commons.collections4.set;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.SortedSet;
025
026import org.apache.commons.collections4.Unmodifiable;
027import org.apache.commons.collections4.iterators.UnmodifiableIterator;
028
029/**
030 * Decorates another <code>SortedSet</code> to ensure it can't be altered.
031 * <p>
032 * This class is Serializable from Commons Collections 3.1.
033 * <p>
034 * Attempts to modify it will result in an UnsupportedOperationException.
035 *
036 * @since 3.0
037 * @version $Id: UnmodifiableSortedSet.html 972421 2015-11-14 20:00:04Z tn $
038 */
039public final class UnmodifiableSortedSet<E>
040        extends AbstractSortedSetDecorator<E>
041        implements Unmodifiable {
042
043    /** Serialization version */
044    private static final long serialVersionUID = -725356885467962424L;
045
046    /**
047     * Factory method to create an unmodifiable set.
048     *
049     * @param <E> the element type
050     * @param set  the set to decorate, must not be null
051     * @return a new unmodifiable {@link SortedSet}
052     * @throws IllegalArgumentException if set is null
053     * @since 4.0
054     */
055    public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set) {
056        if (set instanceof Unmodifiable) {
057            return set;
058        }
059        return new UnmodifiableSortedSet<E>(set);
060    }
061
062    //-----------------------------------------------------------------------
063    /**
064     * Write the collection out using a custom routine.
065     *
066     * @param out  the output stream
067     * @throws IOException
068     */
069    private void writeObject(final ObjectOutputStream out) throws IOException {
070        out.defaultWriteObject();
071        out.writeObject(decorated());
072    }
073
074    /**
075     * Read the collection in using a custom routine.
076     *
077     * @param in  the input stream
078     * @throws IOException
079     * @throws ClassNotFoundException
080     */
081    @SuppressWarnings("unchecked") // (1) should only fail if input stream is incorrect
082    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
083        in.defaultReadObject();
084        setCollection((Collection<E>) in.readObject()); // (1)
085    }
086
087    //-----------------------------------------------------------------------
088    /**
089     * Constructor that wraps (not copies).
090     *
091     * @param set  the set to decorate, must not be null
092     * @throws IllegalArgumentException if set is null
093     */
094    private UnmodifiableSortedSet(final SortedSet<E> set) {
095        super(set);
096    }
097
098    //-----------------------------------------------------------------------
099    @Override
100    public Iterator<E> iterator() {
101        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
102    }
103
104    @Override
105    public boolean add(final E object) {
106        throw new UnsupportedOperationException();
107    }
108
109    @Override
110    public boolean addAll(final Collection<? extends E> coll) {
111        throw new UnsupportedOperationException();
112    }
113
114    @Override
115    public void clear() {
116        throw new UnsupportedOperationException();
117    }
118
119    @Override
120    public boolean remove(final Object object) {
121        throw new UnsupportedOperationException();
122    }
123
124    @Override
125    public boolean removeAll(final Collection<?> coll) {
126        throw new UnsupportedOperationException();
127    }
128
129    @Override
130    public boolean retainAll(final Collection<?> coll) {
131        throw new UnsupportedOperationException();
132    }
133
134    //-----------------------------------------------------------------------
135    @Override
136    public SortedSet<E> subSet(final E fromElement, final E toElement) {
137        final SortedSet<E> sub = decorated().subSet(fromElement, toElement);
138        return new UnmodifiableSortedSet<E>(sub);
139    }
140
141    @Override
142    public SortedSet<E> headSet(final E toElement) {
143        final SortedSet<E> sub = decorated().headSet(toElement);
144        return new UnmodifiableSortedSet<E>(sub);
145    }
146
147    @Override
148    public SortedSet<E> tailSet(final E fromElement) {
149        final SortedSet<E> sub = decorated().tailSet(fromElement);
150        return new UnmodifiableSortedSet<E>(sub);
151    }
152
153}