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