View Javadoc

    1   /*
    2    * Copyright 2005 John G. Wilson
    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
    18  package groovy.util.slurpersupport;
    19
    20  import java.util.Iterator;
    21
    22  /***
    23   * @author John Wilson
    24   *
    25   */
    26
    27  public abstract class NodeIterator implements Iterator {
    28  private final Iterator iter;
    29  private Object nextNode;
    30
    31    public NodeIterator(final Iterator iter) {
    32      this.iter = iter;
    33      this.nextNode = getNextNode(iter);
    34    }
    35
    36    public boolean hasNext() {
    37      return this.nextNode != null;
    38    }
    39
    40    public Object next() {
    41      try {
    42        return this.nextNode;
    43      } finally {
    44        this.nextNode = getNextNode(this.iter);
    45      }
    46    }
    47
    48    public void remove() {
    49      throw new UnsupportedOperationException();
    50    }
    51
    52    protected abstract Object getNextNode(final Iterator iter);
    53  }