    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 *      https://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.multiset.UnmodifiableSortedMultiSet;
    031import org.apache.commons.collections4.set.UnmodifiableSet;
    032
    033/**
    034 * Decorates another {@link SortedBag} to ensure it can't be altered.
    035 * <p>
    036 * This class is Serializable from Commons Collections 3.1.
    037 * </p>
    038 * <p>
    039 * Attempts to modify it will result in an UnsupportedOperationException.
    040 * </p>
    041 *
    042 * @param <E> The type of elements in this bag
    043 * @since 3.0
    044 * @deprecated Since 4.6.0, use {@link UnmodifiableSortedMultiSet} instead.
    045 */
    046@Deprecated
    047public final class UnmodifiableSortedBag<E>
    048        extends AbstractSortedBagDecorator<E> implements Unmodifiable {
    049
    050    /** Serialization version */
    051    private static final long serialVersionUID = -3190437252665717841L;
    052
    053    /**
    054     * Factory method to create an unmodifiable bag.
    055     * <p>
    056     * If the bag passed in is already unmodifiable, it is returned.
    057     *
    058     * @param <E> The type of the elements in the bag
    059     * @param bag  The bag to decorate, must not be null
    060     * @return An unmodifiable SortedBag
    061     * @throws NullPointerException if bag is null
    062     * @since 4.0
    063     */
    064    public static <E> SortedBag<E> unmodifiableSortedBag(final SortedBag<E> bag) {
    065        if (bag instanceof Unmodifiable) {
    066            return bag;
    067        }
    068        return new UnmodifiableSortedBag<>(bag);
    069    }
    070
    071    /**
    072     * Constructor that wraps (not copies).
    073     *
    074     * @param bag  The bag to decorate, must not be null
    075     * @throws NullPointerException if bag is null
    076     */
    077    private UnmodifiableSortedBag(final SortedBag<E> bag) {
    078        super(bag);
    079    }
    080
    081    /**
    082     * Always throws {@link UnsupportedOperationException}.
    083     *
    084     * @param object Ignored.
    085     * @throws UnsupportedOperationException Always thrown.
    086     */
    087    @Override
    088    public boolean add(final E object) {
    089        throw new UnsupportedOperationException();
    090    }
    091
    092    /**
    093     * Always throws {@link UnsupportedOperationException}.
    094     *
    095     * @param object Ignored.
    096     * @param count Ignored.
    097     * @throws UnsupportedOperationException Always thrown.
    098     */
    099    @Override
    100    public boolean add(final E object, final int count) {
    101        throw new UnsupportedOperationException();
    102    }
    103
    104    /**
    105     * Always throws {@link UnsupportedOperationException}.
    106     *
    107     * @param coll Ignored.
    108     * @throws UnsupportedOperationException Always thrown.
    109     */
    110    @Override
    111    public boolean addAll(final Collection<? extends E> coll) {
    112        throw new UnsupportedOperationException();
    113    }
    114
    115    /**
    116     * Always throws {@link UnsupportedOperationException}.
    117     *
    118     * @throws UnsupportedOperationException Always thrown.
    119     */
    120    @Override
    121    public void clear() {
    122        throw new UnsupportedOperationException();
    123    }
    124
    125    @Override
    126    public Iterator<E> iterator() {
    127        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
    128    }
    129
    130    /**
    131     * Deserializes the collection in using a custom routine.
    132     *
    133     * @param in  The input stream
    134     * @throws IOException Thrown if an error occurs while reading from the stream
    135     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
    136     * @throws ClassCastException if deserialized object has wrong type
    137     */
    138    @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
    139    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    140        in.defaultReadObject();
    141        setCollection((Collection<E>) in.readObject());
    142    }
    143
    144    /**
    145     * Always throws {@link UnsupportedOperationException}.
    146     *
    147     * @param object Ignored.
    148     * @throws UnsupportedOperationException Always thrown.
    149     */
    150    @Override
    151    public boolean remove(final Object object) {
    152        throw new UnsupportedOperationException();
    153    }
    154
    155    /**
    156     * Always throws {@link UnsupportedOperationException}.
    157     *
    158     * @param object Ignored.
    159     * @param count Ignored.
    160     * @throws UnsupportedOperationException Always thrown.
    161     */
    162    @Override
    163    public boolean remove(final Object object, final int count) {
    164        throw new UnsupportedOperationException();
    165    }
    166
    167    /**
    168     * Always throws {@link UnsupportedOperationException}.
    169     *
    170     * @param coll Ignored.
    171     * @throws UnsupportedOperationException Always thrown.
    172     */
    173    @Override
    174    public boolean removeAll(final Collection<?> coll) {
    175        throw new UnsupportedOperationException();
    176    }
    177
    178    /**
    179     * Always throws {@link UnsupportedOperationException}.
    180     *
    181     * @param filter Ignored.
    182     * @throws UnsupportedOperationException Always thrown.
    183     * @since 4.4
    184     */
    185    @Override
    186    public boolean removeIf(final Predicate<? super E> filter) {
    187        throw new UnsupportedOperationException();
    188    }
    189
    190    /**
    191     * Always throws {@link UnsupportedOperationException}.
    192     *
    193     * @param coll Ignored.
    194     * @throws UnsupportedOperationException Always thrown.
    195     */
    196    @Override
    197    public boolean retainAll(final Collection<?> coll) {
    198        throw new UnsupportedOperationException();
    199    }
    200
    201    @Override
    202    public Set<E> uniqueSet() {
    203        final Set<E> set = decorated().uniqueSet();
    204        return UnmodifiableSet.unmodifiableSet(set);
    205    }
    206
    207    /**
    208     * Serializes this object to an ObjectOutputStream.
    209     *
    210     * @param out The target ObjectOutputStream.
    211     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    212     */
    213    private void writeObject(final ObjectOutputStream out) throws IOException {
    214        out.defaultWriteObject();
    215        out.writeObject(decorated());
    216    }
    217
    218}