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 gov.va.med.pbm.ampl.utility.NumberUtility;

/**
* A common core of String operations.
*
* @author Ian Meinert
* @version %I%
* @since 1.0
*
*/
public final class StringUtility {

/**
* The StringUtility default constructor, which is not called.
*/
private StringUtility() {
// not called
}

/**
* Validates a given String is numeric.
*
* @param originalString the number as String
* @return boolean
*/
public static boolean isNumeric(String originalString) {
return originalString != null && originalString.matches("^-?\\d+(\\.\\d+)?$");
}

/**
* Validates a given String is numeric and has a given length.
*
* @param originalString the String to validate
* @param length the length of the String
* @return boolean
*/
public static boolean isNumericOfLength(String originalString, int length) {
return isNumeric(originalString) && originalString.length() == length;
}

/**
* Pads the left of a String for a specified length with a given character.
*
* @param originalString the original String to pad
* @param length the length of the pad
* @param padCharacter the character to pad with
* @return the padded String
*/
public static String leftPad(String originalString, int length, char padCharacter) {
String paddedString = originalString;
while (paddedString.length() < length) {
paddedString = padCharacter + paddedString;
}
return paddedString;
}

/**
* Pads the right of a String for a specified length with a given character.
*
* @param originalString the original String to pad
* @param length the length of the pad
* @param padCharacter the character to pad with
* @return the padded String
*/
public static String rightPad(String originalString, int length, char padCharacter) {
String paddedString = originalString;
while (paddedString.length() < length) {
paddedString = paddedString + padCharacter;
}
return paddedString;
}

/**
* Sets the parameter to a zero-length String if it is empty.
*
* @param originalString the String to validate
* @return String
*/
public static String clearNull(String originalString) {
String value = originalString == null ? new String() : originalString;
return value;
}

/**
* Selects a random character from the given string.
*
* @param s the string to select from
* @return string
*/
public static String selectARandomChar(String s) {
int index = NumberUtility .randomBetween(0, s.length());
return new String() + s.charAt(index);
}

/**
* The toString converts an int to a String.
*
* @param value the int to convert
* @return a String representation of the int
*/
public static String toString(int value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
String string = sb.toString();
return string;
}
}