KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > uk > co > jezuk > mango > Bind


1 package uk.co.jezuk.mango;
2
3 /**
4  * Function and Predicate binding adaptors.
5  * @author Jez Higgins, jez@jezuk.co.uk
6  * @version $Id: Bind.java 56 2002-06-12 11:29:55Z jez $
7  */

8 public class Bind
9 {
10   /**
11    * Adapts a <code>BinaryFunction</code> into a <code>UnaryFunction</code>.
12    * If <code>f</code> is an object implementing <code>BinaryFunction</code>, then
13    * <code>Bind.First(f, C).fn(arg)</code> returns <code>f(C, arg)</code>.<p>
14    * Intuitively, you can think of this as "binding" the first argument of a
15    * <code>BinaryFunction</code> to a constant, thus giving a <code>UnaryFunction</code>.
16    */

17   static public UnaryFunction First(final BinaryFunction f, final Object JavaDoc c)
18   {
19     return new UnaryFunction() {
20     private BinaryFunction fn_;
21     private Object JavaDoc c_;
22     { fn_ = f; c_ = c; }
23     public Object JavaDoc fn(Object JavaDoc arg) { return fn_.fn(c_, arg); }
24       };
25   } // First
26

27   /**
28    * Special case which adapts a <code>BinaryPredicate</code> to a <code>Predicate</code>.
29    * If <code>p</code> is a <code>BinaryPredicate</code>, then <code>Bind.First(p, C).test(arg)</code>
30    * returns <code>p.test(C, arg)</code>.
31    */

32   static public Predicate First(final BinaryPredicate p, final Object JavaDoc c)
33   {
34     return new Predicate() {
35     private BinaryPredicate test_;
36     private Object JavaDoc c_;
37     { test_ = p; c_ = c; }
38     public boolean test(Object JavaDoc arg) { return test_.test(c_, arg); }
39       };
40   } // First
41

42   /**
43    * Adapts a <code>BinaryFunction</code> into a <code>UnaryFunction</code>.
44    * If <code>f</code> is an object implementing <code>BinaryFunction</code>, then
45    * <code>Bind.Second(f, C).fn(arg)</code> returns <code>f(arg, C)</code>.<p>
46    * Intuitively, you can think of this as "binding" the second argument of a
47    * <code>BinaryFunction</code> to a constant, thus giving a <code>UnaryFunction</code>.
48    */

49   static public UnaryFunction Second(final BinaryFunction f, final Object JavaDoc c)
50   {
51     return new UnaryFunction() {
52     private BinaryFunction fn_;
53     private Object JavaDoc c_;
54     { fn_ = f; c_ = c; }
55     public Object JavaDoc fn(Object JavaDoc arg) { return fn_.fn(arg, c_); }
56       };
57   } // Second
58

59   /**
60    * Special case which adapts a <code>BinaryPredicate</code> to a <code>Predicate</code>.
61    * If <code>p</code> is a <code>BinaryPredicate</code>, then <code>Bind.Second(p, C).test(arg)</code>
62    * returns <code>p.test(arg, C)</code>.
63    */

64   static public Predicate Second(final BinaryPredicate p, final Object JavaDoc c)
65   {
66     return new Predicate() {
67     private BinaryPredicate test_;
68     private Object JavaDoc c_;
69     { test_ = p; c_ = c; }
70     public boolean test(Object JavaDoc arg) { return test_.test(arg, c_); }
71       };
72   } // Second
73

74   //////////////////////////////////////////
75
private Bind() { }
76 } // Bind
77

78
Popular Tags