Author: arunpatidar
Date: Mon Jul 4 15:19:07 2016 New Revision: 1751307 URL: http://svn.apache.org/viewvc?rev=1751307&view=rev Log: Applied patch from jira issue - OFBIZ-7593 - Enforce noninstantiability to UtilNumber class. Thanks Rishi Solanki and Rohit Koushal for your contribution. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilNumber.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilNumber.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilNumber.java?rev=1751307&r1=1751306&r2=1751307&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilNumber.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilNumber.java Mon Jul 4 15:19:07 2016 @@ -25,7 +25,7 @@ import java.util.Locale; import com.ibm.icu.text.RuleBasedNumberFormat; -public class UtilNumber { +public final class UtilNumber { public static String module = UtilNumber.class.getName(); @@ -36,86 +36,9 @@ public class UtilNumber { private static final int DEFAULT_BD_SCALE = 2; private static final int DEFAULT_BD_ROUNDING_MODE = BigDecimal.ROUND_HALF_UP; - /** - * Method to get BigDecimal scale factor from a property - * @param file - Name of the property file - * @param property - Name of the config property from arithmeticPropertiesFile (e.g., "invoice.decimals") - * @return int - Scale factor to pass to BigDecimal's methods. Defaults to DEFAULT_BD_SCALE (2) - */ - public static int getBigDecimalScale(String file, String property) { - if (UtilValidate.isEmpty(file)) return DEFAULT_BD_SCALE; - if (UtilValidate.isEmpty(property)) return DEFAULT_BD_SCALE; - - int scale = -1; - String value = UtilProperties.getPropertyValue(file, property); - if (value != null) { - try { - scale = Integer.parseInt(value); - } catch (NumberFormatException e) { - } - } - if (scale == -1) { - Debug.logWarning("Could not set decimal precision from " + property + "=" + value + ". Using default scale of " + DEFAULT_BD_SCALE + ".", module); - scale = DEFAULT_BD_SCALE; - } - return scale; - } - - /** - * As above, but use the default properties file - */ - public static int getBigDecimalScale(String property) { - return getBigDecimalScale(arithmeticPropertiesFile, property); - } - - /** - * Method to get BigDecimal rounding mode from a property - * @param file - Name of the property file - * @param property - Name of the config property from arithmeticPropertiesFile (e.g., "invoice.rounding") - * @return int - Rounding mode to pass to BigDecimal's methods. Defaults to DEFAULT_BD_ROUNDING_MODE (BigDecimal.ROUND_HALF_UP) - */ - public static int getBigDecimalRoundingMode(String file, String property) { - if (UtilValidate.isEmpty(file)) return DEFAULT_BD_SCALE; - if (UtilValidate.isEmpty(property)) return DEFAULT_BD_ROUNDING_MODE; - - String value = UtilProperties.getPropertyValue(file, property); - int mode = roundingModeFromString(value); - if (mode == -1) { - Debug.logWarning("Could not set decimal rounding mode from " + property + "=" + value + ". Using default mode of " + DEFAULT_BD_SCALE + ".", module); - return DEFAULT_BD_ROUNDING_MODE; - } - return mode; - } - - /** - * As above, but use the default properties file - */ - public static int getBigDecimalRoundingMode(String property) { - return getBigDecimalRoundingMode(arithmeticPropertiesFile, property); - } - - /** - * Method to get the BigDecimal rounding mode int value from a string name. - * @param value - The name of the mode (e.g., "ROUND_HALF_UP") - * @return int - The int value of the mode (e.g, BigDecimal.ROUND_HALF_UP) or -1 if the input was bad. - */ - public static int roundingModeFromString(String value) { - if (value == null) return -1; - value = value.trim(); - if ("ROUND_HALF_UP".equals(value)) return BigDecimal.ROUND_HALF_UP; - else if ("ROUND_HALF_DOWN".equals(value)) return BigDecimal.ROUND_HALF_DOWN; - else if ("ROUND_HALF_EVEN".equals(value)) return BigDecimal.ROUND_HALF_EVEN; - else if ("ROUND_UP".equals(value)) return BigDecimal.ROUND_UP; - else if ("ROUND_DOWN".equals(value)) return BigDecimal.ROUND_DOWN; - else if ("ROUND_CEILING".equals(value)) return BigDecimal.ROUND_CEILING; - else if ("ROUND_FLOOR".equals(value)) return BigDecimal.ROUND_FLOOR; - else if ("ROUND_UNNECCESSARY".equals(value)) return BigDecimal.ROUND_UNNECESSARY; - return -1; - } - // ICU4J rule sets for the en_US locale. To add more rules, expand this string. // For reference, see the RbnfSampleRuleSets.java file distributed with ICU4J - public static final String ruleSet_en_US = + private static final String ruleSet_en_US = /* * These rules format a number in one of the two styles often used * on checks. %dollars-and-hundredths formats cents as hundredths of @@ -154,7 +77,7 @@ public class UtilNumber { // ICU4J rule sets for the th_TH locale. To add more rules, expand this string. // For reference, see the RbnfSampleRuleSets.java file distributed with ICU4J - public static final String ruleSet_th_TH = + private static final String ruleSet_th_TH = /* * These rules format a number in one of the two styles often used * on checks. %bahts-and-hundredths formats stangs as hundredths of @@ -192,13 +115,92 @@ public class UtilNumber { + " 100: <00<;\n"; // hash map to store ICU4J rule sets keyed to Locale - public static HashMap<Locale, String> rbnfRuleSets; + private static HashMap<Locale, String> rbnfRuleSets; static { rbnfRuleSets = new HashMap<Locale, String>(); rbnfRuleSets.put(Locale.US, ruleSet_en_US); rbnfRuleSets.put(new Locale("th"), ruleSet_th_TH); } + private UtilNumber() {} + + /** + * Method to get BigDecimal scale factor from a property + * @param file - Name of the property file + * @param property - Name of the config property from arithmeticPropertiesFile (e.g., "invoice.decimals") + * @return int - Scale factor to pass to BigDecimal's methods. Defaults to DEFAULT_BD_SCALE (2) + */ + public static int getBigDecimalScale(String file, String property) { + if (UtilValidate.isEmpty(file)) return DEFAULT_BD_SCALE; + if (UtilValidate.isEmpty(property)) return DEFAULT_BD_SCALE; + + int scale = -1; + String value = UtilProperties.getPropertyValue(file, property); + if (value != null) { + try { + scale = Integer.parseInt(value); + } catch (NumberFormatException e) { + } + } + if (scale == -1) { + Debug.logWarning("Could not set decimal precision from " + property + "=" + value + ". Using default scale of " + DEFAULT_BD_SCALE + ".", module); + scale = DEFAULT_BD_SCALE; + } + return scale; + } + + /** + * As above, but use the default properties file + */ + public static int getBigDecimalScale(String property) { + return getBigDecimalScale(arithmeticPropertiesFile, property); + } + + /** + * Method to get BigDecimal rounding mode from a property + * @param file - Name of the property file + * @param property - Name of the config property from arithmeticPropertiesFile (e.g., "invoice.rounding") + * @return int - Rounding mode to pass to BigDecimal's methods. Defaults to DEFAULT_BD_ROUNDING_MODE (BigDecimal.ROUND_HALF_UP) + */ + public static int getBigDecimalRoundingMode(String file, String property) { + if (UtilValidate.isEmpty(file)) return DEFAULT_BD_SCALE; + if (UtilValidate.isEmpty(property)) return DEFAULT_BD_ROUNDING_MODE; + + String value = UtilProperties.getPropertyValue(file, property); + int mode = roundingModeFromString(value); + if (mode == -1) { + Debug.logWarning("Could not set decimal rounding mode from " + property + "=" + value + ". Using default mode of " + DEFAULT_BD_SCALE + ".", module); + return DEFAULT_BD_ROUNDING_MODE; + } + return mode; + } + + /** + * As above, but use the default properties file + */ + public static int getBigDecimalRoundingMode(String property) { + return getBigDecimalRoundingMode(arithmeticPropertiesFile, property); + } + + /** + * Method to get the BigDecimal rounding mode int value from a string name. + * @param value - The name of the mode (e.g., "ROUND_HALF_UP") + * @return int - The int value of the mode (e.g, BigDecimal.ROUND_HALF_UP) or -1 if the input was bad. + */ + public static int roundingModeFromString(String value) { + if (value == null) return -1; + value = value.trim(); + if ("ROUND_HALF_UP".equals(value)) return BigDecimal.ROUND_HALF_UP; + else if ("ROUND_HALF_DOWN".equals(value)) return BigDecimal.ROUND_HALF_DOWN; + else if ("ROUND_HALF_EVEN".equals(value)) return BigDecimal.ROUND_HALF_EVEN; + else if ("ROUND_UP".equals(value)) return BigDecimal.ROUND_UP; + else if ("ROUND_DOWN".equals(value)) return BigDecimal.ROUND_DOWN; + else if ("ROUND_CEILING".equals(value)) return BigDecimal.ROUND_CEILING; + else if ("ROUND_FLOOR".equals(value)) return BigDecimal.ROUND_FLOOR; + else if ("ROUND_UNNECCESSARY".equals(value)) return BigDecimal.ROUND_UNNECESSARY; + return -1; + } + /** * Method to format an amount using a custom rule set. * Current rule sets available: |
Free forum by Nabble | Edit this page |