IteratorChain.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.Collection;
  19. import java.util.Iterator;
  20. import java.util.LinkedList;
  21. import java.util.Objects;
  22. import java.util.Queue;

  23. /**
  24.  * An IteratorChain is an Iterator that wraps a number of Iterators.
  25.  * <p>
  26.  * This class makes multiple iterators look like one to the caller. When any
  27.  * method from the Iterator interface is called, the IteratorChain will delegate
  28.  * to a single underlying Iterator. The IteratorChain will invoke the Iterators
  29.  * in sequence until all Iterators are exhausted.
  30.  * </p>
  31.  * <p>
  32.  * Under many circumstances, linking Iterators together in this manner is more
  33.  * efficient (and convenient) than reading out the contents of each Iterator
  34.  * into a List and creating a new Iterator.
  35.  * </p>
  36.  * <p>
  37.  * Calling a method that adds new Iterator <i>after a method in the Iterator
  38.  * interface has been called</i> will result in an UnsupportedOperationException.
  39.  * </p>
  40.  * <p>
  41.  * NOTE: As from version 3.0, the IteratorChain may contain no iterators. In
  42.  * this case the class will function as an empty iterator.
  43.  * </p>
  44.  * <p>
  45.  * NOTE: As from version 4.0, the IteratorChain stores the iterators in a queue
  46.  * and removes any reference to them as soon as they are not used anymore. Thus,
  47.  * the methods {@code setIterator(Iterator)} and {@code getIterators()} have been
  48.  * removed and {@link #size()} will return the number of remaining iterators in
  49.  * the queue.
  50.  * </p>
  51.  *
  52.  * @param <E> the type of elements in this iterator.
  53.  * @since 2.1
  54.  */
  55. public class IteratorChain<E> implements Iterator<E> {

  56.     /** The chain of iterators */
  57.     private final Queue<Iterator<? extends E>> iteratorQueue = new LinkedList<>();

  58.     /** The current iterator */
  59.     private Iterator<? extends E> currentIterator;

  60.     /**
  61.      * The "last used" Iterator is the Iterator upon which next() or hasNext()
  62.      * was most recently called used for the remove() operation only
  63.      */
  64.     private Iterator<? extends E> lastUsedIterator;

  65.     /**
  66.      * ComparatorChain is "locked" after the first time compare(Object,Object)
  67.      * is called
  68.      */
  69.     private boolean isLocked;

  70.     /**
  71.      * Constructs an IteratorChain with no Iterators.
  72.      * <p>
  73.      * You will normally use {@link #addIterator(Iterator)} to add some
  74.      * iterators after using this constructor.
  75.      * </p>
  76.      */
  77.     public IteratorChain() {
  78.     }

  79.     /**
  80.      * Constructs a new {@code IteratorChain} over the collection of
  81.      * iterators.
  82.      * <p>
  83.      * This method takes a collection of iterators. The newly constructed
  84.      * iterator will iterate through each one of the input iterators in turn.
  85.      * </p>
  86.      *
  87.      * @param iteratorQueue the collection of iterators, not null
  88.      * @throws NullPointerException if iterators collection is or contains null
  89.      * @throws ClassCastException if iterators collection doesn't contain an
  90.      * iterator
  91.      */
  92.     public IteratorChain(final Collection<? extends Iterator<? extends E>> iteratorQueue) {
  93.         for (final Iterator<? extends E> iterator : iteratorQueue) {
  94.             addIterator(iterator);
  95.         }
  96.     }

  97.     /**
  98.      * Constructs an IteratorChain with a single Iterator.
  99.      * <p>
  100.      * This method takes one iterator. The newly constructed iterator will
  101.      * iterate through that iterator. Thus calling this constructor on its own
  102.      * will have no effect other than decorating the input iterator.
  103.      * </p>
  104.      * <p>
  105.      * You will normally use {@link #addIterator(Iterator)} to add some more
  106.      * iterators after using this constructor.
  107.      * </p>
  108.      *
  109.      * @param iterator the first child iterator in the IteratorChain, not null
  110.      * @throws NullPointerException if the iterator is null
  111.      */
  112.     public IteratorChain(final Iterator<? extends E> iterator) {
  113.         addIterator(iterator);
  114.     }

  115.     /**
  116.      * Constructs a new {@code IteratorChain} over the array of iterators.
  117.      * <p>
  118.      * This method takes an array of iterators. The newly constructed iterator
  119.      * will iterate through each one of the input iterators in turn.
  120.      * </p>
  121.      *
  122.      * @param iteratorQueue the array of iterators, not null
  123.      * @throws NullPointerException if iterators array is or contains null
  124.      */
  125.     public IteratorChain(final Iterator<? extends E>... iteratorQueue) {
  126.         for (final Iterator<? extends E> element : iteratorQueue) {
  127.             addIterator(element);
  128.         }
  129.     }

  130.     /**
  131.      * Constructs a new {@code IteratorChain} over the two given iterators.
  132.      * <p>
  133.      * This method takes two iterators. The newly constructed iterator will
  134.      * iterate through each one of the input iterators in turn.
  135.      * </p>
  136.      *
  137.      * @param first the first child iterator in the IteratorChain, not null
  138.      * @param second the second child iterator in the IteratorChain, not null
  139.      * @throws NullPointerException if either iterator is null
  140.      */
  141.     public IteratorChain(final Iterator<? extends E> first, final Iterator<? extends E> second) {
  142.         addIterator(first);
  143.         addIterator(second);
  144.     }

  145.     /**
  146.      * Add an Iterator to the end of the chain
  147.      *
  148.      * @param iterator Iterator to add
  149.      * @throws IllegalStateException if I've already started iterating
  150.      * @throws NullPointerException if the iterator is null
  151.      */
  152.     public void addIterator(final Iterator<? extends E> iterator) {
  153.         checkLocked();
  154.         iteratorQueue.add(Objects.requireNonNull(iterator, "iterator"));
  155.     }

  156.     /**
  157.      * Checks whether the iterator chain is now locked and in use.
  158.      */
  159.     private void checkLocked() {
  160.         if (isLocked) {
  161.             throw new UnsupportedOperationException("IteratorChain cannot be changed after the first use of a method from the Iterator interface");
  162.         }
  163.     }

  164.     /**
  165.      * Return true if any Iterator in the IteratorChain has a remaining element.
  166.      *
  167.      * @return true if elements remain
  168.      */
  169.     @Override
  170.     public boolean hasNext() {
  171.         lockChain();
  172.         updateCurrentIterator();
  173.         lastUsedIterator = currentIterator;
  174.         return currentIterator.hasNext();
  175.     }

  176.     /**
  177.      * Determine if modifications can still be made to the IteratorChain.
  178.      * IteratorChains cannot be modified once they have executed a method from
  179.      * the Iterator interface.
  180.      *
  181.      * @return true if IteratorChain cannot be modified, false if it can
  182.      */
  183.     public boolean isLocked() {
  184.         return isLocked;
  185.     }

  186.     /**
  187.      * Lock the chain so no more iterators can be added. This must be called
  188.      * from all Iterator interface methods.
  189.      */
  190.     private void lockChain() {
  191.         if (!isLocked) {
  192.             isLocked = true;
  193.         }
  194.     }

  195.     /**
  196.      * Returns the next Object of the current Iterator
  197.      *
  198.      * @return Object from the current Iterator
  199.      * @throws java.util.NoSuchElementException if all the Iterators are
  200.      * exhausted
  201.      */
  202.     @Override
  203.     public E next() {
  204.         lockChain();
  205.         updateCurrentIterator();
  206.         lastUsedIterator = currentIterator;

  207.         return currentIterator.next();
  208.     }

  209.     /**
  210.      * Removes from the underlying collection the last element returned by the
  211.      * Iterator. As with next() and hasNext(), this method calls remove() on the
  212.      * underlying Iterator. Therefore, this method may throw an
  213.      * UnsupportedOperationException if the underlying Iterator does not support
  214.      * this method.
  215.      *
  216.      * @throws UnsupportedOperationException if the remove operator is not
  217.      * supported by the underlying Iterator
  218.      * @throws IllegalStateException if the next method has not yet been called,
  219.      * or the remove method has already been called after the last call to the
  220.      * next method.
  221.      */
  222.     @Override
  223.     public void remove() {
  224.         lockChain();
  225.         if (currentIterator == null) {
  226.             updateCurrentIterator();
  227.         }
  228.         lastUsedIterator.remove();
  229.     }

  230.     /**
  231.      * Returns the remaining number of Iterators in the current IteratorChain.
  232.      *
  233.      * @return Iterator count
  234.      */
  235.     public int size() {
  236.         return iteratorQueue.size();
  237.     }

  238.     /**
  239.      * Updates the current iterator field to ensure that the current Iterator is
  240.      * not exhausted
  241.      */
  242.     protected void updateCurrentIterator() {
  243.         if (currentIterator == null) {
  244.             if (iteratorQueue.isEmpty()) {
  245.                 currentIterator = EmptyIterator.<E>emptyIterator();
  246.             } else {
  247.                 currentIterator = iteratorQueue.remove();
  248.             }
  249.             // set last used iterator here, in case the user calls remove
  250.             // before calling hasNext() or next() (although they shouldn't)
  251.             lastUsedIterator = currentIterator;
  252.         }
  253.         while (!currentIterator.hasNext() && !iteratorQueue.isEmpty()) {
  254.             currentIterator = iteratorQueue.remove();
  255.         }
  256.     }

  257. }