Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
package gov.va.med.pbm.ampl.utility;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParsePosition;
import org.owasp.esapi.ESAPI;
import gov.va.med.pbm.ampl.constant.AmplConstants;
/**
* A common core of numeric operations.
*
* @author Ian Meinert
* @version %I%
* @since 1.0
*
*/
public final class NumberUtility {
/**
* The NumberUtility default constructor, which is not called.
*/
private NumberUtility() {
// not called
}
/**
* Converts a String to an Integer.
*
* @param originalString the String to be converted
* @return Integer
*/
public static Integer integerValueOf(String originalString) {
Integer integer = originalString.isEmpty() ? 0 : Integer.valueOf(originalString);
return integer;
}
/**
* Converts a String to a Float.
*
* @param originalString the String to be converted
* @return Float
*/
public static Float floatValueOf(String originalString) {
float val = originalString.isEmpty() || !isNumeric(originalString) ? 0 : Float.valueOf(originalString).floatValue();
return val;
}
/**
* Converts a String to a Long.
*
* @param originalString the String to be converted
* @return Long
*/
public static Long longValueOf(String originalString) {
if (originalString.isEmpty()) {
return 0L;
} else {
return Long.valueOf(originalString).longValue();
}
}
/**
* Converts an Integer to a Long.
*
* @param originalInteger the Integer to be converted
* @return Long
*/
public static Long longValueOf(Integer originalInteger) {
if (originalInteger.equals(0)) {
return 0L;
} else {
return Long.valueOf(originalInteger).longValue();
}
}
/**
* Converts an Integer to a BigDecimal.
*
* @param originalInteger the Integer to be converted
* @return BigDecimal
*/
public static BigDecimal bigDecimalValueOf(Integer originalInteger) {
return BigDecimal.valueOf(originalInteger);
}
/**
* Converts a String to a BigDecimal.
*
* @param originalString the String to be converted
* @return BigDecimal
*/
public static BigDecimal bigDecimalValueOf(String originalString) {
return new BigDecimal(originalString.replaceAll(",", new String()));
}
/**
* Converts a Long to a BigDecimal.
*
* @param originalLong the Long to be converted
* @return BigDecimal
*/
public static BigDecimal bigDecimalValueOf(Long originalLong) {
return new BigDecimal(originalLong);
}
/**
* Converts a String to a double.
*
* @param originalString the double to be converted
* @return double
*/
public static double doubleValueOf(String originalString) {
return isNumeric(originalString) ? Double.parseDouble(originalString) : 0.0;
}
/**
* Generates a random number between two values.
*
* @param start The starting value
* @param end The ending value
* @return int
*/
public static int randomBetween(int start, int end) {
return (int) ESAPI.randomizer().getRandomReal(start, end);
}
/**
* Generates a random Double between two values.
*
* @param start The starting value
* @param end The ending value
* @return Double
*/
public static Float randomFloatBetween(Float start, Float end) {
return ESAPI.randomizer().getRandomReal(start, end);
}
/**
* Generates a random Long number.
*
* @return Long
*/
public static Long randomLong() {
return ESAPI.randomizer().getRandomLong();
}
/**
* Generates a random Integer number.
*
* @return Integer
*/
public static Integer randomInt() {
int range = AmplConstants.RANDOM_MAX_INT_RANGE;
return ESAPI.randomizer().getRandomInteger(0, range);
}
/**
* Generates a random Double number.
*
* @return Double
*/
public static Float randomFloat() {
int range = AmplConstants.RANDOM_MAX_INT_RANGE;
return ESAPI.randomizer().getRandomReal(0, range);
}
/**
* Performs a check to determine if a given String is numeric.
* <p>
* If, after parsing the string, the parser position is at the end of the string, then the entire string is numeric
*
* @param originalString the String to check
* @return boolean value determined if the param is numeric
*/
public static boolean isNumeric(String originalString) {
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(originalString, pos);
return originalString.length() == pos.getIndex();
}
/**
* Performs a check to determine if a given number is prime.
*
* @param number the integer to check
* @return boolean value determined if the param is prime
*/
public static boolean isPrime(int number) {
// check if the number is a multiple of 2
if (number % 2 == 0)
return false;
// if not, then just check the odds
for (int i = AmplConstants.NUMBER_THREE; i * i <= number; i += 2) {
if (number % i == 0)
return false;
}
return true;
}
}