    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.multiset;
    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.MultiSet;
    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 MultiSet} to ensure it can't be altered.
    034 * <p>
    035 * Attempts to modify it will result in an UnsupportedOperationException.
    036 * </p>
    037 *
    038 * @param <E> the type held in the multiset
    039 * @since 4.1
    040 */
    041public final class UnmodifiableMultiSet<E>
    042        extends AbstractMultiSetDecorator<E> implements Unmodifiable {
    043
    044    /** Serialization version */
    045    private static final long serialVersionUID = 20150611L;
    046
    047    /**
    048     * Factory method to create an unmodifiable multiset.
    049     * <p>
    050     * If the multiset passed in is already unmodifiable, it is returned.
    051     * </p>
    052     *
    053     * @param <E>  the type of the elements in the multiset
    054     * @param multiset  the multiset to decorate, may not be null
    055     * @return an unmodifiable MultiSet
    056     * @throws NullPointerException if multiset is null
    057     */
    058    public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
    059        if (multiset instanceof Unmodifiable) {
    060            @SuppressWarnings("unchecked") // safe to upcast
    061            final MultiSet<E> tmpMultiSet = (MultiSet<E>) multiset;
    062            return tmpMultiSet;
    063        }
    064        return new UnmodifiableMultiSet<>(multiset);
    065    }
    066
    067    /**
    068     * Constructor that wraps (not copies).
    069     *
    070     * @param multiset  the multiset to decorate, may not be null
    071     * @throws NullPointerException if multiset is null
    072     */
    073    @SuppressWarnings("unchecked") // safe to upcast
    074    private UnmodifiableMultiSet(final MultiSet<? extends E> multiset) {
    075        super((MultiSet<E>) multiset);
    076    }
    077
    078    @Override
    079    public boolean add(final E object) {
    080        throw new UnsupportedOperationException();
    081    }
    082
    083    @Override
    084    public int 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 Set<MultiSet.Entry<E>> entrySet() {
    100        final Set<MultiSet.Entry<E>> set = decorated().entrySet();
    101        return UnmodifiableSet.unmodifiableSet(set);
    102    }
    103
    104    @Override
    105    public Iterator<E> iterator() {
    106        return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator());
    107    }
    108
    109    /**
    110     * Deserializes the collection in using a custom routine.
    111     *
    112     * @param in  the input stream
    113     * @throws IOException if an error occurs while reading from the stream
    114     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
    115     * @throws ClassCastException if deserialized object has wrong type
    116     */
    117    @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
    118    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    119        in.defaultReadObject();
    120        setCollection((Collection<E>) in.readObject());
    121    }
    122
    123    @Override
    124    public boolean remove(final Object object) {
    125        throw new UnsupportedOperationException();
    126    }
    127
    128    @Override
    129    public int remove(final Object object, final int count) {
    130        throw new UnsupportedOperationException();
    131    }
    132
    133    @Override
    134    public boolean removeAll(final Collection<?> coll) {
    135        throw new UnsupportedOperationException();
    136    }
    137
    138    /**
    139     * @since 4.4
    140     */
    141    @Override
    142    public boolean removeIf(final Predicate<? super E> filter) {
    143        throw new UnsupportedOperationException();
    144    }
    145
    146    @Override
    147    public boolean retainAll(final Collection<?> coll) {
    148        throw new UnsupportedOperationException();
    149    }
    150
    151    @Override
    152    public int setCount(final E object, final int count) {
    153        throw new UnsupportedOperationException();
    154    }
    155
    156    @Override
    157    public Set<E> uniqueSet() {
    158        final Set<E> set = decorated().uniqueSet();
    159        return UnmodifiableSet.unmodifiableSet(set);
    160    }
    161
    162    /**
    163     * Serializes this object to an ObjectOutputStream.
    164     *
    165     * @param out the target ObjectOutputStream.
    166     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    167     */
    168    private void writeObject(final ObjectOutputStream out) throws IOException {
    169        out.defaultWriteObject();
    170        out.writeObject(decorated());
    171    }
    172
    173}