    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.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    @Override
    077    public boolean add(final Object object) {
    078        throw new UnsupportedOperationException();
    079    }
    080
    081    @Override
    082    public boolean addAll(final Collection<? extends E> coll) {
    083        throw new UnsupportedOperationException();
    084    }
    085
    086    @Override
    087    public void clear() {
    088        throw new UnsupportedOperationException();
    089    }
    090
    091    @Override
    092    public Iterator<E> iterator() {
    093        return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
    094    }
    095
    096    @Override
    097    public boolean offer(final E obj) {
    098        throw new UnsupportedOperationException();
    099    }
    100
    101    @Override
    102    public E poll() {
    103        throw new UnsupportedOperationException();
    104    }
    105
    106    /**
    107     * Deserializes the collection in using a custom routine.
    108     *
    109     * @param in  the input stream
    110     * @throws IOException if an I/O error occurs while reading from the input stream
    111     * @throws ClassNotFoundException if the class of a serialized object cannot be found
    112     */
    113    @SuppressWarnings("unchecked")
    114    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    115        in.defaultReadObject();
    116        setCollection((Collection<E>) in.readObject());
    117    }
    118
    119    @Override
    120    public E remove() {
    121        throw new UnsupportedOperationException();
    122    }
    123
    124    @Override
    125    public boolean remove(final Object object) {
    126        throw new UnsupportedOperationException();
    127    }
    128
    129    @Override
    130    public boolean removeAll(final Collection<?> coll) {
    131        throw new UnsupportedOperationException();
    132    }
    133
    134    /**
    135     * @since 4.4
    136     */
    137    @Override
    138    public boolean removeIf(final Predicate<? super E> filter) {
    139        throw new UnsupportedOperationException();
    140    }
    141
    142    @Override
    143    public boolean retainAll(final Collection<?> coll) {
    144        throw new UnsupportedOperationException();
    145    }
    146
    147    /**
    148     * Serializes this object to an ObjectOutputStream.
    149     *
    150     * @param out the target ObjectOutputStream.
    151     * @throws IOException thrown when an I/O errors occur writing to the target stream.
    152     */
    153    private void writeObject(final ObjectOutputStream out) throws IOException {
    154        out.defaultWriteObject();
    155        out.writeObject(decorated());
    156    }
    157
    158}