KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > algorithm > TransformBinary


1 // ============================================================================
2
// $Id: TransformBinary.java,v 1.10 2006/05/08 02:22:04 davidahall Exp $
3
// Copyright (c) 2003-2005 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
package net.sf.jga.fn.algorithm;
33
34 import java.util.Iterator JavaDoc;
35 import net.sf.jga.fn.BinaryFunctor;
36 import net.sf.jga.util.TransformBinaryIterator;
37
38 /**
39  * Applies a BinaryFunctor to corresponding elements in a pair of iterations,
40  * and iterates over the results.
41  * <p>
42  * Copyright &copy; 2003-2005 David A. Hall
43  *
44  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
45  * @deprecated
46  */

47
48 public class TransformBinary<T1,T2,R>
49     extends BinaryFunctor<Iterator JavaDoc<? extends T1>, Iterator JavaDoc<? extends T2>,
50                           Iterator JavaDoc<R>>
51 {
52     static final long serialVersionUID = -6353149079946565288L;
53     
54     // the functor used to transform two iterators
55
private BinaryFunctor<T1,T2,R> _fn;
56
57     /**
58      * Builds an TransformBinary functor that will use the given functor to
59      * process corresponding elements in a pair of iterations.
60      * @throws IllegalArgumentException if the functor is null
61      */

62     public TransformBinary(BinaryFunctor<T1,T2,R> fn) {
63         if (fn == null)
64             throw new IllegalArgumentException JavaDoc();
65         
66         _fn = fn;
67     }
68
69     /**
70      * Returns the functor used to process elements in a pair of iterations.
71      */

72     public BinaryFunctor<T1,T2,R> getFunction() {
73         return _fn;
74     }
75
76     /**
77      * Apply the functor to corresponding elements in the iterations and return
78      * an iterator over the results. The resulting iterator will contain the
79      * same number of elements as the shorter of the two input iterators (if
80      * one of the input iterators is empty, then the resulting iterator will
81      * also be empty).
82      *
83      * @return an iterator over the results of the transformation
84      */

85     public Iterator JavaDoc<R>
86     fn(Iterator JavaDoc<? extends T1> i1, Iterator JavaDoc<? extends T2> i2)
87     {
88         return new TransformBinaryIterator<T1,T2,R>(i1,i2,_fn);
89     }
90     
91     /**
92      * Calls the Visitor's <code>visit(Transform)</code> method, if it
93      * implements the nested Visitor interface.
94      */

95     public void accept(net.sf.jga.fn.Visitor v) {
96         if (v instanceof TransformBinary.Visitor)
97             ((TransformBinary.Visitor)v).visit(this);
98         else
99             v.visit(this);
100     }
101
102     // Object overrides
103

104     public String JavaDoc toString() {
105         return "TransformBinary("+_fn+")";
106     }
107     
108     // AcyclicVisitor
109

110     /**
111      * Interface for classes that may interpret an <b>Transform</b> functor.
112      */

113     public interface Visitor extends net.sf.jga.fn.Visitor {
114         public void visit(TransformBinary host);
115     }
116 }
117
Popular Tags