// ============================================================================
// $Id: UnaryFunctor.java,v 1.16 2006/11/30 05:03:42 davidahall Exp $
// Copyright (c) 2002-2005 David A. Hall
// ============================================================================
// The contents of this file are subject to the Common Development and
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
// file except in compliance with the License. You should have received a copy
// of the the License along with this file: if not, a copy of the License is
// available from Sun Microsystems, Inc.
//
// http://www.sun.com/cddl/cddl.html
//
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
// publish revised and/or new versions of the License. You may not use,
// distribute, or otherwise make this file available under subsequent versions
// of the License.
//
// Alternatively, the contents of this file may be used under the terms of the
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
// case the provisions of the LGPL are applicable instead of those above. If you
// wish to allow use of your version of this file only under the terms of the
// LGPL, and not to allow others to use your version of this file under the
// terms of the CDDL, indicate your decision by deleting the provisions above
// and replace them with the notice and other provisions required by the LGPL.
// If you do not delete the provisions above, a recipient may use your version
// of this file under the terms of either the CDDL or the LGPL.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ============================================================================
package net.sf.jga.fn;
import java.io.Serializable;
import net.sf.jga.fn.adaptor.Bind;
import net.sf.jga.fn.adaptor.ChainBinary;
import net.sf.jga.fn.adaptor.ChainUnary;
import net.sf.jga.fn.adaptor.Generate;
import net.sf.jga.fn.adaptor.Identity;
/**
* A Function Object that takes one argument and returns a result. The
* argument is of type <code>T</code>, and the result is of type <code>R</code>.
* <p>
* Copyright © 2002-2005 David A. Hall
*
* @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
**/
abstract public class UnaryFunctor<T, R> extends Functor<R> implements Serializable, Visitable {
/**
* Executes the function and returns the result.
*/
abstract public R fn(T arg);
/**
* FactoryMethod that creates a UnaryFunctor that passes its argument to
* the given functor, and uses the result as the argument to this
* function. Given argument <b>x</b>, the new functor will return
* <code>fn<sub>this</sub>(f(x)))</code>
*/
public <F> UnaryFunctor<F,R> compose(UnaryFunctor<F,T> f) {
return new ChainUnary<F,T,R>(this,f);
}
/**
* FactoryMethod that creates a BinaryFunctor that passes its arguments to
* the given functor, and uses the result as the argument to this
* function. Given arguments <b>x</b> and <b>y</b>, the new functor will
* return <code>fn<sub>this</sub>(f(x,y)))</code>
*/
public <F1,F2> BinaryFunctor<F1,F2,R> compose(BinaryFunctor<F1,F2,T> f) {
return new ChainBinary<F1,F2,T,R>(this, f);
}
/**
* FactoryMethod that creates a Generator to create the argument to this
* function. The new functor will return
* <code>fn<sub>this</sub>(gen())</code>.
*/
public Generator<R> generate(Generator<T> gen) {
return new Generate<T,R>(this, gen);
}
/**
* FactoryMethod that binds the argument arguments to this function to a specific
* value. The new functor will return
* <code>fn<sub>this</sub>(val)</code>.
*/
public Generator<R> bind(T val) {
return new Bind<T,R>(val, this);
}
// -----------------
// Functor interface
// -----------------
/**
* Executes the function and returns the result.
*/
public R eval(Object... args) {
// @SuppressWarnings
// This generates an unchecked cast warning: we're crossing the line from
// unsafe rawform code to typesafe generic code. This interface is known
// to be unsafe, and documented to the user as such.
return fn((T) args[0]);
}
// -------------------
// Visitable interface
// -------------------
/**
* No-op implementation of Visitable interface.
*/
public void accept(Visitor v) {}
}
|