NodeListIterator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.iterators;

  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;
  20. import java.util.Objects;

  21. import org.w3c.dom.Node;
  22. import org.w3c.dom.NodeList;

  23. /**
  24.  * An {@link Iterator} over a {@link NodeList}.
  25.  * <p>
  26.  * This iterator does not support {@link #remove()} as a {@link NodeList} does not support
  27.  * removal of items.
  28.  * </p>
  29.  *
  30.  * @since 4.0
  31.  * @see NodeList
  32.  */
  33. public class NodeListIterator implements Iterator<Node> {

  34.     /** The original NodeList instance */
  35.     private final NodeList nodeList;
  36.     /** The current iterator index */
  37.     private int index;

  38.     /**
  39.      * Convenience constructor, which creates a new NodeListIterator from
  40.      * the specified node's childNodes.
  41.      *
  42.      * @param node Node, whose child nodes are wrapped by this class. Must not be null
  43.      * @throws NullPointerException if node is null
  44.      */
  45.     public NodeListIterator(final Node node) {
  46.         Objects.requireNonNull(node, "node");
  47.         this.nodeList = node.getChildNodes();
  48.     }

  49.     /**
  50.      * Constructor, that creates a new NodeListIterator from the specified
  51.      * {@code org.w3c.NodeList}
  52.      *
  53.      * @param nodeList node list, which is wrapped by this class. Must not be null
  54.      * @throws NullPointerException if nodeList is null
  55.      */
  56.     public NodeListIterator(final NodeList nodeList) {
  57.         this.nodeList = Objects.requireNonNull(nodeList, "nodeList");
  58.     }

  59.     @Override
  60.     public boolean hasNext() {
  61.         return nodeList != null && index < nodeList.getLength();
  62.     }

  63.     @Override
  64.     public Node next() {
  65.         if (nodeList != null && index < nodeList.getLength()) {
  66.             return nodeList.item(index++);
  67.         }
  68.         throw new NoSuchElementException("underlying nodeList has no more elements");
  69.     }

  70.     /**
  71.      * Throws {@link UnsupportedOperationException}.
  72.      *
  73.      * @throws UnsupportedOperationException always
  74.      */
  75.     @Override
  76.     public void remove() {
  77.         throw new UnsupportedOperationException("remove() method not supported for a NodeListIterator.");
  78.     }
  79. }