Package com.cedarsoftware.util
Class MathUtilities
java.lang.Object
com.cedarsoftware.util.MathUtilities
Mathematical utility class providing enhanced numeric operations and algorithms.
This class provides:
- Minimum/Maximum calculations for various numeric types
- Smart numeric parsing with minimal type selection
- Permutation generation
- Common mathematical constants
Features:
- Support for primitive types (long, double)
- Support for BigInteger and BigDecimal
- Null-safe operations
- Efficient implementations
- Thread-safe operations
- 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
License
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BigDecimal
static final BigDecimal
static final BigInteger
static final BigInteger
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
maximum
(double... values) Calculate the minimum value from an array of values.static long
maximum
(long... values) Calculate the minimum value from an array of values.static BigDecimal
maximum
(BigDecimal... values) Calculate the maximum value from an array of values.static BigInteger
maximum
(BigInteger... values) Calculate the minimum value from an array of values.static double
minimum
(double... values) Calculate the minimum value from an array of values.static long
minimum
(long... values) Calculate the minimum value from an array of values.static BigDecimal
minimum
(BigDecimal... values) Calculate the minimum value from an array of values.static BigInteger
minimum
(BigInteger... values) Calculate the minimum value from an array of values.static <T extends Comparable<? super T>>
booleannextPermutation
(List<T> list) Generates the next lexicographically ordered permutation of the given list.static Number
parseToMinimalNumericType
(String numStr) Parses a string representation of a number into the most appropriate numeric type.
-
Field Details
-
BIG_INT_LONG_MIN
-
BIG_INT_LONG_MAX
-
BIG_DEC_DOUBLE_MIN
-
BIG_DEC_DOUBLE_MAX
-
-
Method Details
-
minimum
public static long minimum(long... values) Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
maximum
public static long maximum(long... values) Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
minimum
public static double minimum(double... values) Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
maximum
public static double maximum(double... values) Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
minimum
Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
maximum
Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
minimum
Calculate the minimum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- minimum value of the provided set.
-
maximum
Calculate the maximum value from an array of values.- Parameters:
values
- Array of values.- Returns:
- maximum value of the provided set.
-
parseToMinimalNumericType
Parses a string representation of a number into the most appropriate numeric type.This method intelligently selects the smallest possible numeric type that can accurately represent the value, following these rules:
- Integer values within Long range: returns
Long
- Integer values outside Long range: returns
BigInteger
- Decimal values within Double precision: returns
Double
- Decimal values requiring more precision: returns
BigDecimal
Examples:
parseToMinimalNumericType("123") → Long(123) parseToMinimalNumericType("1.23") → Double(1.23) parseToMinimalNumericType("1e308") → BigDecimal parseToMinimalNumericType("999999999999999999999") → BigInteger
- Parameters:
numStr
- the string to parse, must not be null- Returns:
- the parsed number in its most appropriate type
- Throws:
NumberFormatException
- if the string cannot be parsed as a numberIllegalArgumentException
- if numStr is null
- Integer values within Long range: returns
-
nextPermutation
Generates the next lexicographically ordered permutation of the given list.This method modifies the input list in-place to produce the next permutation. If there are no more permutations possible, it returns false.
Example:
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3)); do { System.out.println(list); // Prints each permutation } while (nextPermutation(list)); // Output: // [1, 2, 3] // [1, 3, 2] // [2, 1, 3] // [2, 3, 1] // [3, 1, 2] // [3, 2, 1]
- Type Parameters:
T
- type of elements in the list, must implement Comparable- Parameters:
list
- the list to permute, will be modified in-place- Returns:
- true if a next permutation exists and was generated, false if no more permutations exist
- Throws:
IllegalArgumentException
- if list is null
-