KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > pattern > UniqueIterator


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.pattern;
30
31 import com.caucho.xpath.ExprEnvironment;
32 import com.caucho.xpath.XPathException;
33
34 import org.w3c.dom.Node JavaDoc;
35
36 /**
37  * The unique iterator.
38  */

39 public class UniqueIterator extends NodeIterator {
40   private NodeIterator _baseIterator;
41
42   private Node JavaDoc _node;
43
44   private Node JavaDoc []_oldNodes;
45   private int _top;
46
47   /**
48    * Zero arg constructor.
49    */

50   public UniqueIterator(ExprEnvironment env)
51   {
52     super(env);
53   }
54
55   /**
56    * Creates a merge iterator with a given base.
57    */

58   public UniqueIterator(ExprEnvironment env, NodeIterator baseIterator)
59     throws XPathException
60   {
61     super(env);
62     
63     _baseIterator = baseIterator;
64     _node = baseIterator.nextNode();
65     _oldNodes = new Node JavaDoc[32];
66   }
67
68   /**
69    * True if there are more nodes.
70    */

71   public boolean hasNext()
72   {
73     return _node != null;
74   }
75
76   /**
77    * Returns the next node.
78    */

79   public Node JavaDoc nextNode()
80     throws XPathException
81   {
82     Node JavaDoc next = _node;
83
84     if (next == null)
85       return null;
86
87     if (_top == _oldNodes.length) {
88       Node JavaDoc []newNodes = new Node JavaDoc[_oldNodes.length * 2];
89       System.arraycopy(_oldNodes, 0, newNodes, 0, _oldNodes.length);
90       _oldNodes = newNodes;
91     }
92     
93     _oldNodes[_top++] = next;
94
95     while (true) {
96       _node = _baseIterator.nextNode();
97
98       if (_node == null)
99         return next;
100
101       int i;
102       for (i = _top - 1; i >= 0; i--)
103         if (_oldNodes[i] == _node)
104           break;
105
106       if (i < 0)
107         break;
108     }
109
110     return next;
111   }
112
113   /**
114    * Returns a clone
115    */

116   public Object JavaDoc clone()
117   {
118     UniqueIterator clone = new UniqueIterator(_env);
119
120     clone._node = _node;
121     clone._oldNodes = new Node JavaDoc[_oldNodes.length];
122     System.arraycopy(_oldNodes, 0, clone._oldNodes, 0, _oldNodes.length);
123     clone._top = _top;
124
125     return clone;
126   }
127 }
128
129
Popular Tags