SkippingIterator.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.Iterator;

  19. /**
  20.  * Decorates another iterator to skip the first N elements.
  21.  * <p>
  22.  * In case an offset parameter other than 0 is provided, the decorated
  23.  * iterator is immediately advanced to this position, skipping all elements
  24.  * before that position.
  25.  * </p>
  26.  *
  27.  * @param <E> the type of elements returned by this iterator.
  28.  * @since 4.1
  29.  */
  30. public class SkippingIterator<E> extends AbstractIteratorDecorator<E> {

  31.     /** The offset to bound the first element return */
  32.     private final long offset;

  33.     /** The position of the current element */
  34.     private long pos;

  35.     /**
  36.      * Decorates the specified iterator to skip all elements until the iterator
  37.      * reaches the position at {@code offset}.
  38.      * <p>
  39.      * The iterator is immediately advanced until it reaches the position at {@code offset},
  40.      * incurring O(n) time.
  41.      *
  42.      * @param iterator  the iterator to be decorated
  43.      * @param offset  the index of the first element of the decorated iterator to return
  44.      * @throws NullPointerException if iterator is null
  45.      * @throws IllegalArgumentException if offset is negative
  46.      */
  47.     public SkippingIterator(final Iterator<E> iterator, final long offset) {
  48.         super(iterator);

  49.         if (offset < 0) {
  50.             throw new IllegalArgumentException("Offset parameter must not be negative.");
  51.         }

  52.         this.offset = offset;
  53.         this.pos = 0;
  54.         init();
  55.     }

  56.     /**
  57.      * Skips the given number of elements.
  58.      */
  59.     private void init() {
  60.         while (pos < offset && hasNext()) {
  61.             next();
  62.         }
  63.     }

  64.     @Override
  65.     public E next() {
  66.         final E next = super.next();
  67.         pos++;
  68.         return next;
  69.     }

  70.     /**
  71.      * {@inheritDoc}
  72.      * <p>
  73.      * In case an offset other than 0 was specified, the underlying iterator will be advanced
  74.      * to this position upon creation. A call to {@link #remove()} will still result in an
  75.      * {@link IllegalStateException} if no explicit call to {@link #next()} has been made prior
  76.      * to calling {@link #remove()}.
  77.      */
  78.     @Override
  79.     public void remove() {
  80.         if (pos <= offset) {
  81.             throw new IllegalStateException("remove() cannot be called before calling next()");
  82.         }
  83.         super.remove();
  84.     }

  85. }