Coverage Summary for Class: AbstractIndexedListIterator (com.google.common.collect)

Class Class, % Method, % Line, %
AbstractIndexedListIterator 100% (1/1) 37.5% (3/8) 47.1% (8/17)


1 /* 2  * Copyright (C) 2009 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect; 18  19 import static com.google.common.base.Preconditions.checkPositionIndex; 20  21 import com.google.common.annotations.GwtCompatible; 22 import java.util.ListIterator; 23 import java.util.NoSuchElementException; 24  25 /** 26  * This class provides a skeletal implementation of the {@link ListIterator} interface across a 27  * fixed number of elements that may be retrieved by position. It does not support {@link #remove}, 28  * {@link #set}, or {@link #add}. 29  * 30  * @author Jared Levy 31  */ 32 @GwtCompatible 33 abstract class AbstractIndexedListIterator<E> extends UnmodifiableListIterator<E> { 34  private final int size; 35  private int position; 36  37  /** Returns the element with the specified index. This method is called by {@link #next()}. */ 38  protected abstract E get(int index); 39  40  /** 41  * Constructs an iterator across a sequence of the given size whose initial position is 0. That 42  * is, the first call to {@link #next()} will return the first element (or throw {@link 43  * NoSuchElementException} if {@code size} is zero). 44  * 45  * @throws IllegalArgumentException if {@code size} is negative 46  */ 47  protected AbstractIndexedListIterator(int size) { 48  this(size, 0); 49  } 50  51  /** 52  * Constructs an iterator across a sequence of the given size with the given initial position. 53  * That is, the first call to {@link #nextIndex()} will return {@code position}, and the first 54  * call to {@link #next()} will return the element at that index, if available. Calls to {@link 55  * #previous()} can retrieve the preceding {@code position} elements. 56  * 57  * @throws IndexOutOfBoundsException if {@code position} is negative or is greater than {@code 58  * size} 59  * @throws IllegalArgumentException if {@code size} is negative 60  */ 61  protected AbstractIndexedListIterator(int size, int position) { 62  checkPositionIndex(position, size); 63  this.size = size; 64  this.position = position; 65  } 66  67  @Override 68  public final boolean hasNext() { 69  return position < size; 70  } 71  72  @Override 73  public final E next() { 74  if (!hasNext()) { 75  throw new NoSuchElementException(); 76  } 77  return get(position++); 78  } 79  80  @Override 81  public final int nextIndex() { 82  return position; 83  } 84  85  @Override 86  public final boolean hasPrevious() { 87  return position > 0; 88  } 89  90  @Override 91  public final E previous() { 92  if (!hasPrevious()) { 93  throw new NoSuchElementException(); 94  } 95  return get(--position); 96  } 97  98  @Override 99  public final int previousIndex() { 100  return position - 1; 101  } 102 }