diff --git a/src/main/java/com/samskivert/util/Folds.java b/src/main/java/com/samskivert/util/Folds.java new file mode 100644 index 00000000..156331fb --- /dev/null +++ b/src/main/java/com/samskivert/util/Folds.java @@ -0,0 +1,112 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2010 Michael Bayne, et al. +// +// This library 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; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library 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. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +import java.math.BigInteger; +import java.util.Collection; +import java.util.Map; + +/** + * Various useful folds over iterables. + */ +public class Folds +{ + /** + * Sums the supplied collection of numbers to an int with a left to right fold. + */ + public static int sum (int zero, Iterable values) + { + for (Number value : values) { + zero += value.intValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a long with a left to right fold. + */ + public static long sum (long zero, Iterable values) + { + for (Number value : values) { + zero += value.longValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a float with a left to right fold. + */ + public static float sum (float zero, Iterable values) + { + for (Number value : values) { + zero += value.floatValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a double with a left to right fold. + */ + public static double sum (double zero, Iterable values) + { + for (Number value : values) { + zero += value.doubleValue(); + } + return zero; + } + + /** + * Sums the supplied collection of numbers to a bigint with a left to right fold. + */ + public static BigInteger sum (BigInteger zero, Iterable values) + { + for (BigInteger value : values) { + zero = zero.add(value); + } + return zero; + } + + /** + * Merges the supplied collections into zero using a left to right fold of + * {@link Collection#addAll}. + */ + public static > C addAll ( + C zero, Iterable> values) + { + for (Collection value : values) { + zero.addAll(value); + } + return zero; + } + + /** + * Merges the supplied maps into zero using a left to right fold of + * {@link Map#putAll}. + */ + public static > M putAll ( + M zero, Iterable> values) + { + for (Map value : values) { + zero.putAll(value); + } + return zero; + } +}