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

net.sf.staccatocommons.collections.reduction.Reductions Maven / Gradle / Ivy

/*
 Copyright (c) 2011, The Staccato-Commons 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.collections.reduction;

import static net.sf.staccatocommons.lang.number.NumberTypes.*;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import net.sf.staccatocommons.collections.reduction.internal.FoldReducer;
import net.sf.staccatocommons.collections.reduction.internal.MapReduction;
import net.sf.staccatocommons.collections.stream.Stream;
import net.sf.staccatocommons.collections.stream.Streams;
import net.sf.staccatocommons.defs.Applicable;
import net.sf.staccatocommons.defs.Applicable2;
import net.sf.staccatocommons.defs.type.NumberType;
import net.sf.staccatocommons.lang.Compare;
import net.sf.staccatocommons.lang.function.Functions;
import net.sf.staccatocommons.restrictions.Constant;

import org.apache.commons.lang.mutable.MutableInt;

/**
 * Common {@link Reduction}s modeled after SQL aggregate
 * functions
 * 
 * @author flbulgarelli
 * @since 1.2
 */
public class Reductions {

  @Constant
  public static  Reduction count() {
    return new AbstractReduction() {
      public Integer reduce(A element, Integer accum) {
        return accum + 1;
      }

      public Integer initial() {
        return 0;
      }
    };
  }

  @Constant
  public static  Reduction fastCount() {
    return new AbstractReduction() {
      public MutableInt reduce(A element, MutableInt accum) {
        accum.increment();
        return accum;
      }

      public MutableInt initial() {
        return new MutableInt();
      }
    };
  }

  public static  Reduction> append() {
    return new AbstractReduction>() {
      public Stream reduce(A element, Stream accum) {
        return Streams.cons(element, accum);
      }

      public Stream initial() {
        return Streams.empty();
      }
    };
  }

  public static  Reduction> fastAppend() {
    return new AbstractReduction>() {
      public List reduce(A element, List accum) {
        accum.add(element);
        return accum;
      }

      public List initial() {
        return new LinkedList();
      }
    };
  }

  @Constant
  public static Reduction sum() {
    return sum(integer());
  }

  public static  Reduction sum(NumberType numberType) {
    return from(numberType.zero(), numberType.add());
  }

  public static  Reduction sumOn(Applicable function) {
    return sumOn(function, integer());
  }

  public static  Reduction sumOn(Applicable function, NumberType numberType) {
    return from(numberType.zero(), numberType.add().of(function).flip());
  }

  @Constant
  public static  Reduction first() {
    return new AbstractReduction() {
      public A reduce(A element, A accum) {
        return accum;
      }

      public A initial(A element) {
        return element;
      }
    };
  }

  @Constant
  public static > Reduction max() {
    return max(Compare. natural());
  }

  public static  Reduction max(Comparator comparator) {
    return from(Compare.max(comparator));
  }

  public static > Reduction maxOn(Applicable function) {
    return max(Compare.on(function));
  }

  @Constant
  public static > Reduction min() {
    return min(Compare. natural());
  }

  public static  Reduction min(Comparator comparator) {
    return from(Compare.min(comparator));
  }

  public static > Reduction minOn(Applicable function) {
    return min(Compare.on(function));
  }

  public static  Reduction from(Applicable2 function) {
    return from(Functions. identity(), function);
  }

  /***/
  public static  Reduction from(Applicable unary,
    Applicable2 binary) {
    return new MapReduction(unary, binary);
  }

  public static  Reduction from(B initial, Applicable2 function) {
    return new FoldReducer(function, initial);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy