/**
  * Gets the next element in the iteration.
  *
  * @return the next element in the iteration.
  * @throws java.util.NoSuchElementException if the iteration has exhausted all elements.
  * @see java.util.Iterator#next()
  * @see #hasNext()
  */
 @Override
 public T next() {
   Assert.isTrue(hasNext(), new NoSuchElementException("The iteration has no more elements!"));
   T value = iterators.peek().next();
   nextCalled.set(true);
   return value;
 }
  /**
   * Constructs an instance of the BreadthFirstIterator class wrapping the Iterator of Iterators to
   * collectively traverse the Iterators in order, left to right, top to bottom.
   *
   * @param iterators the Iterator of Iterators to encapsulate in a breadth-first traversal.
   * @throws java.lang.NullPointerException if the Iterator of Iterators reference is null.
   * @see java.util.Iterator
   */
  public BreadthFirstIterator(final Iterator<Iterator<T>> iterators) {
    Assert.notNull(iterators, "The Iterator of Iterators must not be null!");

    for (Iterator<T> iterator : CollectionUtils.iterable(iterators)) {
      if (iterator != null) {
        this.iterators.add(iterator);
      }
    }
  }
  /**
   * Determines whether another element is available in the iteration.
   *
   * @return a boolean value indicating whether another element is available in the iteration.
   * @see java.util.Iterator#hasNext()
   */
  @Override
  public boolean hasNext() {
    while (!(iterators.isEmpty() || iterators.peek().hasNext())) {
      Assert.isFalse(
          iterators.removeFirst().hasNext(),
          new IllegalStateException("removing a non-empty Iterator"));
    }

    return (!iterators.isEmpty() && iterators.peek().hasNext());
  }
 /**
  * Removes the current element in the iteration.
  *
  * @throws java.lang.IllegalStateException if the next method has not yet been called, or the
  *     remove method has already been called after the last call to the next method.
  * @see java.util.Iterator#remove()
  */
 @Override
 public void remove() {
   Assert.state(nextCalled.compareAndSet(true, false), "next was not called before remove");
   iterators.peek().remove();
 }
 /**
  * Constructs an instance of the {@link RegexFileFilter} class initialized with the given regular
  * expression (regex), expressed as a {@link Pattern}.
  *
  * @param regularExpression regular expression (regex) used to match and filter {@link File}s.
  * @throws IllegalArgumentException if the regular expression {@link Pattern} is null.
  * @see java.util.regex.Pattern
  */
 public RegexFileFilter(Pattern regularExpression) {
   Assert.notNull(regularExpression, "The Regular Expression (Pattern) cannot be null");
   this.regularExpression = regularExpression;
 }