001: // ============================================================================
002: // $Id: AdaptorFunctors.java,v 1.2 2006/12/16 16:48:58 davidahall Exp $
003: // Copyright (c) 2006 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.fn.adaptor;
034:
035: import java.util.Iterator;
036: import net.sf.jga.fn.BinaryFunctor;
037: import net.sf.jga.fn.Generator;
038: import net.sf.jga.fn.UnaryFunctor;
039:
040: /**
041: * Static factory methods for selected functors in the Adaptor package.
042: * <p>
043: * Copyright © 2006 David A. Hall
044: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
045: */
046:
047: public final class AdaptorFunctors {
048:
049: // Stateless functors for which only one instance is really necessary
050: static private Identity _id = new Identity();
051: static private Project1st _proj1 = new Project1st();
052: static private Project2nd _proj2 = new Project2nd();
053:
054: /**
055: * Returns a Boolean Generator that produces TRUE when both input generators return TRUE.
056: */
057: static public Generator<Boolean> and(Generator<Boolean> g1,
058: Generator<Boolean> g2) {
059: return new AndGenerator(g1, g2);
060: }
061:
062: /**
063: * Returns a Boolean Functor that produces TRUE when both input functors return TRUE for
064: * same given argument.
065: */
066: static public <T> UnaryFunctor<T, Boolean> and(
067: UnaryFunctor<T, Boolean> uf1, UnaryFunctor<T, Boolean> uf2) {
068: return new AndUnary<T>(uf1, uf2);
069: }
070:
071: /**
072: * Returns a Boolean Functor that produces TRUE when both input functors return TRUE for
073: * same given pair of arguments.
074: */
075: static public <T1, T2> BinaryFunctor<T1, T2, Boolean> and(
076: BinaryFunctor<T1, T2, Boolean> uf1,
077: BinaryFunctor<T1, T2, Boolean> uf2) {
078: return new AndBinary<T1, T2>(uf1, uf2);
079: }
080:
081: /**
082: * Returns a generator that evaluates the <tt>test</tt> and, when true, evaluates the
083: * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
084: * generator returns <tt>null</tt>.
085: */
086: static public <R> Generator<R> conditional(Generator<Boolean> test,
087: Generator<R> trueFn) {
088: return new ConditionalGenerator<R>(test, trueFn,
089: new Constant<R>(null));
090: }
091:
092: /**
093: * Returns a generator that evaluates the <tt>test</tt> and, when true, evaluates the
094: * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
095: * generator evaluates <tt>falseFn</tt> and the returns the results
096: */
097: static public <R> Generator<R> conditional(Generator<Boolean> test,
098: Generator<R> trueFn, Generator<R> falseFn) {
099: 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> conditional(
108: UnaryFunctor<T, Boolean> test, UnaryFunctor<T, R> trueFn) {
109: return new ConditionalUnary<T, R>(test, trueFn,
110: 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> conditional(
120: UnaryFunctor<T, Boolean> test, UnaryFunctor<T, R> trueFn,
121: UnaryFunctor<T, R> falseFn) {
122: return new ConditionalUnary<T, R>(test, trueFn, falseFn);
123: }
124:
125: /**
126: * Returns a functor that evaluates the <tt>test</tt> and, when true, evaluates the
127: * <tt>trueFn</tt> and returns the results. When the <tt>test</tt> is false, the
128: * functor returns <tt>null</tt>. Both functors receive the same pair of arguments when
129: * valuated.
130: */
131: static public <T1, T2, R> BinaryFunctor<T1, T2, R> conditional(
132: BinaryFunctor<T1, T2, Boolean> test,
133: BinaryFunctor<T1, T2, R> trueFn) {
134: return new ConditionalBinary<T1, T2, R>(test, trueFn,
135: 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> conditional(
145: BinaryFunctor<T1, T2, Boolean> test,
146: BinaryFunctor<T1, T2, R> trueFn,
147: BinaryFunctor<T1, T2, R> falseFn) {
148: return new ConditionalBinary<T1, T2, R>(test, trueFn, falseFn);
149: }
150:
151: /**
152: */
153: static public <R> Generator<R> constant(R value) {
154: return new Constant<R>(value);
155: }
156:
157: /**
158: */
159: static public <T, R> UnaryFunctor<T, R> constantUnary(R value) {
160: return new ConstantUnary<T, R>(value);
161: }
162:
163: /**
164: */
165: static public <T1, T2, R> BinaryFunctor<T1, T2, R> constantBinary(
166: R value) {
167: return new ConstantBinary<T1, T2, R>(value);
168: }
169:
170: /**
171: */
172: @SuppressWarnings("unchecked")
173: static public <T> UnaryFunctor<T, T> identity() {
174: return (Identity<T>) _id;
175: }
176:
177: /**
178: * Returns a Boolean Generator that produces TRUE when either input generators return TRUE.
179: */
180: static public Generator<Boolean> or(Generator<Boolean> g1,
181: Generator<Boolean> g2) {
182: return new OrGenerator(g1, g2);
183: }
184:
185: /**
186: * Returns a Boolean Functor that produces TRUE when either input functors return TRUE for
187: * same given argument.
188: */
189: static public <T> UnaryFunctor<T, Boolean> or(
190: UnaryFunctor<T, Boolean> uf1, UnaryFunctor<T, Boolean> uf2) {
191: return new OrUnary<T>(uf1, uf2);
192: }
193:
194: /**
195: * Returns a Boolean Functor that produces TRUE when either input functors return TRUE for
196: * same given pair of arguments.
197: */
198: static public <T1, T2> BinaryFunctor<T1, T2, Boolean> or(
199: BinaryFunctor<T1, T2, Boolean> uf1,
200: BinaryFunctor<T1, T2, Boolean> uf2) {
201: return new OrBinary<T1, T2>(uf1, uf2);
202: }
203:
204: /**
205: */
206: @SuppressWarnings("unchecked")
207: static public <T1, T2> BinaryFunctor<T1, T2, T1> project1st() {
208: return _proj1;
209: }
210:
211: /**
212: */
213: @SuppressWarnings("unchecked")
214: static public <T1, T2> BinaryFunctor<T1, T2, T2> project2nd() {
215: return _proj2;
216: }
217: }
|