KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: AdaptorFunctors.java,v 1.2 2006/12/16 16:48:58 davidahall Exp $
3
// Copyright (c) 2006 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 java.util.Iterator JavaDoc;
36 import net.sf.jga.fn.BinaryFunctor;
37 import net.sf.jga.fn.Generator;
38 import net.sf.jga.fn.UnaryFunctor;
39
40 /**
41  * Static factory methods for selected functors in the Adaptor package.
42  * <p>
43  * Copyright &copy; 2006 David A. Hall
44  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
45  */

46
47 public final class AdaptorFunctors {
48     
49     // Stateless functors for which only one instance is really necessary
50
static private Identity _id = new Identity();
51     static private Project1st _proj1 = new Project1st();
52     static private Project2nd _proj2 = new Project2nd();
53
54     /**
55      * Returns a Boolean Generator that produces TRUE when both input generators return TRUE.
56      */

57     static public Generator<Boolean JavaDoc> and(Generator<Boolean JavaDoc> g1, Generator<Boolean JavaDoc> g2) {
58         return new AndGenerator(g1, g2);
59     }
60     
61     /**
62      * Returns a Boolean Functor that produces TRUE when both input functors return TRUE for
63      * same given argument.
64      */

65     static public <T> UnaryFunctor<T,Boolean JavaDoc>
66     and(UnaryFunctor<T,Boolean JavaDoc> uf1, UnaryFunctor<T,Boolean JavaDoc> uf2)
67     {
68         return new AndUnary<T>(uf1, uf2);
69     }
70     
71     /**
72      * Returns a Boolean Functor that produces TRUE when both input functors return TRUE for
73      * same given pair of arguments.
74      */

75     static public <T1,T2> BinaryFunctor<T1,T2,Boolean JavaDoc>
76     and(BinaryFunctor<T1,T2,Boolean JavaDoc> uf1, BinaryFunctor<T1,T2,Boolean JavaDoc> uf2)
77     {
78         return new AndBinary<T1,T2>(uf1, uf2);
79     }
80
81
82     /**
83      * Returns a generator that evaluates the <tt>test</tt> and, when true, evaluates the
84      * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
85      * generator returns <tt>null</tt>.
86      */

87     static public <R> Generator<R> conditional(Generator<Boolean JavaDoc> test, Generator<R> trueFn) {
88         return new ConditionalGenerator<R>(test, trueFn, new Constant<R>(null));
89     }
90
91     /**
92      * Returns a generator that evaluates the <tt>test</tt> and, when true, evaluates the
93      * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
94      * generator evaluates <tt>falseFn</tt> and the returns the results
95      */

96     static public <R> Generator<R>
97     conditional(Generator<Boolean JavaDoc> test, Generator<R> trueFn, Generator<R> falseFn)
98     {
99         return new ConditionalGenerator<R>(test, trueFn, falseFn);
100     }
101
102     /**
103      * Returns a functor that evaluates the <tt>test</tt> and, when true, evaluates the
104      * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
105      * functor returns <tt>null</tt>. Both functors receiv the same argument when evaluated.
106      */

107     static public <T,R> UnaryFunctor<T,R>
108     conditional(UnaryFunctor<T,Boolean JavaDoc> test, UnaryFunctor<T,R> trueFn)
109     {
110         return new ConditionalUnary<T,R>(test, trueFn, new ConstantUnary<T,R>(null));
111     }
112
113     /**
114      * Returns a functor that evaluates the <tt>test</tt> and, when true, evaluates the
115      * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
116      * functor evaluates <tt>falseFn</tt> and the returns the results. Both functors
117      * that are evaluated receive the same pair of arguments.
118      */

119     static public <T,R> UnaryFunctor<T,R>
120     conditional(UnaryFunctor<T,Boolean JavaDoc> test, UnaryFunctor<T,R> trueFn, UnaryFunctor<T,R> falseFn)
121     {
122         return new ConditionalUnary<T,R>(test, trueFn, falseFn);
123     }
124
125
126     /**
127      * Returns a functor that evaluates the <tt>test</tt> and, when true, evaluates the
128      * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
129      * functor returns <tt>null</tt>. Both functors receive the same pair of arguments when
130      * valuated.
131      */

132     static public <T1,T2,R> BinaryFunctor<T1,T2,R>
133     conditional(BinaryFunctor<T1,T2,Boolean JavaDoc> test, BinaryFunctor<T1,T2,R> trueFn)
134     {
135         return new ConditionalBinary<T1,T2,R>(test, trueFn, new ConstantBinary<T1,T2,R>(null));
136     }
137
138     /**
139      * Returns a functor that evaluates the <tt>test</tt> and, when true, evaluates the
140      * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
141      * functor evaluates <tt>falseFn</tt> and the returns the results. Both functors
142      * that are evaluated receive the same pair of arguments.
143      */

144     static public <T1,T2,R> BinaryFunctor<T1,T2,R>
145     conditional(BinaryFunctor<T1,T2,Boolean JavaDoc> test, BinaryFunctor<T1,T2,R> trueFn,
146                 BinaryFunctor<T1,T2,R> falseFn)
147     {
148         return new ConditionalBinary<T1,T2,R>(test, trueFn, falseFn);
149     }
150
151     
152     /**
153      */

154     static public <R> Generator<R> constant(R value) {
155         return new Constant<R>(value);
156     }
157
158     
159     /**
160      */

161     static public <T,R> UnaryFunctor<T,R> constantUnary(R value) {
162         return new ConstantUnary<T,R>(value);
163     }
164
165     
166     /**
167      */

168     static public <T1,T2,R> BinaryFunctor<T1,T2,R> constantBinary(R value) {
169         return new ConstantBinary<T1,T2,R>(value);
170     }
171
172     
173     /**
174      */

175     @SuppressWarnings JavaDoc("unchecked")
176     static public <T> UnaryFunctor<T,T> identity() {
177         return (Identity<T>) _id;
178     }
179
180    
181     /**
182      * Returns a Boolean Generator that produces TRUE when either input generators return TRUE.
183      */

184     static public Generator<Boolean JavaDoc> or(Generator<Boolean JavaDoc> g1, Generator<Boolean JavaDoc> g2) {
185         return new OrGenerator(g1, g2);
186     }
187
188     
189     /**
190      * Returns a Boolean Functor that produces TRUE when either input functors return TRUE for
191      * same given argument.
192      */

193     static public <T> UnaryFunctor<T,Boolean JavaDoc>
194     or(UnaryFunctor<T,Boolean JavaDoc> uf1, UnaryFunctor<T,Boolean JavaDoc> uf2)
195     {
196         return new OrUnary<T>(uf1, uf2);
197     }
198
199     
200     /**
201      * Returns a Boolean Functor that produces TRUE when either input functors return TRUE for
202      * same given pair of arguments.
203      */

204     static public <T1,T2> BinaryFunctor<T1,T2,Boolean JavaDoc>
205     or(BinaryFunctor<T1,T2,Boolean JavaDoc> uf1, BinaryFunctor<T1,T2,Boolean JavaDoc> uf2)
206     {
207         return new OrBinary<T1,T2>(uf1, uf2);
208     }
209
210
211     /**
212      */

213     @SuppressWarnings JavaDoc("unchecked")
214     static public <T1,T2> BinaryFunctor<T1,T2,T1> project1st() {
215         return _proj1;
216     }
217
218     
219     
220     /**
221      */

222     @SuppressWarnings JavaDoc("unchecked")
223     static public <T1,T2> BinaryFunctor<T1,T2,T2> project2nd() {
224         return _proj2;
225     }
226 }
227
228
Popular Tags