001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.pattern;
030:
031: import com.caucho.xpath.ExprEnvironment;
032: import com.caucho.xpath.XPathException;
033:
034: import org.w3c.dom.Node;
035:
036: /**
037: * The unique iterator.
038: */
039: public class UniqueIterator extends NodeIterator {
040: private NodeIterator _baseIterator;
041:
042: private Node _node;
043:
044: private Node[] _oldNodes;
045: private int _top;
046:
047: /**
048: * Zero arg constructor.
049: */
050: public UniqueIterator(ExprEnvironment env) {
051: super (env);
052: }
053:
054: /**
055: * Creates a merge iterator with a given base.
056: */
057: public UniqueIterator(ExprEnvironment env, NodeIterator baseIterator)
058: throws XPathException {
059: super (env);
060:
061: _baseIterator = baseIterator;
062: _node = baseIterator.nextNode();
063: _oldNodes = new Node[32];
064: }
065:
066: /**
067: * True if there are more nodes.
068: */
069: public boolean hasNext() {
070: return _node != null;
071: }
072:
073: /**
074: * Returns the next node.
075: */
076: public Node nextNode() throws XPathException {
077: Node next = _node;
078:
079: if (next == null)
080: return null;
081:
082: if (_top == _oldNodes.length) {
083: Node[] newNodes = new Node[_oldNodes.length * 2];
084: System.arraycopy(_oldNodes, 0, newNodes, 0,
085: _oldNodes.length);
086: _oldNodes = newNodes;
087: }
088:
089: _oldNodes[_top++] = next;
090:
091: while (true) {
092: _node = _baseIterator.nextNode();
093:
094: if (_node == null)
095: return next;
096:
097: int i;
098: for (i = _top - 1; i >= 0; i--)
099: if (_oldNodes[i] == _node)
100: break;
101:
102: if (i < 0)
103: break;
104: }
105:
106: return next;
107: }
108:
109: /**
110: * Returns a clone
111: */
112: public Object clone() {
113: UniqueIterator clone = new UniqueIterator(_env);
114:
115: clone._node = _node;
116: clone._oldNodes = new Node[_oldNodes.length];
117: System.arraycopy(_oldNodes, 0, clone._oldNodes, 0,
118: _oldNodes.length);
119: clone._top = _top;
120:
121: return clone;
122: }
123: }
|