UnmodifiableMultiSet.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.multiset;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.util.Collection;
  22. import java.util.Iterator;
  23. import java.util.Set;
  24. import java.util.function.Predicate;

  25. import org.apache.commons.collections4.MultiSet;
  26. import org.apache.commons.collections4.Unmodifiable;
  27. import org.apache.commons.collections4.iterators.UnmodifiableIterator;
  28. import org.apache.commons.collections4.set.UnmodifiableSet;

  29. /**
  30.  * Decorates another {@link MultiSet} to ensure it can't be altered.
  31.  * <p>
  32.  * Attempts to modify it will result in an UnsupportedOperationException.
  33.  * </p>
  34.  *
  35.  * @param <E> the type held in the multiset
  36.  * @since 4.1
  37.  */
  38. public final class UnmodifiableMultiSet<E>
  39.         extends AbstractMultiSetDecorator<E> implements Unmodifiable {

  40.     /** Serialization version */
  41.     private static final long serialVersionUID = 20150611L;

  42.     /**
  43.      * Factory method to create an unmodifiable multiset.
  44.      * <p>
  45.      * If the multiset passed in is already unmodifiable, it is returned.
  46.      * </p>
  47.      *
  48.      * @param <E>  the type of the elements in the multiset
  49.      * @param multiset  the multiset to decorate, may not be null
  50.      * @return an unmodifiable MultiSet
  51.      * @throws NullPointerException if multiset is null
  52.      */
  53.     public static <E> MultiSet<E> unmodifiableMultiSet(final MultiSet<? extends E> multiset) {
  54.         if (multiset instanceof Unmodifiable) {
  55.             @SuppressWarnings("unchecked") // safe to upcast
  56.             final MultiSet<E> tmpMultiSet = (MultiSet<E>) multiset;
  57.             return tmpMultiSet;
  58.         }
  59.         return new UnmodifiableMultiSet<>(multiset);
  60.     }

  61.     /**
  62.      * Constructor that wraps (not copies).
  63.      *
  64.      * @param multiset  the multiset to decorate, may not be null
  65.      * @throws NullPointerException if multiset is null
  66.      */
  67.     @SuppressWarnings("unchecked") // safe to upcast
  68.     private UnmodifiableMultiSet(final MultiSet<? extends E> multiset) {
  69.         super((MultiSet<E>) multiset);
  70.     }

  71.     @Override
  72.     public boolean add(final E object) {
  73.         throw new UnsupportedOperationException();
  74.     }

  75.     @Override
  76.     public int add(final E object, final int count) {
  77.         throw new UnsupportedOperationException();
  78.     }

  79.     @Override
  80.     public boolean addAll(final Collection<? extends E> coll) {
  81.         throw new UnsupportedOperationException();
  82.     }

  83.     @Override
  84.     public void clear() {
  85.         throw new UnsupportedOperationException();
  86.     }

  87.     @Override
  88.     public Set<MultiSet.Entry<E>> entrySet() {
  89.         final Set<MultiSet.Entry<E>> set = decorated().entrySet();
  90.         return UnmodifiableSet.unmodifiableSet(set);
  91.     }

  92.     @Override
  93.     public Iterator<E> iterator() {
  94.         return UnmodifiableIterator.<E>unmodifiableIterator(decorated().iterator());
  95.     }

  96.     /**
  97.      * Deserializes the collection in using a custom routine.
  98.      *
  99.      * @param in  the input stream
  100.      * @throws IOException if an error occurs while reading from the stream
  101.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  102.      * @throws ClassCastException if deserialized object has wrong type
  103.      */
  104.     @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
  105.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  106.         in.defaultReadObject();
  107.         setCollection((Collection<E>) in.readObject());
  108.     }

  109.     @Override
  110.     public boolean remove(final Object object) {
  111.         throw new UnsupportedOperationException();
  112.     }

  113.     @Override
  114.     public int remove(final Object object, final int count) {
  115.         throw new UnsupportedOperationException();
  116.     }

  117.     @Override
  118.     public boolean removeAll(final Collection<?> coll) {
  119.         throw new UnsupportedOperationException();
  120.     }

  121.     /**
  122.      * @since 4.4
  123.      */
  124.     @Override
  125.     public boolean removeIf(final Predicate<? super E> filter) {
  126.         throw new UnsupportedOperationException();
  127.     }

  128.     @Override
  129.     public boolean retainAll(final Collection<?> coll) {
  130.         throw new UnsupportedOperationException();
  131.     }

  132.     @Override
  133.     public int setCount(final E object, final int count) {
  134.         throw new UnsupportedOperationException();
  135.     }

  136.     @Override
  137.     public Set<E> uniqueSet() {
  138.         final Set<E> set = decorated().uniqueSet();
  139.         return UnmodifiableSet.unmodifiableSet(set);
  140.     }

  141.     /**
  142.      * Serializes this object to an ObjectOutputStream.
  143.      *
  144.      * @param out the target ObjectOutputStream.
  145.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  146.      */
  147.     private void writeObject(final ObjectOutputStream out) throws IOException {
  148.         out.defaultWriteObject();
  149.         out.writeObject(decorated());
  150.     }

  151. }