All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.staccatocommons.reductions.AbstractReduction Maven / Gradle / Ivy

/**
 *  Copyright (c) 2010-2012, The StaccatoCommons Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; version 3 of the License.
 *
 *  This program 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.  See the
 *  GNU Lesser General Public License for more details.
 */

package net.sf.staccatocommons.reductions;

import net.sf.staccatocommons.defs.Applicable;
import net.sf.staccatocommons.defs.reduction.Accumulator;
import net.sf.staccatocommons.defs.reduction.Reduction;

/**
 * 
 * @author flbulgarelli
 * 
 * @param 
 * @param 
 */
public abstract class AbstractReduction implements Reduction {

  @Override
  public final  Reduction then(final Applicable function) {
    return new AbstractReduction() {
      public Accumulator newAccumulator() {
        final Accumulator start = AbstractReduction.this.newAccumulator();
        return new Accumulator() {
          public void accumulate(A element) {
            start.accumulate(element);
          }

          public C value() {
            return function.apply(start.value());
          }
        };
      }
    };
  }

  @Override
  public final  Reduction of(final Applicable function) {
    return new AbstractReduction() {
      public Accumulator newAccumulator() {
        final Accumulator accum = AbstractReduction.this.newAccumulator();
        return new Accumulator() {
          public void accumulate(C element) {
            accum.accumulate(function.apply(element));
          }

          public B value() {
            return accum.value();
          }
        };
      }
    };
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy