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.Bag;
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 Bag} 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 UnmodifiableBag<E>
042        extends AbstractBagDecorator<E> implements Unmodifiable {
043
044    /** Serialization version */
045    private static final long serialVersionUID = -1873799975157099624L;
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 Bag
055     * @throws NullPointerException if bag is null
056     * @since 4.0
057     */
058    public static <E> Bag<E> unmodifiableBag(final Bag<? extends E> bag) {
059        if (bag instanceof Unmodifiable) {
060            @SuppressWarnings("unchecked") // safe to upcast
061            final Bag<E> tmpBag = (Bag<E>) bag;
062            return tmpBag;
063        }
064        return new UnmodifiableBag<>(bag);
065    }
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    @SuppressWarnings("unchecked") // safe to upcast
075    private UnmodifiableBag(final Bag<? extends E> bag) {
076        super((Bag<E>) bag);
077    }
078
079    //-----------------------------------------------------------------------
080    /**
081     * Write the collection out using a custom routine.
082     *
083     * @param out  the output stream
084     * @throws IOException if an error occurs while writing to the stream
085     */
086    private void writeObject(final ObjectOutputStream out) throws IOException {
087        out.defaultWriteObject();
088        out.writeObject(decorated());
089    }
090
091    /**
092     * Read the collection in using a custom routine.
093     *
094     * @param in  the input stream
095     * @throws IOException if an error occurs while reading from the stream
096     * @throws ClassNotFoundException if an object read from the stream can not be loaded
097     * @throws ClassCastException if deserialised object has wrong type
098     */
099    @SuppressWarnings("unchecked") // will throw CCE, see Javadoc
100    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
101        in.defaultReadObject();
102        setCollection((Collection<E>) in.readObject());
103    }
104
105    //-----------------------------------------------------------------------
106    @Override
107    public Iterator<E> iterator() {
108        return UnmodifiableIterator.<E> unmodifiableIterator(decorated().iterator());
109    }
110
111    @Override
112    public boolean add(final E object) {
113        throw new UnsupportedOperationException();
114    }
115
116    @Override
117    public boolean addAll(final Collection<? extends E> coll) {
118        throw new UnsupportedOperationException();
119    }
120
121    @Override
122    public void clear() {
123        throw new UnsupportedOperationException();
124    }
125
126    @Override
127    public boolean remove(final Object object) {
128        throw new UnsupportedOperationException();
129    }
130
131    @Override
132    public boolean removeAll(final Collection<?> coll) {
133        throw new UnsupportedOperationException();
134    }
135
136    @Override
137    public boolean retainAll(final Collection<?> coll) {
138        throw new UnsupportedOperationException();
139    }
140
141    //-----------------------------------------------------------------------
142    @Override
143    public boolean add(final E object, final int count) {
144        throw new UnsupportedOperationException();
145    }
146
147    @Override
148    public boolean remove(final Object object, final int count) {
149        throw new UnsupportedOperationException();
150    }
151
152    @Override
153    public Set<E> uniqueSet() {
154        final Set<E> set = decorated().uniqueSet();
155        return UnmodifiableSet.<E> unmodifiableSet(set);
156    }
157
158}