TransformedIterator.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2012 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 17package com.google.common.collect; 18 19import static com.google.common.base.Preconditions.checkNotNull; 20 21import com.google.common.annotations.GwtCompatible; 22import com.google.common.base.Function; 23 24import java.util.Iterator; 25 26/** 27 * An iterator that transforms a backing iterator; for internal use. This avoids 28 * the object overhead of constructing a {@link Function} for internal methods. 29 * 30 * @author Louis Wasserman 31 */ 32@GwtCompatible 33abstract class TransformedIterator<F, T> implements Iterator<T> { 34 final Iterator<? extends F> backingIterator; 35 36 TransformedIterator(Iterator<? extends F> backingIterator) { 37 this.backingIterator = checkNotNull(backingIterator); 38 } 39 40 abstract T transform(F from); 41 42 public final boolean hasNext() { 43 return backingIterator.hasNext(); 44 } 45 46 public final T next() { 47 return transform(backingIterator.next()); 48 } 49 50 public final void remove() { 51 backingIterator.remove(); 52 } 53} 54