KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > adaptor > ComposeUnary


1 // ============================================================================
2
// $Id: ComposeUnary.java,v 1.9 2006/01/08 00:52:25 davidahall Exp $
3
// Copyright (c) 2002-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.adaptor;
34
35 import net.sf.jga.fn.BinaryFunctor;
36 import net.sf.jga.fn.UnaryFunctor;
37
38 /**
39  * Unary Functor that passes the results of two Unary Functors as the arguments
40  * to a Binary Functor. This allows for the construction of compound functors
41  * from the primitives found in the arithmetic, logical, property, and
42  * comparison packages.
43  * <p>
44  * For example: LogicalAnd is of limited utility since it takes only Boolean
45  * arguments. To use LogicalAnd for something a little more interesting
46  * (eg, is the given integer between 1 and 10), combine LogicalAnd with
47  * GreaterEqual and LessEqual using Binary&nbsp;Compose and Bind1st as follows:
48  * <pre>
49  * new ComposeUnary&lt;Integer,Boolean,Boolean,Boolean&gt; (
50  * new Bind1st&lt;Integer,Integer,Boolean&gt; (1, new LessEqual&lt;Integer&gt;()),
51  * new Bind1st&lt;Integer,Integer,Boolean&gt; (10, new GreaterEqual&lt;Integer&gt;()),
52  * new LogicalAnd());
53  * </pre>
54  * While it may not be the most readable construction in the world, it does
55  * become easier over time.
56  * <p>
57  * Copyright &copy; 2002-2005 David A. Hall
58  *
59  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
60  **/

61
62 public class ComposeUnary<T,F1,F2,R> extends UnaryFunctor<T,R> {
63
64     static final long serialVersionUID = -836030733262754108L;
65     
66     // the first of two inner functors
67
private UnaryFunctor<T,F1> _f;
68
69     // the second of two inner functors
70
private UnaryFunctor<T,F2> _g;
71
72     // the outer functor
73
private BinaryFunctor<F1,F2,R> _h;
74
75     /**
76      * Builds a ComposeUnary functor, given two inner functors <b>f</b> and
77      * <b>g</b>, and outer functor <b>h</b>.
78      * @throws IllegalArgumentException if any of the functors is missing
79      */

80     public ComposeUnary(UnaryFunctor<T,F1> f, UnaryFunctor<T,F2> g,
81                         BinaryFunctor<F1,F2, R> h)
82     {
83         if (f == null || g == null || h == null) {
84             throw new IllegalArgumentException JavaDoc("Three functors are required");
85         }
86         
87         _f = f; _g =g; _h = h;
88     }
89
90     /**
91      * Returns the first of two inner functors
92      * @return the first of two inner functors
93      */

94     public UnaryFunctor<T,F1> getFirstInnerFunctor() { return _f; }
95
96     /**
97      * Returns the second of two inner functors
98      * @return the second of two inner functors
99      */

100     public UnaryFunctor<T,F2> getSecondInnerFunctor() { return _g; }
101
102     /**
103      * Returns the outer functor
104      * @return the outer functor
105      */

106     public BinaryFunctor<F1,F2,R> getOuterFunctor() { return _h; }
107
108     // UnaryFunctor interface
109

110     /**
111      * Given argument <b>x</b>, passes x to both inner functors, and passes the
112      * results of those functors to the outer functor.
113      *
114      * @return h(f(x), g(x))
115      */

116     public R fn(T x) {
117         return _h.fn(_f.fn(x), _g.fn(x));
118     }
119     
120     /**
121      * Calls the Visitor's <code>visit(ComposeUnary)</code> method, if it
122      * implements the nested Visitor interface.
123      */

124     public void accept(net.sf.jga.fn.Visitor v) {
125         if (v instanceof ComposeUnary.Visitor)
126             ((ComposeUnary.Visitor)v).visit(this);
127         else
128             v.visit(this);
129     }
130
131     // Object overrides
132

133     public String JavaDoc toString() {
134         return _h+".compose("+_f+","+_g+")";
135     }
136     
137     // Acyclic Visitor
138

139     /**
140      * Interface for classes that may interpret a <b>ComposeUnary</b> functor.
141      */

142     public interface Visitor extends net.sf.jga.fn.Visitor {
143         public void visit(ComposeUnary host);
144     }
145 }
146
Popular Tags