PrecisionValidator

public class PrecisionValidator extends BaseValidator {
    
    private PrecisionValidator() {
        
    }
    
    public static final int ORACLE_NUMBER_PRECISION_RANGE_MIN = 1;
    public static final int ORACLE_NUMBER_PRECISION_RANGE_MAX = 38;
    public static final int ORACLE_NUMBER_SCALE_RANGE_MIN = -84;
    public static final int ORACLE_NUMBER_SCALE_RANGE_MAX = 127;
    
    
    /**
     * Validation Rule: A number can be stored in an oracle NUMBER column by specified precision and scale
     * @param precision          The total number of digits
     * @param scale              The number of digits to the right of the decimal point
     * @param inputText          Input string text
     * @return boolean           Return true when input string is valid number can be stored in an oracle NUMBER column 
     *                           by specified precision and scale, otherwise return false.                        
     */
    public static boolean validateNumberPrecision(int precision, int scale, String inputText) {
        boolean isValid = false;
        
        if (Util.isEmpty(inputText)) {
            isValid = true;
        }
        
        if (!Util.isEmpty(inputText) && NumericValidator.isNumeric(inputText)) {

            if (precision > PrecisionValidator.ORACLE_NUMBER_PRECISION_RANGE_MAX) {
                precision = PrecisionValidator.ORACLE_NUMBER_PRECISION_RANGE_MAX;
            }
            
            if (precision < PrecisionValidator.ORACLE_NUMBER_PRECISION_RANGE_MIN) {
                precision = PrecisionValidator.ORACLE_NUMBER_PRECISION_RANGE_MIN;
            }
            
            if (scale > PrecisionValidator.ORACLE_NUMBER_SCALE_RANGE_MAX) {
                scale = PrecisionValidator.ORACLE_NUMBER_SCALE_RANGE_MAX;
            }
            
            if (scale < PrecisionValidator.ORACLE_NUMBER_SCALE_RANGE_MIN) {
                scale = PrecisionValidator.ORACLE_NUMBER_SCALE_RANGE_MIN;
            }
            
            int lastDecimalIndex = inputText.lastIndexOf('.');
            int currentPrecision = 0;
            int currentScale = 0;
            boolean isValidPrecision = false;
            boolean isValidScale = false;
            
            if (lastDecimalIndex > 0) {
                currentPrecision = lastDecimalIndex;
                currentScale = inputText.length() - lastDecimalIndex - 1;
            } else {
                currentPrecision = inputText.length();
                currentScale = 0;
            }
            
            if (currentPrecision <= precision) {
                isValidPrecision = true;
            }
            
            if (scale >= 0 && currentScale <= scale) {
                isValidScale = true;
            }
            
            if (scale < 0) {
                isValidScale = true;
            }
            
            if (isValidPrecision && isValidScale) {
                isValid = true;
            }
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number can be stored in an oracle NUMBER column by specified precision and scale
     * @param precision          The total number of digits
     * @param scale              The number of digits to the right of the decimal point
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when input string is valid number can be stored in an oracle NUMBER column 
     *                           by specified precision and scale, otherwise return false.                        
     */
    public static boolean validateNumberPrecision(int precision, int scale, String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = PrecisionValidator.validateNumberPrecision(precision, scale, inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number can be stored in an oracle NUMBER column by specified precision and scale
     * @param precision          The total number of digits
     * @param scale              The number of digits to the right of the decimal point
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when input string is valid number can be stored in an oracle NUMBER column 
     *                           by specified precision and scale, otherwise return false.                        
     */
    public static boolean validateNumberPrecision(int precision, int scale, String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = PrecisionValidator.validateNumberPrecision(precision, scale, inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    
    /**
     * Validation Rule: A number is integer and the value is between -2147483648 and 2147483647
     * @param inputText          Input string text
     * @return boolean           Return true when input string is an integer and the value is between -2147483648 and 2147483647,
     *                           otherwise return false.                        
     */
    public static boolean validateIntegerPrecision(String inputText) {
        boolean isValid = false;
        
        if (Util.isEmpty(inputText)) {
            isValid = true;
        }
        
        if (!Util.isEmpty(inputText) && IntegerValidator.isInteger(inputText)) {
            try {
                Integer.parseInt(inputText);
                isValid = true;
            } catch (Exception e) {
                isValid = false;
            }
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is integer and the value is between -2147483648 and 2147483647
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when input string is an integer and the value is between -2147483648 and 2147483647,
     *                           otherwise return false.                        
     */
    public static boolean validateIntegerPrecision(String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = PrecisionValidator.validateIntegerPrecision(inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is integer and the value is between -2147483648 and 2147483647
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when input string is an integer and the value is between -2147483648 and 2147483647,
     *                           otherwise return false.                        
     */
    public static boolean validateIntegerPrecision(String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = PrecisionValidator.validateIntegerPrecision(inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    
    /**
     * Validation Rule: A number is type long and the value is between -9223372036854775808L and 9223372036854775807L
     * @param inputText          Input string text
     * @return boolean           Return true when input string is type long number and the value is between -9223372036854775808L and 9223372036854775807L,
     *                           otherwise return false.                        
     */
    public static boolean validateLongPrecision(String inputText) {
        boolean isValid = false;
        
        if (Util.isEmpty(inputText)) {
            isValid = true;
        }
        
        if (!Util.isEmpty(inputText) && IntegerValidator.isInteger(inputText)) {
            try {
                Long.parseLong(inputText);
                isValid = true;
            } catch (Exception e) {
                isValid = false;
            }
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is type long and the value is between -9223372036854775808L and 9223372036854775807L
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when input string is type long number and the value is between -9223372036854775808L and 9223372036854775807L,
     *                           otherwise return false.                        
     */
    public static boolean validateLongPrecision(String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = PrecisionValidator.validateLongPrecision(inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is type long and the value is between -9223372036854775808L and 9223372036854775807L
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when input string is type long number and the value is between -9223372036854775808L and 9223372036854775807L,
     *                           otherwise return false.                        
     */
    public static boolean validateLongPrecision(String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = PrecisionValidator.validateLongPrecision(inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    
    /**
     * Validation Rule: A number is type float and the value is between 1.401298464324817E-45f and 3.4028234663852886E38f
     * @param inputText          Input string text
     * @return boolean           Return true when input string is type float number and the value is between 1.401298464324817E-45f and 3.4028234663852886E38f,
     *                           otherwise return false.                        
     */
    public static boolean validateFloatPrecision(String inputText) {
        boolean isValid = false;
        
        if (Util.isEmpty(inputText)) {
            isValid = true;
        }
        
        if (!Util.isEmpty(inputText) && NumericValidator.isNumeric(inputText)) {
            try {
                Float.parseFloat(inputText);
                isValid = true;
            } catch (Exception e) {
                isValid = false;
            }
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is type float and the value is between 1.401298464324817E-45f and 3.4028234663852886E38f
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when input string is type float number and the value is between 1.401298464324817E-45f and 3.4028234663852886E38f,
     *                           otherwise return false.                        
     */
    public static boolean validateFloatPrecision(String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = PrecisionValidator.validateFloatPrecision(inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is type float and the value is between 1.401298464324817E-45f and 3.4028234663852886E38f
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when input string is type float number and the value is between 1.401298464324817E-45f and 3.4028234663852886E38f,
     *                           otherwise return false.                        
     */
    public static boolean validateFloatPrecision(String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = PrecisionValidator.validateFloatPrecision(inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    
    /**
     * Validation Rule: A number is type double and the value is between 4.9E-324 and 1.7976931348623157E308
     * @param inputText          Input string text
     * @return boolean           Return true when input string is type double number and the value is between 4.9E-324 and 1.7976931348623157E308,
     *                           otherwise return false.                        
     */
    public static boolean validateDoublePrecision(String inputText) {
        boolean isValid = false;
        
        if (Util.isEmpty(inputText)) {
            isValid = true;
        }
        
        if (!Util.isEmpty(inputText) && NumericValidator.isNumeric(inputText)) {
            try {
                Double.parseDouble(inputText);
                isValid = true;
            } catch (Exception e) {
                isValid = false;
            }
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is type double and the value is between 4.9E-324 and 1.7976931348623157E308
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param fieldErrorMessage  Field error message
     * @return boolean           Return true when input string is type double number and the value is between 4.9E-324 and 1.7976931348623157E308,
     *                           otherwise return false.                        
     */
    public static boolean validateDoublePrecision(String inputText, String fieldErrorParam, String fieldErrorMessage) {
        boolean isValid = PrecisionValidator.validateDoublePrecision(inputText);
        
        if (!isValid) {
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }
    
    /**
     * Validation Rule: A number is type double and the value is between 4.9E-324 and 1.7976931348623157E308
     * @param inputText          Input string text
     * @param fieldErrorParam    Field error tag parameter
     * @param bundle             Language property file id
     * @param key                Message key
     * @param defaultMessage     Default message
     * @param arguments          The arguments of message
     * @return boolean           Return true when input string is type double number and the value is between 4.9E-324 and 1.7976931348623157E308,
     *                           otherwise return false.                        
     */
    public static boolean validateDoublePrecision(String inputText, String fieldErrorParam, 
            String bundle, String key, String defaultMessage, String[] arguments) {
        boolean isValid = PrecisionValidator.validateDoublePrecision(inputText);
        
        if (!isValid) {
            String fieldErrorMessage = MessageUtil.getText(bundle, key, defaultMessage, arguments);
            addFieldErrorMessage(fieldErrorParam, fieldErrorMessage);
        }
        
        return isValid;
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值