1 33 package net.sf.jga.fn.logical; 34 35 import java.util.Collection ; 36 import java.util.Iterator ; 37 import java.util.ArrayList ; 38 import net.sf.jga.fn.UnaryFunctor; 39 import net.sf.jga.fn.UnaryPredicate; 40 41 56 57 public class All<T> extends UnaryPredicate<T> { 58 59 static final long serialVersionUID = 545530729045853706L; 60 61 private Collection <UnaryFunctor<T,Boolean >> _branches; 62 63 68 public All() { 69 _branches = new ArrayList <UnaryFunctor<T,Boolean >>(); 70 } 71 72 78 public All(Collection <UnaryFunctor<T,Boolean >> branches) { 79 _branches = (branches != null) ? branches : new ArrayList <UnaryFunctor<T,Boolean >>(); 80 } 81 82 86 public Iterator <UnaryFunctor<T,Boolean >> branches() { 87 return _branches.iterator(); 88 } 89 90 92 99 public Boolean fn(T x) { 100 for (UnaryFunctor<T,Boolean > pred : _branches) { 101 if (! pred.fn(x)) 102 return false; 103 } 104 105 return true; 106 } 107 108 112 public void accept(net.sf.jga.fn.Visitor v) { 113 if (v instanceof All.Visitor) 114 ((All.Visitor)v).visit(this); 115 else 116 v.visit(this); 117 } 118 119 121 public String toString() { 122 StringBuffer buf = new StringBuffer ("All("); 123 String sep = ""; 124 for (UnaryFunctor<T,Boolean > pred : _branches) { 125 buf.append(sep).append(pred); 126 sep=","; 127 } 128 buf.append(")"); 129 return buf.toString(); 130 } 131 132 134 137 public interface Visitor extends net.sf.jga.fn.Visitor { 138 public void visit(All host); 139 } 140 } 141 | Popular Tags |