1 package net.sf.jga.fn.algorithm; 33 34 import java.util.Iterator ; 35 import net.sf.jga.fn.UnaryFunctor; 36 37 47 48 public class ForEach<T,R> extends UnaryFunctor<Iterator <? extends T>, R> { 49 static final long serialVersionUID = -2342252375909337974L; 50 51 private UnaryFunctor<T,R> _fn; 53 54 59 public ForEach(UnaryFunctor<T,R> fn) { 60 if (fn == null) 61 throw new IllegalArgumentException (); 62 63 _fn = fn; 64 } 65 66 69 public UnaryFunctor<T,R> getFunction() { 70 return _fn; 71 } 72 73 79 public R fn(Iterator <? extends T> iterator) { 80 R value = null; 81 while (iterator.hasNext()) { 82 value = _fn.fn(iterator.next()); 83 } 84 85 return value; 86 } 87 88 92 public void accept(net.sf.jga.fn.Visitor v) { 93 if (v instanceof ForEach.Visitor) 94 ((ForEach.Visitor)v).visit(this); 95 else 96 v.visit(this); 97 } 98 99 101 public String toString() { 102 return "ForEach"; 103 } 104 105 107 110 public interface Visitor extends net.sf.jga.fn.Visitor { 111 public void visit(ForEach host); 112 } 113 } 114 | Popular Tags |