1 33 package net.sf.jga.fn.algorithm; 34 35 import java.util.Iterator ; 36 import net.sf.jga.fn.UnaryFunctor; 37 import net.sf.jga.fn.comparison.Equality; 38 import net.sf.jga.fn.comparison.EqualTo; 39 import net.sf.jga.util.FindIterator; 40 41 48 49 public class Count<T> extends UnaryFunctor<Iterator <? extends T>,Long > { 50 51 static final long serialVersionUID = 5477750093695096889L; 52 53 private UnaryFunctor<T,Boolean > _eq; 55 56 60 public Count (T value) { 61 this(new EqualTo<T>().bind2nd(value)); 62 } 63 64 68 public Count (Equality<T> eq, T value) { 69 this(eq.bind2nd(value)); 70 } 71 72 76 public Count (UnaryFunctor<T,Boolean > eq) { 77 _eq = eq; 78 } 79 80 83 public UnaryFunctor<T,Boolean > getComparisonFn() { 84 return _eq; 85 } 86 87 91 public Long fn (Iterator <? extends T> iterator) { 92 long count = 0; 93 FindIterator<T> finder = new FindIterator<T>(iterator); 94 95 while (finder.findNext(_eq)) { 96 ++count; 97 finder.next(); 98 } 99 100 return new Long (count); 101 } 102 103 107 public void accept(net.sf.jga.fn.Visitor v) { 108 if (v instanceof Count.Visitor) 109 ((Count.Visitor)v).visit(this); 110 else 111 v.visit(this); 112 } 113 114 116 public String toString() { 117 return "Count["+_eq+"]"; 118 } 119 120 122 125 public interface Visitor extends net.sf.jga.fn.Visitor { 126 public void visit(Count host); 127 } 128 } 129 | Popular Tags |