📄 generictypevalidator.java
字号:
}
formatter.setParseIntegerOnly(true);
ParsePosition pos = new ParsePosition(0);
Number num = formatter.parse(value, pos);
// If there was no error and we used the whole string
if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
if (num.doubleValue() >= Long.MIN_VALUE &&
num.doubleValue() <= Long.MAX_VALUE) {
result = new Long(num.longValue());
}
}
}
return result;
}
/**
* Checks if the value can safely be converted to a float primitive.
*
*@param value The value validation is being performed on.
*@return the converted Float value.
*/
public static Float formatFloat(String value) {
if (value == null) {
return null;
}
try {
return new Float(value);
} catch (NumberFormatException e) {
return null;
}
}
/**
* Checks if the value can safely be converted to a float primitive.
*
*@param value The value validation is being performed on.
*@param locale The locale to use to parse the number (system default if
* null)
*@return the converted Float value.
*/
public static Float formatFloat(String value, Locale locale) {
Float result = null;
if (value != null) {
NumberFormat formatter = null;
if (locale != null) {
formatter = NumberFormat.getInstance(locale);
} else {
formatter = NumberFormat.getInstance(Locale.getDefault());
}
ParsePosition pos = new ParsePosition(0);
Number num = formatter.parse(value, pos);
// If there was no error and we used the whole string
if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
if (num.doubleValue() >= (Float.MAX_VALUE * -1) &&
num.doubleValue() <= Float.MAX_VALUE) {
result = new Float(num.floatValue());
}
}
}
return result;
}
/**
* Checks if the value can safely be converted to a double primitive.
*
*@param value The value validation is being performed on.
*@return the converted Double value.
*/
public static Double formatDouble(String value) {
if (value == null) {
return null;
}
try {
return new Double(value);
} catch (NumberFormatException e) {
return null;
}
}
/**
* Checks if the value can safely be converted to a double primitive.
*
*@param value The value validation is being performed on.
*@param locale The locale to use to parse the number (system default if
* null)
*@return the converted Double value.
*/
public static Double formatDouble(String value, Locale locale) {
Double result = null;
if (value != null) {
NumberFormat formatter = null;
if (locale != null) {
formatter = NumberFormat.getInstance(locale);
} else {
formatter = NumberFormat.getInstance(Locale.getDefault());
}
ParsePosition pos = new ParsePosition(0);
Number num = formatter.parse(value, pos);
// If there was no error and we used the whole string
if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length()) {
if (num.doubleValue() >= (Double.MAX_VALUE * -1) &&
num.doubleValue() <= Double.MAX_VALUE) {
result = new Double(num.doubleValue());
}
}
}
return result;
}
/**
* <p>
*
* Checks if the field is a valid date. The <code>Locale</code> is used
* with <code>java.text.DateFormat</code>. The setLenient method is set to
* <code>false</code> for all.</p>
*
*@param value The value validation is being performed on.
*@param locale The Locale to use to parse the date (system default if
* null)
*@return the converted Date value.
*/
public static Date formatDate(String value, Locale locale) {
Date date = null;
if (value == null) {
return null;
}
try {
DateFormat formatter = null;
if (locale != null) {
formatter =
DateFormat.getDateInstance(DateFormat.SHORT, locale);
} else {
formatter =
DateFormat.getDateInstance(
DateFormat.SHORT,
Locale.getDefault());
}
formatter.setLenient(false);
date = formatter.parse(value);
} catch (ParseException e) {
// Bad date so return null
Log log = LogFactory.getLog(GenericTypeValidator.class);
if (log.isDebugEnabled()) {
log.debug("Date parse failed value=[" + value + "], " +
"locale=[" + locale + "] " + e);
}
}
return date;
}
/**
* <p>
* Checks if the field is a valid date. The pattern is used with <code>java.text.SimpleDateFormat</code>
* . If strict is true, then the length will be checked so '2/12/1999' will
* not pass validation with the format 'MM/dd/yyyy' because the month isn't
* two digits. The setLenient method is set to <code>false</code> for all.
* </p>
*
*@param value The value validation is being performed on.
*@param datePattern The pattern passed to <code>SimpleDateFormat</code>.
*@param strict Whether or not to have an exact match of the
* datePattern.
*@return the converted Date value.
*/
public static Date formatDate(String value, String datePattern, boolean strict) {
Date date = null;
if (value == null
|| datePattern == null
|| datePattern.length() == 0) {
return null;
}
try {
SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
formatter.setLenient(false);
date = formatter.parse(value);
if (strict) {
if (datePattern.length() != value.length()) {
date = null;
}
}
} catch (ParseException e) {
// Bad date so return null
Log log = LogFactory.getLog(GenericTypeValidator.class);
if (log.isDebugEnabled()) {
log.debug("Date parse failed value=[" + value + "], " +
"pattern=[" + datePattern + "], " +
"strict=[" + strict + "] " + e);
}
}
return date;
}
/**
* <p>
* Checks if the field is a valid credit card number.</p> <p>
*
* Reference Sean M. Burke's <a href="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">
* script</a> .</p>
*
*@param value The value validation is being performed on.
*@return the converted Credit Card number.
*/
public static Long formatCreditCard(String value) {
return GenericValidator.isCreditCard(value) ? new Long(value) : null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -