package net.sf.kerner.utils.math;

import java.util.ArrayList;
import java.util.List;

import net.sf.kerner.utils.counter.Counter;

public class MathUtils {

	private MathUtils() {
	}

	/**
	 * Round a floating point number ({@code double}) with an accuracy up to
	 * given decimal place.
	 * 
	 * @param number
	 *            {@code double} that is rounded to given decimal place
	 * @param decimalPlace
	 *            decimal place to which given {@code double} is rounded
	 * @return rounded {@code double}
	 */
	public static double round(double number, int decimalPlace) {
		int n = decimalPlace;
		Counter c = new Counter();
		while (n > 10) {
			n = n / 10;
			c.count();
		}

		decimalPlace = decimalPlace + c.getCount();
		// System.out.println("number: " + number + ", digits: " +
		// decimalPlace);
		final double factor = Math.pow(10, decimalPlace);
		final double result = Math.round(number * factor) / factor;
		// System.out.println("result: " + result);
		return result;
	}

	public static int max(int... values) {
		int result = Integer.MIN_VALUE;
		for (int i : values) {
			if (i > result)
				result = i;
		}
		return result;
	}

	public static int min(int... values) {
		int result = Integer.MAX_VALUE;
		for (int i : values) {
			if (i < result)
				result = i;
		}
		return result;
	}
	
	public static double mean(double... values) {
		if(values.length < 1)
			throw new IllegalArgumentException();
		return sum(values) / values.length;
	}
	
	public static double sum(double... values) {
		double result = 0;
		for (double d : values) {
			result += d;
		}
		return result;
	}
	
	public static double stdDev(double... values){
		double sumOfSquares = 0;
		final double mean = mean(values);
		for(double d : values)
			sumOfSquares += (d - mean) * (d - mean);
		return Math.sqrt(sumOfSquares /( values.length - 1));
	}
	
	static <V> List<V> trimm(List<V> list){
		final List<V> result = new ArrayList<V>();
		for(V o : list){
			if(o != null)
				result.add(o);
		}
		return result;
	}
	
	public static Integer increment(Integer integer){
		return Integer.valueOf(integer.intValue() + 1);
	}
	
	public static Integer add(Integer integer1, Integer integer2){
		return Integer.valueOf(integer1.intValue() + integer2.intValue());
	}
}

@@ -1,135 +1,984 @@
-package net.sf.kerner.utils.math;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport net.sf.kerner.ut
+/**********************************************************************%0ACopyright (c) 2009-2011 Alexander Kerner. All rights reserved.%0ALicensed under the Apache License, Version 2.0 (the %22License%22);%0Ayou may not use this file except in compliance with the License.%0AYou may obtain a copy of the License at%0A%0A    http://www.apache.org/licenses/LICENSE-2.0%0A%0AUnless required by applicable law or agreed to in writing, software%0Adistributed under the License is distributed on an %22AS IS%22 BASIS,%0AWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0ASee the License for the specific language governing permissions and%0Alimitations under the License.%0A ***********************************************************************/%0A%0Apackage net.sf.kerner.utils.math;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * %0A * Provides static helper methods. %0A * %0A *%0A * @author %3Ca href=%22mailto:alex.kerner.24@googlema
 il
-s
 .co
-unter.Counter;%0A
+m%22%3EAlexander Kerner%3C/a%3E%0A * @version 2011-10-25%0A *%0A */
 %0Apub
@@ -1450,33 +1450,17 @@
 ;%0A%09%09
-Counter c = new Counter()
+int c = 0
 ;%0A%09%09
@@ -1499,23 +1499,17 @@
 %09%09%09c
-.count()
+ ++
 ;%0A%09%09%7D%0A
-%0A
 %09%09de
@@ -1541,100 +1541,8 @@
  + c
-.getCount();%0A%09%09// System.out.println(%22number: %22 + number + %22, digits: %22 +%0A%09%09// decimalPlace)
 ;%0A%09%09
@@ -1657,54 +1657,8 @@
 or;%0A
-%09%09// System.out.println(%22result: %22 + result);%0A
 %09%09re

Add JavaDoc. round() does not use Counter anymore but simple int.