| 1 package net.sf.jga.fn.algorithm; 33 34 import java.util.Collection ; 35 import net.sf.jga.fn.BinaryFunctor; 36 import net.sf.jga.fn.UnaryFunctor; 37 import net.sf.jga.fn.BinaryPredicate; 38 import net.sf.jga.util.FindIterator; 39 40 48 49 public class ElementOf<T> extends BinaryPredicate<T, Collection <? extends T>> { 50 51 static final long serialVersionUID = 4639100512962835854L; 52 53 private BinaryFunctor<T,T,Boolean > _eq; 55 56 63 public ElementOf() {} 64 65 70 public ElementOf(BinaryFunctor<T,T,Boolean > eq) { 71 _eq = eq; 72 } 73 74 78 public BinaryFunctor<T,T,Boolean > getComparisonFn() { 79 return _eq; 80 } 81 82 85 public Boolean fn(T value, Collection <? extends T> collection) { 86 if (_eq == null) 87 return collection.contains(value); 88 else { 89 FindIterator<T> finder = 90 new FindIterator<T>(collection.iterator()); 91 UnaryFunctor<T,Boolean > uf = _eq.bind2nd(value); 92 return finder.findNext(uf); 93 } 94 } 95 96 100 public void accept(net.sf.jga.fn.Visitor v) { 101 if (v instanceof ElementOf.Visitor) 102 ((ElementOf.Visitor)v).visit(this); 103 else 104 v.visit(this); 105 } 106 107 109 public String toString() { 110 return "ElementOf"; 111 } 112 113 115 118 public interface Visitor extends net.sf.jga.fn.Visitor { 119 public void visit(ElementOf host); 120 } 121 } 122 | Popular Tags |