001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.GwtCompatible;
020import com.google.errorprone.annotations.CanIgnoreReturnValue;
021import java.util.Iterator;
022
023/**
024 * An iterator which forwards all its method calls to another iterator. Subclasses should override
025 * one or more methods to modify the behavior of the backing iterator as desired per the <a
026 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
027 *
028 * <p><b>{@code default} method warning:</b> This class forwards calls to <i>only some</i> {@code
029 * default} methods. Specifically, it forwards calls only for methods that existed <a
030 * href="https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html">before {@code default}
031 * methods were introduced</a>. For newer methods, like {@code forEachRemaining}, it inherits their
032 * default implementations. When those implementations invoke methods, they invoke methods on the
033 * {@code ForwardingIterator}.
034 *
035 * @author Kevin Bourrillion
036 * @since 2.0
037 */
038@GwtCompatible
039public abstract class ForwardingIterator<T> extends ForwardingObject implements Iterator<T> {
040
041  /** Constructor for use by subclasses. */
042  protected ForwardingIterator() {}
043
044  @Override
045  protected abstract Iterator<T> delegate();
046
047  @Override
048  public boolean hasNext() {
049    return delegate().hasNext();
050  }
051
052  @CanIgnoreReturnValue
053  @Override
054  public T next() {
055    return delegate().next();
056  }
057
058  @Override
059  public void remove() {
060    delegate().remove();
061  }
062}