1 21 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 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 hasNext; 66 private Object next; 67 68 private void scanNext() { 69 while(sourceIterator.hasNext()) { 70 Object 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 82 public boolean hasNext() { 83 if(this.hasNext == null) { 84 scanNext(); 85 } 86 return this.hasNext.booleanValue(); 87 } 88 89 92 public Object next() { 93 if(this.hasNext == null) { 94 scanNext(); 95 } 96 if(Boolean.FALSE.equals(this.hasNext)) { 97 throw new NoSuchElementException(); 98 } 99 Object next = this.next; 100 this.hasNext = null; 101 return next; 102 } 103 104 107 public void remove() { 108 throw new UnsupportedOperationException (); 109 } 110 }; 111 } 112 113 public Object [] toArray() { 114 return this.toArray(new Object [0]); 115 } 116 117 public Object [] toArray(Object [] a) { 118 Collection bucket = new ArrayList(); 119 Iterator i = this.sourceCollection.iterator(); 120 while(i.hasNext()) { 121 Object 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 o) { 130 return this.sourceCollection.add(this.filter.encode(o)); 131 } 132 133 public boolean remove(Object 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 [] 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 [] 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 |