001: // ============================================================================
002: // $Id: Accumulate.java,v 1.11 2006/01/08 00:52:25 davidahall Exp $
003: // Copyright (c) 2003-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: package net.sf.jga.fn.algorithm;
033:
034: import java.util.Iterator;
035: import net.sf.jga.fn.BinaryFunctor;
036: import net.sf.jga.fn.UnaryFunctor;
037:
038: /**
039: * Applies a BinaryFunctor to each element in an iteration, and returns the
040: * final result. Each member of the collection is passed to the functor along
041: * with the previous result. If the two arg constructor is used, then the given
042: * value is used on the first invocation of the functor: otherwise the first
043: * element of the iteration is consumed and passed as the starting value.
044: * <p>
045: * If the iteration was empty, then the result of this function is the starting
046: * value or null if no starting value was given.
047: * <p>
048: * If no starting value was given, and the iteration has exactly one element,
049: * then the element is returned without the BinaryFunctor being used.
050: * <p>
051: * To Serialize an Accumulate, the generic parameter T must be serializable.
052: * <p>
053: * Copyright © 2003-2005 David A. Hall
054: *
055: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
056: */
057:
058: public class Accumulate<T> extends
059: UnaryFunctor<Iterator<? extends T>, T> {
060:
061: static final long serialVersionUID = 4611344190624502921L;
062:
063: // Functor applied to each element in an iteration in turn
064: private BinaryFunctor<T, T, T> _fn;
065:
066: // The start value
067: private T _value;
068:
069: // Flag indicating that the start value was given
070: private boolean _givenValue = false;
071:
072: /**
073: * Builds an Accumulate functor that will use the given functor to process
074: * elements in an iteration. The first element in the iteration will be
075: * used as the start value.
076: */
077: public Accumulate(BinaryFunctor<T, T, T> fn) {
078: if (fn == null)
079: throw new IllegalArgumentException();
080:
081: _fn = fn;
082: }
083:
084: /**
085: * Builds an Accumulate functor that will use the given start value and
086: * functor to process elements in an iteration. The first element in the
087: * iteration will be used as the start value.
088: */
089: public Accumulate(T startValue, BinaryFunctor<T, T, T> fn) {
090: this (fn);
091: _value = startValue;
092: _givenValue = true;
093: }
094:
095: /**
096: * Returns the functor used to process elements in the iteration.
097: */
098: public BinaryFunctor<T, T, T> getFunction() {
099: return _fn;
100: }
101:
102: /**
103: * Returns the start value, or null if no start value was given.
104: */
105:
106: public T getStartValue() {
107: return _value;
108: }
109:
110: /**
111: * Returns true if a start value was passed at construction.
112: */
113:
114: public boolean hasStartValue() {
115: return _givenValue;
116: }
117:
118: /**
119: * Apply the functor to the elements of the iteration and return the final
120: * result. Results do not accumulate from one invocation to the next: each
121: * time this method is called, the accumulation starts over with the given
122: * start value.
123: */
124: public T fn(Iterator<? extends T> iterator) {
125: T value = _givenValue ? _value : iterator.hasNext() ? iterator
126: .next() : null;
127:
128: while (iterator.hasNext()) {
129: value = _fn.fn(value, iterator.next());
130: }
131:
132: return value;
133: }
134:
135: /**
136: * Calls the Visitor's <code>visit(Accumulate)</code> method, if it
137: * implements the nested Visitor interface.
138: */
139: public void accept(net.sf.jga.fn.Visitor v) {
140: if (v instanceof Accumulate.Visitor)
141: ((Accumulate.Visitor) v).visit(this );
142: else
143: v.visit(this );
144: }
145:
146: // Object overrides
147:
148: public String toString() {
149: return "Accumulate";
150: }
151:
152: // AcyclicVisitor
153:
154: /**
155: * Interface for classes that may interpret an <b>Accumulate</b> functor.
156: */
157: public interface Visitor extends net.sf.jga.fn.Visitor {
158: public void visit(Accumulate host);
159: }
160: }
|