001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.iterators;
018
019import java.util.Iterator;
020import java.util.NoSuchElementException;
021import java.util.Objects;
022
023import org.w3c.dom.Node;
024import org.w3c.dom.NodeList;
025
026/**
027 * An {@link Iterator} over a {@link NodeList}.
028 * <p>
029 * This iterator does not support {@link #remove()} as a {@link NodeList} does not support
030 * removal of items.
031 *
032 * @since 4.0
033 * @see NodeList
034 */
035public class NodeListIterator implements Iterator<Node> {
036
037    /** The original NodeList instance */
038    private final NodeList nodeList;
039    /** The current iterator index */
040    private int index;
041
042    /**
043     * Convenience constructor, which creates a new NodeListIterator from
044     * the specified node's childNodes.
045     *
046     * @param node Node, whose child nodes are wrapped by this class. Must not be null
047     * @throws NullPointerException if node is null
048     */
049    public NodeListIterator(final Node node) {
050        Objects.requireNonNull(node, "node");
051        this.nodeList = node.getChildNodes();
052    }
053
054    /**
055     * Constructor, that creates a new NodeListIterator from the specified
056     * {@code org.w3c.NodeList}
057     *
058     * @param nodeList node list, which is wrapped by this class. Must not be null
059     * @throws NullPointerException if nodeList is null
060     */
061    public NodeListIterator(final NodeList nodeList) {
062        this.nodeList = Objects.requireNonNull(nodeList, "nodeList");
063    }
064
065    @Override
066    public boolean hasNext() {
067        return nodeList != null && index < nodeList.getLength();
068    }
069
070    @Override
071    public Node next() {
072        if (nodeList != null && index < nodeList.getLength()) {
073            return nodeList.item(index++);
074        }
075        throw new NoSuchElementException("underlying nodeList has no more elements");
076    }
077
078    /**
079     * Throws {@link UnsupportedOperationException}.
080     *
081     * @throws UnsupportedOperationException always
082     */
083    @Override
084    public void remove() {
085        throw new UnsupportedOperationException("remove() method not supported for a NodeListIterator.");
086    }
087}