KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > FilteredCollection


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 8, 2005
23  */

24 package org.lobobrowser.util;
25
26 import java.util.*;
27
28 public class FilteredCollection implements Collection {
29     private final ObjectFilter filter;
30     private final Collection sourceCollection;
31     
32     public FilteredCollection(Collection sourceCollection, ObjectFilter filter) {
33         this.filter = filter;
34         this.sourceCollection = sourceCollection;
35     }
36
37     public int size() {
38         int count = 0;
39         Iterator i = this.sourceCollection.iterator();
40         while(i.hasNext()) {
41             if(this.filter.decode(i.next()) != null) {
42                 count++;
43             }
44         }
45         return count;
46     }
47
48     public boolean isEmpty() {
49         Iterator i = this.sourceCollection.iterator();
50         while(i.hasNext()) {
51             if(this.filter.decode(i.next()) != null) {
52                 return false;
53             }
54         }
55         return true;
56     }
57
58     public boolean contains(Object JavaDoc o) {
59         return this.sourceCollection.contains(this.filter.encode(o));
60     }
61
62     public Iterator iterator() {
63         final Iterator sourceIterator = this.sourceCollection.iterator();
64         return new Iterator() {
65             private Boolean JavaDoc hasNext;
66             private Object JavaDoc next;
67             
68             private void scanNext() {
69                 while(sourceIterator.hasNext()) {
70                     Object JavaDoc item = filter.decode(sourceIterator.next());
71                     if(item != null) {
72                         hasNext = Boolean.TRUE;
73                         this.next = item;
74                     }
75                 }
76                 hasNext = Boolean.FALSE;
77             }
78             
79             /* (non-Javadoc)
80              * @see java.util.Iterator#hasNext()
81              */

82             public boolean hasNext() {
83                 if(this.hasNext == null) {
84                     scanNext();
85                 }
86                 return this.hasNext.booleanValue();
87             }
88
89             /* (non-Javadoc)
90              * @see java.util.Iterator#next()
91              */

92             public Object JavaDoc next() {
93                 if(this.hasNext == null) {
94                     scanNext();
95                 }
96                 if(Boolean.FALSE.equals(this.hasNext)) {
97                     throw new NoSuchElementException();
98                 }
99                 Object JavaDoc next = this.next;
100                 this.hasNext = null;
101                 return next;
102             }
103
104             /* (non-Javadoc)
105              * @see java.util.Iterator#remove()
106              */

107             public void remove() {
108                 throw new UnsupportedOperationException JavaDoc();
109             }
110         };
111     }
112
113     public Object JavaDoc[] toArray() {
114         return this.toArray(new Object JavaDoc[0]);
115     }
116
117     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
118         Collection bucket = new ArrayList();
119         Iterator i = this.sourceCollection.iterator();
120         while(i.hasNext()) {
121             Object JavaDoc item = this.filter.decode(i.next());
122             if(item != null) {
123                 bucket.add(item);
124             }
125         }
126         return bucket.toArray(a);
127     }
128
129     public boolean add(Object JavaDoc o) {
130         return this.sourceCollection.add(this.filter.encode(o));
131     }
132
133     public boolean remove(Object JavaDoc o) {
134         return this.sourceCollection.remove(this.filter.encode(o));
135     }
136
137     public boolean containsAll(Collection c) {
138         Iterator i = c.iterator();
139         while(i.hasNext()) {
140             if(!this.contains(i.next())) {
141                 return false;
142             }
143         }
144         return true;
145     }
146
147     public boolean addAll(Collection c) {
148         boolean result = false;
149         Iterator i = c.iterator();
150         while(i.hasNext()) {
151             if(this.add(i.next())) {
152                 result = true;
153             }
154         }
155         return result;
156     }
157
158     public boolean removeAll(Collection c) {
159         boolean result = false;
160         Iterator i = c.iterator();
161         while(i.hasNext()) {
162             if(this.remove(i.next())) {
163                 result = true;
164             }
165         }
166         return result;
167     }
168
169     public boolean retainAll(Collection c) {
170         boolean result = false;
171         Object JavaDoc[] values = this.toArray();
172         for(int i = 0; i < values.length; i++) {
173             if(!c.contains(values[i])) {
174                 if(this.remove(values[i])) {
175                     result = true;
176                 }
177             }
178         }
179         return result;
180     }
181
182     public void clear() {
183         Object JavaDoc[] values = this.toArray();
184         for(int i = 0; i < values.length; i++) {
185             this.sourceCollection.remove(this.filter.encode(values[i]));
186         }
187     }
188 }
189
Popular Tags