📄 coercions.java
字号:
//-------------------------------------
/**
*
* Coerces a value to the given primitive number class
**/
public static Number coerceToPrimitiveNumber (Object pValue,
Class pClass,
Logger pLogger)
throws ELException
{
if (pValue == null ||
"".equals (pValue)) {
return coerceToPrimitiveNumber (ZERO, pClass);
}
else if (pValue instanceof Character) {
char val = ((Character) pValue).charValue ();
return coerceToPrimitiveNumber (new Short((short) val), pClass);
}
else if (pValue instanceof Boolean) {
if (pLogger.isLoggingError ()) {
pLogger.logError (Constants.BOOLEAN_TO_NUMBER,
pValue,
pClass.getName ());
}
return coerceToPrimitiveNumber (ZERO, pClass);
}
else if (pValue.getClass () == pClass) {
return (Number) pValue;
}
else if (pValue instanceof Number) {
return coerceToPrimitiveNumber ((Number) pValue, pClass);
}
else if (pValue instanceof String) {
try {
return coerceToPrimitiveNumber ((String) pValue, pClass);
}
catch (Exception exc) {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.STRING_TO_NUMBER_EXCEPTION,
(String) pValue,
pClass.getName ());
}
return coerceToPrimitiveNumber (ZERO, pClass);
}
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.COERCE_TO_NUMBER,
pValue.getClass ().getName (),
pClass.getName ());
}
return coerceToPrimitiveNumber (0, pClass);
}
}
//-------------------------------------
/**
*
* Coerces a value to an Integer, returning null if the coercion
* isn't possible.
**/
public static Integer coerceToInteger (Object pValue,
Logger pLogger)
throws ELException
{
if (pValue == null) {
return null;
}
else if (pValue instanceof Character) {
return PrimitiveObjects.getInteger
((int) (((Character) pValue).charValue ()));
}
else if (pValue instanceof Boolean) {
if (pLogger.isLoggingWarning ()) {
pLogger.logWarning (Constants.BOOLEAN_TO_NUMBER,
pValue,
Integer.class.getName ());
}
return PrimitiveObjects.getInteger
(((Boolean) pValue).booleanValue () ? 1 : 0);
}
else if (pValue instanceof Integer) {
return (Integer) pValue;
}
else if (pValue instanceof Number) {
return PrimitiveObjects.getInteger (((Number) pValue).intValue ());
}
else if (pValue instanceof String) {
try {
return Integer.valueOf ((String) pValue);
}
catch (Exception exc) {
if (pLogger.isLoggingWarning ()) {
pLogger.logWarning
(Constants.STRING_TO_NUMBER_EXCEPTION,
(String) pValue,
Integer.class.getName ());
}
return null;
}
}
else {
if (pLogger.isLoggingWarning ()) {
pLogger.logWarning
(Constants.COERCE_TO_NUMBER,
pValue.getClass ().getName (),
Integer.class.getName ());
}
return null;
}
}
//-------------------------------------
/**
*
* Coerces a long to the given primitive number class
**/
static Number coerceToPrimitiveNumber (long pValue,
Class pClass)
throws ELException
{
if (pClass == Byte.class || pClass == Byte.TYPE) {
return PrimitiveObjects.getByte ((byte) pValue);
}
else if (pClass == Short.class || pClass == Short.TYPE) {
return PrimitiveObjects.getShort ((short) pValue);
}
else if (pClass == Integer.class || pClass == Integer.TYPE) {
return PrimitiveObjects.getInteger ((int) pValue);
}
else if (pClass == Long.class || pClass == Long.TYPE) {
return PrimitiveObjects.getLong (pValue);
}
else if (pClass == Float.class || pClass == Float.TYPE) {
return PrimitiveObjects.getFloat ((float) pValue);
}
else if (pClass == Double.class || pClass == Double.TYPE) {
return PrimitiveObjects.getDouble ((double) pValue);
}
else {
return PrimitiveObjects.getInteger (0);
}
}
//-------------------------------------
/**
*
* Coerces a double to the given primitive number class
**/
static Number coerceToPrimitiveNumber (double pValue,
Class pClass)
throws ELException
{
if (pClass == Byte.class || pClass == Byte.TYPE) {
return PrimitiveObjects.getByte ((byte) pValue);
}
else if (pClass == Short.class || pClass == Short.TYPE) {
return PrimitiveObjects.getShort ((short) pValue);
}
else if (pClass == Integer.class || pClass == Integer.TYPE) {
return PrimitiveObjects.getInteger ((int) pValue);
}
else if (pClass == Long.class || pClass == Long.TYPE) {
return PrimitiveObjects.getLong ((long) pValue);
}
else if (pClass == Float.class || pClass == Float.TYPE) {
return PrimitiveObjects.getFloat ((float) pValue);
}
else if (pClass == Double.class || pClass == Double.TYPE) {
return PrimitiveObjects.getDouble (pValue);
}
else {
return PrimitiveObjects.getInteger (0);
}
}
//-------------------------------------
/**
*
* Coerces a Number to the given primitive number class
**/
static Number coerceToPrimitiveNumber (Number pValue,
Class pClass)
throws ELException
{
if (pClass == Byte.class || pClass == Byte.TYPE) {
return PrimitiveObjects.getByte (pValue.byteValue ());
}
else if (pClass == Short.class || pClass == Short.TYPE) {
return PrimitiveObjects.getShort (pValue.shortValue ());
}
else if (pClass == Integer.class || pClass == Integer.TYPE) {
return PrimitiveObjects.getInteger (pValue.intValue ());
}
else if (pClass == Long.class || pClass == Long.TYPE) {
return PrimitiveObjects.getLong (pValue.longValue ());
}
else if (pClass == Float.class || pClass == Float.TYPE) {
return PrimitiveObjects.getFloat (pValue.floatValue ());
}
else if (pClass == Double.class || pClass == Double.TYPE) {
return PrimitiveObjects.getDouble (pValue.doubleValue ());
}
else if (pClass == BigInteger.class) {
if (pValue instanceof BigDecimal)
return ((BigDecimal) pValue).toBigInteger();
else
return BigInteger.valueOf(pValue.longValue());
}
else if (pClass == BigDecimal.class) {
if (pValue instanceof BigInteger)
return new BigDecimal((BigInteger) pValue);
else
return new BigDecimal(pValue.doubleValue());
}
else {
return PrimitiveObjects.getInteger (0);
}
}
//-------------------------------------
/**
*
* Coerces a String to the given primitive number class
**/
static Number coerceToPrimitiveNumber (String pValue,
Class pClass)
throws ELException
{
if (pClass == Byte.class || pClass == Byte.TYPE) {
return Byte.valueOf (pValue);
}
else if (pClass == Short.class || pClass == Short.TYPE) {
return Short.valueOf (pValue);
}
else if (pClass == Integer.class || pClass == Integer.TYPE) {
return Integer.valueOf (pValue);
}
else if (pClass == Long.class || pClass == Long.TYPE) {
return Long.valueOf (pValue);
}
else if (pClass == Float.class || pClass == Float.TYPE) {
return Float.valueOf (pValue);
}
else if (pClass == Double.class || pClass == Double.TYPE) {
return Double.valueOf (pValue);
}
else if (pClass == BigInteger.class) {
return new BigInteger(pValue);
}
else if (pClass == BigDecimal.class) {
return new BigDecimal(pValue);
}
else {
return PrimitiveObjects.getInteger (0);
}
}
//-------------------------------------
/**
*
* Coerces a value to a Character
**/
public static Character coerceToCharacter (Object pValue,
Logger pLogger)
throws ELException
{
if (pValue == null ||
"".equals (pValue)) {
return PrimitiveObjects.getCharacter ((char) 0);
}
else if (pValue instanceof Character) {
return (Character) pValue;
}
else if (pValue instanceof Boolean) {
if (pLogger.isLoggingError ()) {
pLogger.logError (Constants.BOOLEAN_TO_CHARACTER, pValue);
}
return PrimitiveObjects.getCharacter ((char) 0);
}
else if (pValue instanceof Number) {
return PrimitiveObjects.getCharacter
((char) ((Number) pValue).shortValue ());
}
else if (pValue instanceof String) {
String str = (String) pValue;
return PrimitiveObjects.getCharacter (str.charAt (0));
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.COERCE_TO_CHARACTER,
pValue.getClass ().getName ());
}
return PrimitiveObjects.getCharacter ((char) 0);
}
}
//-------------------------------------
/**
*
* Coerces a value to a Boolean
**/
public static Boolean coerceToBoolean (Object pValue,
Logger pLogger)
throws ELException
{
if (pValue == null ||
"".equals (pValue)) {
return Boolean.FALSE;
}
else if (pValue instanceof Boolean) {
return (Boolean) pValue;
}
else if (pValue instanceof String) {
String str = (String) pValue;
try {
return Boolean.valueOf (str);
}
catch (Exception exc) {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.STRING_TO_BOOLEAN,
exc,
(String) pValue);
}
return Boolean.FALSE;
}
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.COERCE_TO_BOOLEAN,
pValue.getClass ().getName ());
}
return Boolean.TRUE;
}
}
//-------------------------------------
/**
*
* Coerces a value to the specified Class that is not covered by any
* of the above cases
**/
public static Object coerceToObject (Object pValue,
Class pClass,
Logger pLogger)
throws ELException
{
if (pValue == null) {
return null;
}
else if (pClass.isAssignableFrom (pValue.getClass ())) {
return pValue;
}
else if (pValue instanceof String) {
String str = (String) pValue;
PropertyEditor pe = PropertyEditorManager.findEditor (pClass);
if (pe == null) {
if ("".equals (str)) {
return null;
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.NO_PROPERTY_EDITOR,
str,
pClass.getName ());
}
return null;
}
}
try {
pe.setAsText (str);
return pe.getValue ();
}
catch (IllegalArgumentException exc) {
if ("".equals (str)) {
return null;
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.PROPERTY_EDITOR_ERROR,
exc,
pValue,
pClass.getName ());
}
return null;
}
}
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.COERCE_TO_OBJECT,
pValue.getClass ().getName (),
pClass.getName ());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -