/*
 *  Copyright 2011 Eric F. Savage, code@efsavage.com
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.ajah.util;

/**
 * Utilities that do math.
 * 
 * @author <a href="http://efsavage.com">Eric F. Savage</a>, <a
 *         href="mailto:code@efsavage.com">code@efsavage.com</a>.
 * 
 */
public class MathUtils {

	/**
	 * Reduces a value by a percentage, with a minimum change. Examples:
	 * 
	 * Reducing 100 by 5% with a minimum of 1 would yield 95.
	 * 
	 * Reducing 100 by 7% with a minimum of 10 would yield 90.
	 * 
	 * Reducing 1 by 10% with a minimum of 2 would yield -2.
	 * 
	 * @param value
	 * @param percentageChange
	 * @param minChange
	 * @return The lesser of the value minus the value * percentage, or the
	 *         value minus the minChange.
	 */
	public static int reduce(int value, double percentageChange, int minChange) {
		int reduction = (int) (value * percentageChange);
		if (reduction > minChange) {
			return value - minChange;
		}
		return value - reduction;
	}

}
@@ -1534,9 +1534,1400 @@
 on;%0A%09%7D%0A%0A
+%09/**%0A%09 * Divides a divident by divisor , with a default value if the divisor is%0A%09 * zero.%0A%09 * %0A%09 * @param dividend%0A%09 *            The dividen.%0A%09 * @param divisor%0A%09 *            The divisor.%0A%09 * @param defaultDivisor%0A%09 *            The defaultDivisor, if the divisor is zero.%0A%09 * @return divident/divisor or divident/defaultDivisor%0A%09 */%0A%09public static long divideZero(long dividend, long divisor, long defaultDivisor) %7B%0A%09%09if (divisor == 0) %7B%0A%09%09%09return dividend / defaultDivisor;%0A%09%09%7D%0A%09%09return dividend / divisor;%0A%09%7D%0A%0A%09/**%0A%09 * Adds an array of longs to another array of longs. The resulting array%0A%09 * will be as long as the longer of the two parameters.%0A%09 * %0A%09 * @param array1%0A%09 * @param array2%0A%09 * @return%0A%09 */%0A%09public static long%5B%5D add(long%5B%5D array1, long%5B%5D array2) %7B%0A%09%09AjahUtils.requireParam(array1, %22array1%22);%0A%09%09AjahUtils.requireParam(array2, %22array2%22);%0A%09%09long%5B%5D result = new long%5Barray1.length %3E array2.length ? array1.length : array2.length%5D;%0A%09%09for (int i = 0; i %3C result.length; i++) %7B%0A%09%09%09if (array1.length %3C i) %7B%0A%09%09%09%09if (array2.length %3C i) %7B%0A%09%09%09%09%09// Shouldn't happen%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%09%09%09%7D else if (array2.length %3C i) %7B%0A%09%09%09%09continue;%0A%09%09%09%7D else %7B%0A%09%09%09%09result%5Bi%5D = array1%5Bi%5D + array2%5Bi%5D;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return result;%0A%09%7D%0A%0A%09public static void addTo(final long%5B%5D to, final long%5B%5D from) %7B%0A%09%09for (int i = 0; i %3C to.length; i++) %7B%0A%09%09%09to%5Bi%5D = to%5Bi%5D + from%5Bi%5D;%0A%09%09%7D%0A%09%7D%0A%0A
 %7D

Add some simple math methods