KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > FilteredNodeListImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library 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 GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Oct 9, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import org.lobobrowser.js.*;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import java.util.*;
30
31 class FilteredNodeListImpl extends AbstractScriptableDelegate implements NodeList JavaDoc {
32     private final Collection sourceNodeList;
33     private final NodeFilter filter;
34     private final Object JavaDoc lock;
35     
36     /**
37      * @param filter
38      * @param list
39      */

40     public FilteredNodeListImpl(NodeFilter filter, Collection list, Object JavaDoc lock) {
41         super();
42         this.filter = filter;
43         sourceNodeList = list;
44         this.lock = lock;
45     }
46
47     public Node JavaDoc item(int index) {
48         synchronized(this.lock) {
49             int count = 0;
50             Iterator i = this.sourceNodeList.iterator();
51             while(i.hasNext()) {
52                 Node JavaDoc node = (Node JavaDoc) i.next();
53                 if(this.filter.accept(node)) {
54                     if(count == index) {
55                         return node;
56                     }
57                     count++;
58                 }
59             }
60             return null;
61         }
62     }
63
64     public int getLength() {
65         synchronized(this.lock) {
66             int count = 0;
67             Iterator i = this.sourceNodeList.iterator();
68             while(i.hasNext()) {
69                 Node JavaDoc node = (Node JavaDoc) i.next();
70                 if(this.filter.accept(node)) {
71                     count++;
72                 }
73             }
74             return count;
75         }
76     }
77 }
78
Popular Tags