PushbackIterator.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.iterators;

  18. import java.util.ArrayDeque;
  19. import java.util.Deque;
  20. import java.util.Iterator;
  21. import java.util.Objects;

  22. /**
  23.  * Decorates an iterator to support pushback of elements.
  24.  * <p>
  25.  * The decorator stores the pushed back elements in a LIFO manner: the last element
  26.  * that has been pushed back, will be returned as the next element in a call to {@link #next()}.
  27.  * </p>
  28.  * <p>
  29.  * The decorator does not support the removal operation. Any call to {@link #remove()} will
  30.  * result in an {@link UnsupportedOperationException}.
  31.  * </p>
  32.  *
  33.  * @param <E> the type of elements returned by this iterator.
  34.  * @since 4.0
  35.  */
  36. public class PushbackIterator<E> implements Iterator<E> {

  37.     /**
  38.      * Decorates the specified iterator to support one-element lookahead.
  39.      * <p>
  40.      * If the iterator is already a {@link PushbackIterator} it is returned directly.
  41.      * </p>
  42.      *
  43.      * @param <E>  the element type
  44.      * @param iterator  the iterator to decorate
  45.      * @return a new peeking iterator
  46.      * @throws NullPointerException if the iterator is null
  47.      */
  48.     public static <E> PushbackIterator<E> pushbackIterator(final Iterator<? extends E> iterator) {
  49.         Objects.requireNonNull(iterator, "iterator");
  50.         if (iterator instanceof PushbackIterator<?>) {
  51.             @SuppressWarnings("unchecked") // safe cast
  52.             final PushbackIterator<E> it = (PushbackIterator<E>) iterator;
  53.             return it;
  54.         }
  55.         return new PushbackIterator<>(iterator);
  56.     }

  57.     /** The iterator being decorated. */
  58.     private final Iterator<? extends E> iterator;

  59.     /** The LIFO queue containing the pushed back items. */
  60.     private final Deque<E> items = new ArrayDeque<>();

  61.     /**
  62.      * Constructs a new instance.
  63.      *
  64.      * @param iterator  the iterator to decorate
  65.      */
  66.     public PushbackIterator(final Iterator<? extends E> iterator) {
  67.         this.iterator = iterator;
  68.     }

  69.     @Override
  70.     public boolean hasNext() {
  71.         return !items.isEmpty() || iterator.hasNext();
  72.     }

  73.     @Override
  74.     public E next() {
  75.         return !items.isEmpty() ? items.pop() : iterator.next();
  76.     }

  77.     /**
  78.      * Push back the given element to the iterator.
  79.      * <p>
  80.      * Calling {@link #next()} immediately afterwards will return exactly this element.
  81.      * </p>
  82.      *
  83.      * @param item  the element to push back to the iterator
  84.      */
  85.     public void pushback(final E item) {
  86.         items.push(item);
  87.     }

  88.     /**
  89.      * This iterator will always throw an {@link UnsupportedOperationException}.
  90.      *
  91.      * @throws UnsupportedOperationException always
  92.      */
  93.     @Override
  94.     public void remove() {
  95.         throw new UnsupportedOperationException();
  96.     }

  97. }