KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ============================================================================
2
// $Id: Accumulate.java,v 1.11 2006/01/08 00:52:25 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.fn.UnaryFunctor;
37
38 /**
39  * Applies a BinaryFunctor to each element in an iteration, and returns the
40  * final result. Each member of the collection is passed to the functor along
41  * with the previous result. If the two arg constructor is used, then the given
42  * value is used on the first invocation of the functor: otherwise the first
43  * element of the iteration is consumed and passed as the starting value.
44  * <p>
45  * If the iteration was empty, then the result of this function is the starting
46  * value or null if no starting value was given.
47  * <p>
48  * If no starting value was given, and the iteration has exactly one element,
49  * then the element is returned without the BinaryFunctor being used.
50  * <p>
51  * To Serialize an Accumulate, the generic parameter T must be serializable.
52  * <p>
53  * Copyright &copy; 2003-2005 David A. Hall
54  *
55  * @author <a HREF="mailto:davidahall@users.sf.net">David A. Hall</a>
56  */

57
58 public class Accumulate<T> extends UnaryFunctor<Iterator JavaDoc<? extends T>, T> {
59     
60     static final long serialVersionUID = 4611344190624502921L;
61     
62     // Functor applied to each element in an iteration in turn
63
private BinaryFunctor<T,T,T> _fn;
64
65     // The start value
66
private T _value;
67
68     // Flag indicating that the start value was given
69
private boolean _givenValue = false;
70
71     /**
72      * Builds an Accumulate functor that will use the given functor to process
73      * elements in an iteration. The first element in the iteration will be
74      * used as the start value.
75      */

76     public Accumulate(BinaryFunctor<T,T,T> fn) {
77         if (fn == null)
78             throw new IllegalArgumentException JavaDoc();
79         
80         _fn = fn;
81     }
82
83     /**
84      * Builds an Accumulate functor that will use the given start value and
85      * functor to process elements in an iteration. The first element in the
86      * iteration will be used as the start value.
87      */

88     public Accumulate(T startValue, BinaryFunctor<T,T,T> fn){
89         this(fn);
90         _value = startValue;
91         _givenValue = true;
92     }
93
94     /**
95      * Returns the functor used to process elements in the iteration.
96      */

97     public BinaryFunctor<T,T,T> getFunction() {
98         return _fn;
99     }
100
101     /**
102      * Returns the start value, or null if no start value was given.
103      */

104
105     public T getStartValue() { return _value; }
106
107     /**
108      * Returns true if a start value was passed at construction.
109      */

110
111     public boolean hasStartValue() { return _givenValue; }
112
113     /**
114      * Apply the functor to the elements of the iteration and return the final
115      * result. Results do not accumulate from one invocation to the next: each
116      * time this method is called, the accumulation starts over with the given
117      * start value.
118      */

119     public T fn(Iterator JavaDoc<? extends T> iterator) {
120         T value = _givenValue ? _value :
121             iterator.hasNext() ? iterator.next() : null;
122         
123         while (iterator.hasNext()) {
124             value = _fn.fn(value, iterator.next());
125         }
126
127         return value;
128     }
129     
130     /**
131      * Calls the Visitor's <code>visit(Accumulate)</code> method, if it
132      * implements the nested Visitor interface.
133      */

134     public void accept(net.sf.jga.fn.Visitor v) {
135         if (v instanceof Accumulate.Visitor)
136             ((Accumulate.Visitor)v).visit(this);
137         else
138             v.visit(this);
139     }
140
141     // Object overrides
142

143     public String JavaDoc toString() {
144         return "Accumulate";
145     }
146     
147     // AcyclicVisitor
148

149     /**
150      * Interface for classes that may interpret an <b>Accumulate</b> functor.
151      */

152     public interface Visitor extends net.sf.jga.fn.Visitor {
153         public void visit(Accumulate host);
154     }
155 }
156
Popular Tags