/** * Factory method to create an unmodifiable queue. * <p> * If the queue passed in is already unmodifiable, it is returned. * * @param <E> the type of the elements in the queue * @param queue the queue to decorate, must not be null * @return an unmodifiable Queue * @throws NullPointerException if queue is null */ public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) { if (queue instanceof Unmodifiable) { @SuppressWarnings("unchecked") // safe to upcast final Queue<E> tmpQueue = (Queue<E>) queue; return tmpQueue; } return new UnmodifiableQueue<>(queue); }
/** * Returns an unmodifiable queue backed by the given queue. * * @param <E> the type of the elements in the queue * @param queue the queue to make unmodifiable, must not be null * @return an unmodifiable queue backed by that queue * @throws NullPointerException if the queue is null */ public static <E> Queue<E> unmodifiableQueue(final Queue<? extends E> queue) { return UnmodifiableQueue.unmodifiableQueue(queue); }
/** * Write the collection out using a custom routine. * * @param out the output stream * @throws IOException if an I/O error occurs while writing to the output stream */ private void writeObject(final ObjectOutputStream out) throws IOException { out.defaultWriteObject(); out.writeObject(decorated()); }
/** * Read the collection in using a custom routine. * * @param in the input stream * @throws IOException if an I/O error occurs while reading from the input stream * @throws ClassNotFoundException if the class of a serialized object can not be found */ @SuppressWarnings("unchecked") private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); setCollection((Collection<E>) in.readObject()); }
@Override public Iterator<E> iterator() { return UnmodifiableIterator.unmodifiableIterator(decorated().iterator()); }