1 /**********************************************
2 * Copyright (C) 2010 Lukas Laag
3 * This file is part of lib-gwt-svg.
4 *
5 * libgwtsvg is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * libgwtsvg is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with libgwtsvg. If not, see http://www.gnu.org/licenses/
17 **********************************************/
18 /*
19 * Copyright (c) 2004 World Wide Web Consortium,
20 *
21 * (Massachusetts Institute of Technology, European Research Consortium for
22 * Informatics and Mathematics, Keio University). All Rights Reserved. This
23 * work is distributed under the W3C(r) Software License [1] in the hope that
24 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26 *
27 * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28 */
29 package org.vectomatic.dom.svg;
30
31 import java.util.Iterator;
32
33 import com.google.gwt.dom.client.Node;
34 import com.google.gwt.dom.client.NodeList;
35
36 /**
37 * Wrapper class for DOM NodeList
38 * @author laaglu
39 */
40 public class OMNodeList<T extends OMNode> implements Iterable<T> {
41 private NodeList<? extends Node> ot;
42 /**
43 * Constructor
44 * @param nodeList The wrapped node list
45 */
46 protected OMNodeList(NodeList<? extends Node> nodeList) {
47 this.ot = nodeList;
48 }
49 /**
50 * Returns the <code>index</code>th item in the collection. If
51 * <code>index</code> is greater than or equal to the number of nodes in
52 * the list, this returns <code>null</code>.
53 * @param index Index into the collection.
54 * @return The node at the <code>index</code>th position in the
55 * <code>NodeList</code>, or <code>null</code> if that is not a valid
56 * index.
57 */
58 public final T getItem(int index) {
59 Node node = ot.getItem(index);
60 return (node != null) ? OMNode.<T>convert(node) : null;
61 }
62 /**
63 * The number of nodes in the list. The range of valid child node indices
64 * is 0 to <code>length-1</code> inclusive.
65 */
66 public final int getLength() {
67 return ot.getLength();
68 }
69 /**
70 * Returns an iterator over the {@link org.vectomatic.dom.svg.OMNode}
71 * elements in this list in proper sequence.
72 *
73 * <p>This implementation returns a straightforward implementation of the
74 * iterator interface, relying on the backing list's {@code getNumberOfItems()},
75 * and {@code getItem(int)} methods.
76 *
77 * <p>Note that the iterator returned by this method will throw an
78 * {@code UnsupportedOperationException} in response to its
79 * {@code remove} method.
80 *
81 * @return an iterator over the {@link org.vectomatic.dom.svg.OMNode}
82 * elements in this list in proper sequence
83 */
84 @Override
85 public Iterator<T> iterator() {
86 return new Iterator<T>() {
87 private int index;
88
89 @Override
90 public boolean hasNext() {
91 return index < getLength();
92 }
93
94 @Override
95 public T next() {
96 return getItem(index++);
97 }
98
99 @Override
100 public void remove() {
101 throw new UnsupportedOperationException();
102 }
103 };
104 }
105 }