001: // ============================================================================
002: // $Id: UnaryFunctor.java,v 1.16 2006/11/30 05:03:42 davidahall Exp $
003: // Copyright (c) 2002-2005 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;
034:
035: import java.io.Serializable;
036: import net.sf.jga.fn.adaptor.Bind;
037: import net.sf.jga.fn.adaptor.ChainBinary;
038: import net.sf.jga.fn.adaptor.ChainUnary;
039: import net.sf.jga.fn.adaptor.Generate;
040: import net.sf.jga.fn.adaptor.Identity;
041:
042: /**
043: * A Function Object that takes one argument and returns a result. The
044: * argument is of type <code>T</code>, and the result is of type <code>R</code>.
045: * <p>
046: * Copyright © 2002-2005 David A. Hall
047: *
048: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
049: **/
050:
051: abstract public class UnaryFunctor<T, R> extends Functor<R> implements
052: Serializable, Visitable {
053: /**
054: * Executes the function and returns the result.
055: */
056: abstract public R fn(T arg);
057:
058: /**
059: * FactoryMethod that creates a UnaryFunctor that passes its argument to
060: * the given functor, and uses the result as the argument to this
061: * function. Given argument <b>x</b>, the new functor will return
062: * <code>fn<sub>this</sub>(f(x)))</code>
063: */
064: public <F> UnaryFunctor<F, R> compose(UnaryFunctor<F, T> f) {
065: return new ChainUnary<F, T, R>(this , f);
066: }
067:
068: /**
069: * FactoryMethod that creates a BinaryFunctor that passes its arguments to
070: * the given functor, and uses the result as the argument to this
071: * function. Given arguments <b>x</b> and <b>y</b>, the new functor will
072: * return <code>fn<sub>this</sub>(f(x,y)))</code>
073: */
074: public <F1, F2> BinaryFunctor<F1, F2, R> compose(
075: BinaryFunctor<F1, F2, T> f) {
076: return new ChainBinary<F1, F2, T, R>(this , f);
077: }
078:
079: /**
080: * FactoryMethod that creates a Generator to create the argument to this
081: * function. The new functor will return
082: * <code>fn<sub>this</sub>(gen())</code>.
083: */
084:
085: public Generator<R> generate(Generator<T> gen) {
086: return new Generate<T, R>(this , gen);
087: }
088:
089: /**
090: * FactoryMethod that binds the argument arguments to this function to a specific
091: * value. The new functor will return
092: * <code>fn<sub>this</sub>(val)</code>.
093: */
094:
095: public Generator<R> bind(T val) {
096: return new Bind<T, R>(val, this );
097: }
098:
099: // -----------------
100: // Functor interface
101: // -----------------
102:
103: /**
104: * Executes the function and returns the result.
105: */
106: public R eval(Object... args) {
107: // @SuppressWarnings
108: // This generates an unchecked cast warning: we're crossing the line from
109: // unsafe rawform code to typesafe generic code. This interface is known
110: // to be unsafe, and documented to the user as such.
111: return fn((T) args[0]);
112: }
113:
114: // -------------------
115: // Visitable interface
116: // -------------------
117:
118: /**
119: * No-op implementation of Visitable interface.
120: */
121: public void accept(Visitor v) {
122: }
123: }
|