001: // ============================================================================
002: // $Id: Generate1st.java,v 1.7 2006/01/08 00:52:25 davidahall Exp $
003: // Copyright (c) 2004-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.adaptor;
034:
035: import net.sf.jga.fn.BinaryFunctor;
036: import net.sf.jga.fn.Generator;
037: import net.sf.jga.fn.UnaryFunctor;
038:
039: /**
040: * UnaryFunctor that uses a Generator to produce the 1st argument to a given
041: * BinaryFunctor.
042: * <p>
043: * Copyright © 2004-2005 David A. Hall
044: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
045: */
046:
047: public class Generate1st<T1, T2, R> extends UnaryFunctor<T2, R> {
048:
049: static final long serialVersionUID = -7075574361370967089L;
050:
051: // The Functor applied to each value generated
052: private BinaryFunctor<T1, T2, R> _fn;
053:
054: // The Nested Generator
055: private Generator<T1> _gen;
056:
057: public Generate1st(BinaryFunctor<T1, T2, R> fn, Generator<T1> gen) {
058: _fn = fn;
059: _gen = gen;
060: }
061:
062: /**
063: * Returns the Functor that is invoked
064: */
065:
066: public BinaryFunctor<T1, T2, R> getFunctor() {
067: return _fn;
068: }
069:
070: /**
071: * Returns the generator that produces the 1st argument.
072: */
073:
074: public Generator<T1> getGenerator() {
075: return _gen;
076: }
077:
078: /**
079: * Returns the results of the functor, using the nested generator to
080: * produce the first argument.
081: */
082: public R fn(T2 arg) {
083: return _fn.fn(_gen.gen(), arg);
084: }
085:
086: /**
087: * Calls the Visitor's <code>visit(Generate1st)</code> method, if it
088: * implements the nested Visitor interface.
089: */
090: public void accept(net.sf.jga.fn.Visitor v) {
091: if (v instanceof Generate1st.Visitor)
092: ((Generate1st.Visitor) v).visit(this );
093: else
094: v.visit(this );
095: }
096:
097: // Object overrides
098:
099: public String toString() {
100: return _fn + ".generate1st(" + _gen + ")";
101: }
102:
103: // Acyclic Visitor
104:
105: /**
106: * Interface for classes that may interpret a <b>Generate1st</b>
107: * functor.
108: */
109: public interface Visitor extends net.sf.jga.fn.Visitor {
110: public void visit(Generate1st host);
111: }
112: }
|