| 1 33 package net.sf.jga.fn.comparison; 34 35 import java.util.Comparator ; 36 import net.sf.jga.fn.UnaryFunctor; 37 import net.sf.jga.fn.UnaryPredicate; 38 import net.sf.jga.util.ComparableComparator; 39 40 56 57 public class Between<T> extends UnaryPredicate<T> { 58 59 static final long serialVersionUID = 7520704443234013748L; 60 61 private UnaryFunctor<T,Boolean > _ge; 62 private UnaryFunctor<T,Boolean > _le; 63 64 71 public Between(T lo, T hi, Comparator <? super T> comp) { 72 if (lo == null || hi == null) { 73 String msg = "a pair of values is required"; 74 throw new IllegalArgumentException (msg); 75 } 76 77 if (comp.compare(lo,hi) > 0) { 78 String msg = "lo value must be less than hi value"; 79 throw new IllegalArgumentException (msg); 80 } 81 82 _ge = new GreaterEqual<T>(comp).bind2nd(lo); 83 _le = new LessEqual<T>(comp).bind2nd(hi); 84 } 85 86 92 public Between(UnaryFunctor<T,Boolean > lo, UnaryFunctor<T,Boolean > hi) { 93 if (lo == null || hi == null) { 94 String msg = "a pair of predicates is required"; 95 throw new IllegalArgumentException (msg); 96 } 97 98 _ge = lo; 99 _le = hi; 100 } 101 102 104 109 public Boolean fn(T x) { 110 return Boolean.valueOf(_ge.fn(x).booleanValue() && _le.fn(x).booleanValue()); 111 } 112 113 117 public void accept(net.sf.jga.fn.Visitor v) { 118 if (v instanceof Between.Visitor) 119 ((Between.Visitor)v).visit(this); 120 else 121 v.visit(this); 122 } 123 124 126 public String toString() { 127 return "Between"; 128 } 129 130 132 135 public interface Visitor extends net.sf.jga.fn.Visitor { 136 public void visit(Between host); 137 } 138 139 144 145 static public class Comparable<T extends java.lang.Comparable <? super T>> 146 extends Between<T> 147 { 148 static final long serialVersionUID = 4385771596777515479L; 149 public Comparable(T lo, T hi) { super(lo, hi, new ComparableComparator<T>()); } 150 } 151 } 152 | Popular Tags |