
com.cedarsoftware.util.MathUtilities Maven / Gradle / Ivy
package com.cedarsoftware.util;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Useful Math utilities
*
* @author John DeRegnaucourt ([email protected])
*
* Copyright (c) Cedar Software LLC
*
* 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.
*/
public class MathUtilities
{
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static long minimum(long... values)
{
int len = values.length;
long current = values[0];
for (int i=1; i < len; i++)
{
current = Math.min(values[i], current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static long maximum(long... values)
{
int len = values.length;
long current = values[0];
for (int i=1; i < len; i++)
{
current = Math.max(values[i], current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static double minimum(double... values)
{
int len = values.length;
double current = values[0];
for (int i=1; i < len; i++)
{
current = Math.min(values[i], current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static double maximum(double... values)
{
int len = values.length;
double current = values[0];
for (int i=1; i < len; i++)
{
current = Math.max(values[i], current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static BigInteger minimum(BigInteger... values)
{
int len = values.length;
if (len == 1)
{
if (values[0] == null)
{
throw new IllegalArgumentException("Cannot passed null BigInteger entry to minimum()");
}
return values[0];
}
BigInteger current = values[0];
for (int i=1; i < len; i++)
{
if (values[i] == null)
{
throw new IllegalArgumentException("Cannot passed null BigInteger entry to minimum()");
}
current = values[i].min(current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static BigInteger maximum(BigInteger... values)
{
int len = values.length;
if (len == 1)
{
if (values[0] == null)
{
throw new IllegalArgumentException("Cannot passed null BigInteger entry to maximum()");
}
return values[0];
}
BigInteger current = values[0];
for (int i=1; i < len; i++)
{
if (values[i] == null)
{
throw new IllegalArgumentException("Cannot passed null BigInteger entry to maximum()");
}
current = values[i].max(current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static BigDecimal minimum(BigDecimal... values)
{
int len = values.length;
if (len == 1)
{
if (values[0] == null)
{
throw new IllegalArgumentException("Cannot passed null BigDecimal entry to minimum()");
}
return values[0];
}
BigDecimal current = values[0];
for (int i=1; i < len; i++)
{
if (values[i] == null)
{
throw new IllegalArgumentException("Cannot passed null BigDecimal entry to minimum()");
}
current = values[i].min(current);
}
return current;
}
/**
* Calculate the minimum value from an array of values.
*
* @param values Array of values.
* @return minimum value of the provided set.
*/
public static BigDecimal maximum(BigDecimal... values)
{
int len = values.length;
if (len == 1)
{
if (values[0] == null)
{
throw new IllegalArgumentException("Cannot passed null BigDecimal entry to maximum()");
}
return values[0];
}
BigDecimal current = values[0];
for (int i=1; i < len; i++)
{
if (values[i] == null)
{
throw new IllegalArgumentException("Cannot passed null BigDecimal entry to maximum()");
}
current = values[i].max(current);
}
return current;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy