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