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