1 33 package net.sf.jga.algorithms; 34 35 import java.util.Collection ; 36 import java.util.Iterator ; 37 import net.sf.jga.fn.UnaryFunctor; 38 import net.sf.jga.fn.comparison.Equality; 39 import net.sf.jga.fn.comparison.NotEqualTo; 40 import net.sf.jga.fn.logical.UnaryNegate; 41 import net.sf.jga.util.FindIterator; 42 43 import static net.sf.jga.util.ArrayUtils.*; 44 import static net.sf.jga.util.CollectionUtils.*; 45 46 55 56 public class Filter { 57 58 62 65 static public <T> Iterable <T> filter(T[] ts, UnaryFunctor<T,Boolean > pred) { 66 return new FilterIterable<T>(iterable(ts), pred); 67 } 68 69 73 static public <T> Iterable <T> remove(T[] ts, T value) { 74 return filter(ts, new NotEqualTo<T>().bind2nd(value)); 75 } 76 77 81 static public <T> Iterable <T> remove(T[] ts, T value, Equality<T> eq) { 82 return filter(ts, new UnaryNegate<T>(eq.bind2nd(value))); 83 } 84 85 88 static public <T> Iterable <T> remove(T[] ts, UnaryFunctor<T,Boolean > pred) { 89 return filter(ts, new UnaryNegate<T>(pred)); 90 } 91 92 96 99 static public <T> Iterable <T> filter(Iterable <? extends T> i, UnaryFunctor<T,Boolean > pred) { 100 return new FilterIterable<T>(i, pred); 101 } 102 103 107 static public <T> Iterable <T> remove(Iterable <? extends T> i, T value) { 108 return filter(i, new NotEqualTo<T>().bind2nd(value)); 109 } 110 111 115 static public <T> Iterable <T> remove(Iterable <? extends T> i, T value, Equality<T> eq) { 116 return filter(i, new UnaryNegate<T>(eq.bind2nd(value))); 117 } 118 119 122 static public <T> Iterable <T> remove(Iterable <? extends T> i, UnaryFunctor<T,Boolean > pred) { 123 return filter(i, new UnaryNegate<T>(pred)); 124 } 125 126 130 133 static public <T> Iterator <T> filter(Iterator <? extends T> iter, UnaryFunctor<T,Boolean > pred) { 134 return new net.sf.jga.util.FilterIterator<T>(iter, pred); 135 } 136 137 141 static public <T> Iterator <T> remove(Iterator <? extends T> iter, T value) { 142 return filter(iter, new NotEqualTo<T>().bind2nd(value)); 143 } 144 145 149 static public <T> Iterator <T> remove(Iterator <? extends T> iter, T value, Equality<T> eq) { 150 return filter(iter, new UnaryNegate<T>(eq.bind2nd(value))); 151 } 152 153 156 static public <T> Iterator <T> remove(Iterator <? extends T> iter,UnaryFunctor<T,Boolean > pred){ 157 return filter(iter, new UnaryNegate<T>(pred)); 158 } 159 160 164 167 static public <T, TCollection extends Collection <? super T>> TCollection 168 filter(Iterable <? extends T> cin, UnaryFunctor<T,Boolean > pred, TCollection cout) { 169 return append(cout, filter(cin.iterator(), pred)); 170 } 171 172 173 177 static public <T, TCollection extends Collection <? super T>> TCollection 178 remove(Iterable <? extends T> cin, T value, TCollection cout) { 179 return append(cout, remove(cin.iterator(), value)); 180 } 181 182 183 187 static public <T, TCollection extends Collection <? super T>> TCollection 188 remove(Iterable <? extends T> cin, T value, Equality<T> eq, TCollection cout) { 189 return append(cout, remove(cin.iterator(), value, eq)); 190 } 191 192 193 196 static public <T, TCollection extends Collection <? super T>> TCollection 197 remove(Iterable <? extends T> cin, UnaryFunctor<T,Boolean > pred, TCollection cout) { 198 return append(cout, remove(cin.iterator(), pred)); 199 } 200 201 205 208 209 212 static public class FilterIterable<T> implements Iterable <T> { 213 214 private Iterable <? extends T> _delegate; 215 216 private UnaryFunctor<T,Boolean > _filter; 218 219 223 public FilterIterable(Iterable <? extends T> iter, 224 UnaryFunctor<T,Boolean > pred) 225 { 226 _delegate = iter; 227 _filter = pred; 228 } 229 230 234 public Iterator <T> iterator() { 235 return new net.sf.jga.util.FilterIterator<T>(_delegate.iterator(), _filter); 236 } 237 } 238 239 240 243 244 static public class FilterIterator<T> implements Iterator <T> { 245 246 private FindIterator<T> _base; 248 249 private UnaryFunctor<T,Boolean > _filter; 251 252 private boolean _testedNext; 255 256 260 public FilterIterator(Iterator <? extends T> iter, UnaryFunctor<T,Boolean > pred) { 261 _base = new FindIterator<T>(iter); 262 _filter = pred; 263 } 264 265 269 public boolean hasNext(){ 270 _testedNext = true; 271 return _base.findNext(_filter); 272 } 273 274 public T next() { 275 if (!_testedNext) { 276 hasNext(); 277 } 278 279 _testedNext = false; 280 return _base.next(); 281 } 282 283 public void remove() { throw new UnsupportedOperationException (); } 284 } 285 } 286 | Popular Tags |