    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.bag;
    018
    019import java.io.IOException;
    020import java.io.ObjectInputStream;
    021import java.io.ObjectOutputStream;
    022import java.util.Collection;
    023import java.util.Iterator;
    024import java.util.Set;
    025
    026import org.apache.commons.collections4.SortedBag;
    027import org.apache.commons.collections4.Unmodifiable;
    028import org.apache.commons.collections4.iterators.UnmodifiableIterator;
    029import org.apache.commons.collections4.set.UnmodifiableSet;
    030
    031/**
    032 * Decorates another {@link SortedBag} to ensure it can't be altered.
    033 * <p>
    034 * This class is Serializable from Commons Collections 3.1.
    035 * <p>
    036 * Attempts to modify it will result in an UnsupportedOperationException.
    037 *
    038 * @param <E> the type of elements in this bag
    039 * @since 3.0
    040 */
    041public final class UnmodifiableSortedBag<E>
    042        extends AbstractSortedBagDecorator<E> implements Unmodifiable {
    043
    044    /** Serialization version */
    045    private static final long serialVersionUID = -3190437252665717841L;
    046
    047    /**
    048     * Factory method to create an unmodifiable bag.
    049     * <p>
    050     * If the bag passed in is already unmodifiable, it is returned.
    051     *
    052     * @param <E> the type of the elements in the bag
    053     * @param bag  the bag to decorate, must not be null
    054     * @return an unmodifiable SortedBag
    055     * @throws NullPointerException if bag is null
    056     * @since 4.0
    057     */
    058    public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) {
    059        if (bag instanceof Unmodifiable) {
    060            return bag;
    061        }
    062        return new UnmodifiableSortedBag<>(bag);
    063    }
    064
    065    //-----------------------------------------------------------------------
    066    /**
    067     * Constructor that wraps (not copies).
    068     *
    069     * @param bag  the bag to decorate, must not be null
    070     * @throws NullPointerException if bag is null
    071     */
    072    private UnmodifiableSortedBag(final SortedBag<E> bag) {
    073        super(bag);
    074    }
    075
    076    //-----------------------------------------------------------------------
    077    /**
    078     * Write the collection out using a custom routine.
    079     *
    080     * @param out  the output stream
    081     * @throws IOException if an error occurs while writing to the stream
    082     */
    083    private void writeObject(final ObjectOutputStream out) throws IOException {
    084        out.defaultWriteObject();
    085        out.writeObject(decorated());
    086    }
    087
    088    /**
    089     * Read the collection in using a custom routine.
    090     *
    091     * @param in  the input stream
    092     * @throws IOException if an error occurs while reading from the stream
    093     * @throws ClassNotFoundException if an object read from the stream can not be loaded
    094     * @throws ClassCastException if deserialised object has wrong type
    095     */
    096    @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
    097    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    098        in.defaultReadObject();
    099        setCollection((Collection<E>) in.readObject());
    100    }
    101
    102    //-----------------------------------------------------------------------
    103    @Override
    104    public Iterator<E> iterator() {
    105        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
    106    }
    107
    108    @Override
    109    public boolean add(final E object) {
    110        throw new UnsupportedOperationException();
    111    }
    112
    113    @Override
    114    public boolean addAll(final Collection<? extends E> coll) {
    115        throw new UnsupportedOperationException();
    116    }
    117
    118    @Override
    119    public void clear() {
    120        throw new UnsupportedOperationException();
    121    }
    122
    123    @Override
    124    public boolean remove(final Object object) {
    125        throw new UnsupportedOperationException();
    126    }
    127
    128    @Override
    129    public boolean removeAll(final Collection<?> coll) {
    130        throw new UnsupportedOperationException();
    131    }
    132
    133    @Override
    134    public boolean retainAll(final Collection<?> coll) {
    135        throw new UnsupportedOperationException();
    136    }
    137
    138    //-----------------------------------------------------------------------
    139    @Override
    140    public boolean add(final E object, final int count) {
    141        throw new UnsupportedOperationException();
    142    }
    143
    144    @Override
    145    public boolean remove(final Object object, final int count) {
    146        throw new UnsupportedOperationException();
    147    }
    148
    149    @Override
    150    public Set<E> uniqueSet() {
    151        final Set<E> set = decorated().uniqueSet();
    152        return UnmodifiableSet.unmodifiableSet(set);
    153    }
    154
    155}