001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.iterators; 018 019import java.util.Iterator; 020import java.util.NoSuchElementException; 021import java.util.Objects; 022 023/** 024 * Decorates another iterator to return elements in a specific range. 025 * <p> 026 * The decorated iterator is bounded in the range [offset, offset+max). 027 * The {@code offset} corresponds to the position of the first element to 028 * be returned from the decorated iterator, and {@code max} is the maximum 029 * number of elements to be returned at most. 030 * </p> 031 * <p> 032 * In case an offset parameter other than 0 is provided, the decorated 033 * iterator is immediately advanced to this position, skipping all elements 034 * before that position. 035 * </p> 036 * 037 * @param <E> the type of elements returned by this iterator. 038 * @since 4.1 039 */ 040public class BoundedIterator<E> implements Iterator<E> { 041 042 /** The iterator being decorated. */ 043 private final Iterator<? extends E> iterator; 044 045 /** The offset to bound the first element return */ 046 private final long offset; 047 048 /** The max number of elements to return */ 049 private final long max; 050 051 /** The position of the current element */ 052 private long pos; 053 054 /** 055 * Decorates the specified iterator to return at most the given number of elements, 056 * skipping all elements until the iterator reaches the position at {@code offset}. 057 * <p> 058 * The iterator is immediately advanced until it reaches the position at {@code offset}, 059 * incurring O(n) time. 060 * </p> 061 * 062 * @param iterator the iterator to be decorated 063 * @param offset the index of the first element of the decorated iterator to return 064 * @param max the maximum number of elements of the decorated iterator to return 065 * @throws NullPointerException if iterator is null 066 * @throws IllegalArgumentException if either offset or max is negative 067 */ 068 public BoundedIterator(final Iterator<? extends E> iterator, final long offset, final long max) { 069 if (offset < 0) { 070 throw new IllegalArgumentException("Offset parameter must not be negative."); 071 } 072 if (max < 0) { 073 throw new IllegalArgumentException("Max parameter must not be negative."); 074 } 075 076 this.iterator = Objects.requireNonNull(iterator, "iterator"); 077 this.offset = offset; 078 this.max = max; 079 pos = 0; 080 init(); 081 } 082 083 /** 084 * Checks whether the iterator is still within its bounded range. 085 * @return {@code true} if the iterator is within its bounds, {@code false} otherwise 086 */ 087 private boolean checkBounds() { 088 if (pos - offset + 1 > max) { 089 return false; 090 } 091 return true; 092 } 093 094 @Override 095 public boolean hasNext() { 096 if (!checkBounds()) { 097 return false; 098 } 099 return iterator.hasNext(); 100 } 101 102 /** 103 * Advances the underlying iterator to the beginning of the bounded range. 104 */ 105 private void init() { 106 while (pos < offset && iterator.hasNext()) { 107 iterator.next(); 108 pos++; 109 } 110 } 111 112 @Override 113 public E next() { 114 if (!checkBounds()) { 115 throw new NoSuchElementException(); 116 } 117 final E next = iterator.next(); 118 pos++; 119 return next; 120 } 121 122 /** 123 * {@inheritDoc} 124 * <p> 125 * In case an offset other than 0 was specified, the underlying iterator will be advanced 126 * to this position upon creation. A call to {@link #remove()} will still result in an 127 * {@link IllegalStateException} if no explicit call to {@link #next()} has been made prior 128 * to calling {@link #remove()}. 129 * </p> 130 */ 131 @Override 132 public void remove() { 133 if (pos <= offset) { 134 throw new IllegalStateException("remove() cannot be called before calling next()"); 135 } 136 iterator.remove(); 137 } 138}