And also, avoid redundant work.
package com.XXX.XXX.XXX; import com.XXX.XXX.XXX.utilities.NumericUtilities;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils; import java.util.Arrays; public class ChokeSizeReformatting { private static final String INVALID_CHARACTER="(TH)|(THS)|(in)|[`]|[']|[-]|[\"]|\\s";
private static final String[] IDENTIFIED_STRING={"FUL", "NONE", "OPEN","NO CHOKE","FO"};
private static final String[] SPECIAL_CASE = {".", "ADJ","\\"};
private static final String[] SPECIAL_CASE_REPLACEMENT = {"0.", "64","/"}; //fist step: to do basic cleaning of the data,
//and also indicate if the string is number or not.
public static double cleanChokeSizeString(String rawChokeSize) { String cleanedRawString = rawChokeSize.replaceAll(INVALID_CHARACTER,""); String modifiedString =
StringUtils.replaceEach(cleanedRawString,SPECIAL_CASE, SPECIAL_CASE_REPLACEMENT); //logic from LA, TX, OK, CO completion
if (Arrays.stream(IDENTIFIED_STRING).anyMatch(modifiedString::contains)) { modifiedString = "1"; }
else if (NumericUtilities.isDivisible(modifiedString)) { modifiedString = NumericUtilities.fractionToDecimal(modifiedString);
} else if (!NumberUtils.isNumber(modifiedString)) { modifiedString = "0"; } return Double.parseDouble(modifiedString); } }
package com.xxxx.xxxx.xxxx.utilities; import org.apache.commons.lang3.math.NumberUtils;public class NumericUtilities { //check if the data extracted from string can be dividedpublic static boolean isDivisible(String str){ int charIndex = (str.contains("/")? str.indexOf("/"): 0); return (str.contains("/") && NumberUtils.isNumber(str.substring(0,charIndex)) && NumberUtils.isNumber(str.substring(charIndex + 1)) && !str.substring(charIndex + 1).equals("0")) ; } //if data is fraction, then parse to decimalpublic static String fractionToDecimal(String str) { int charIndex = str.indexOf("/");double numerator = Double.parseDouble(str.substring(0, charIndex));double denominator = Double.parseDouble(str.substring(charIndex + 1)); return String.valueOf(numerator / denominator); } }