📄 generictypevalidator.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.validator;
import java.io.Serializable;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This class contains basic methods for performing validations that return the
* correctly typed class based on the validation performed.
*
* @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
*/
public class GenericTypeValidator implements Serializable {
/**
* Checks if the value can safely be converted to a byte primitive.
*
*@param value The value validation is being performed on.
*@return the converted Byte value.
*/
public static Byte formatByte(String value) {
if (value == null) {
return null;
}
try {
return new Byte(value);
} catch (NumberFormatException e) {
return null;
}
}
/**
* Checks if the value can safely be converted to a byte 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 Byte value.
*/
public static Byte formatByte(String value, Locale locale) {
Byte result = null;
if (value != null) {
NumberFormat formatter = null;
if (locale != null) {
formatter = NumberFormat.getNumberInstance(locale);
} else {
formatter = NumberFormat.getNumberInstance(Locale.getDefault());
}
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() >= Byte.MIN_VALUE &&
num.doubleValue() <= Byte.MAX_VALUE) {
result = new Byte(num.byteValue());
}
}
}
return result;
}
/**
* Checks if the value can safely be converted to a short primitive.
*
*@param value The value validation is being performed on.
*@return the converted Short value.
*/
public static Short formatShort(String value) {
if (value == null) {
return null;
}
try {
return new Short(value);
} catch (NumberFormatException e) {
return null;
}
}
/**
* Checks if the value can safely be converted to a short primitive.
*
*@param value The value validation is being performed on.
*@param locale The locale to use to parse the number (system default if
* null)vv
*@return the converted Short value.
*/
public static Short formatShort(String value, Locale locale) {
Short result = null;
if (value != null) {
NumberFormat formatter = null;
if (locale != null) {
formatter = NumberFormat.getNumberInstance(locale);
} else {
formatter = NumberFormat.getNumberInstance(Locale.getDefault());
}
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() >= Short.MIN_VALUE &&
num.doubleValue() <= Short.MAX_VALUE) {
result = new Short(num.shortValue());
}
}
}
return result;
}
/**
* Checks if the value can safely be converted to a int primitive.
*
*@param value The value validation is being performed on.
*@return the converted Integer value.
*/
public static Integer formatInt(String value) {
if (value == null) {
return null;
}
try {
return new Integer(value);
} catch (NumberFormatException e) {
return null;
}
}
/**
* Checks if the value can safely be converted to an int 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 Integer value.
*/
public static Integer formatInt(String value, Locale locale) {
Integer result = null;
if (value != null) {
NumberFormat formatter = null;
if (locale != null) {
formatter = NumberFormat.getNumberInstance(locale);
} else {
formatter = NumberFormat.getNumberInstance(Locale.getDefault());
}
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() >= Integer.MIN_VALUE &&
num.doubleValue() <= Integer.MAX_VALUE) {
result = new Integer(num.intValue());
}
}
}
return result;
}
/**
* Checks if the value can safely be converted to a long primitive.
*
*@param value The value validation is being performed on.
*@return the converted Long value.
*/
public static Long formatLong(String value) {
if (value == null) {
return null;
}
try {
return new Long(value);
} catch (NumberFormatException e) {
return null;
}
}
/**
* Checks if the value can safely be converted to a long 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 Long value.
*/
public static Long formatLong(String value, Locale locale) {
Long result = null;
if (value != null) {
NumberFormat formatter = null;
if (locale != null) {
formatter = NumberFormat.getNumberInstance(locale);
} else {
formatter = NumberFormat.getNumberInstance(Locale.getDefault());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -