📄 coercions.java
字号:
return null;
}
}
//-------------------------------------
// Applying operators
//-------------------------------------
/**
*
* Performs all of the necessary type conversions, then calls on the
* appropriate operator.
**/
public static Object applyArithmeticOperator
(Object pLeft,
Object pRight,
ArithmeticOperator pOperator,
Logger pLogger)
throws ELException
{
if (pLeft == null &&
pRight == null) {
if (pLogger.isLoggingWarning ()) {
pLogger.logWarning
(Constants.ARITH_OP_NULL,
pOperator.getOperatorSymbol ());
}
return PrimitiveObjects.getInteger (0);
}
else if (isBigDecimal(pLeft) || isBigDecimal(pRight)) {
BigDecimal left = (BigDecimal)
coerceToPrimitiveNumber(pLeft, BigDecimal.class, pLogger);
BigDecimal right = (BigDecimal)
coerceToPrimitiveNumber(pRight, BigDecimal.class, pLogger);
return pOperator.apply(left, right);
}
else if (isFloatingPointType(pLeft) ||
isFloatingPointType(pRight) ||
isFloatingPointString(pLeft) ||
isFloatingPointString(pRight)) {
if (isBigInteger(pLeft) || isBigInteger(pRight)) {
BigDecimal left = (BigDecimal)
coerceToPrimitiveNumber(pLeft, BigDecimal.class, pLogger);
BigDecimal right = (BigDecimal)
coerceToPrimitiveNumber(pRight, BigDecimal.class, pLogger);
return pOperator.apply(left, right);
} else {
double left =
coerceToPrimitiveNumber(pLeft, Double.class, pLogger).
doubleValue();
double right =
coerceToPrimitiveNumber(pRight, Double.class, pLogger).
doubleValue();
return
PrimitiveObjects.getDouble(pOperator.apply(left, right));
}
}
else if (isBigInteger(pLeft) || isBigInteger(pRight)) {
BigInteger left = (BigInteger)
coerceToPrimitiveNumber(pLeft, BigInteger.class, pLogger);
BigInteger right = (BigInteger)
coerceToPrimitiveNumber(pRight, BigInteger.class, pLogger);
return pOperator.apply(left, right);
}
else {
long left =
coerceToPrimitiveNumber (pLeft, Long.class, pLogger).
longValue ();
long right =
coerceToPrimitiveNumber (pRight, Long.class, pLogger).
longValue ();
return
PrimitiveObjects.getLong (pOperator.apply (left, right));
}
}
//-------------------------------------
/**
*
* Performs all of the necessary type conversions, then calls on the
* appropriate operator.
**/
public static Object applyRelationalOperator
(Object pLeft,
Object pRight,
RelationalOperator pOperator,
Logger pLogger)
throws ELException
{
if (isBigDecimal(pLeft) || isBigDecimal(pRight)) {
BigDecimal left = (BigDecimal)
coerceToPrimitiveNumber(pLeft, BigDecimal.class, pLogger);
BigDecimal right = (BigDecimal)
coerceToPrimitiveNumber(pRight, BigDecimal.class, pLogger);
return PrimitiveObjects.getBoolean(pOperator.apply(left, right));
}
else if (isFloatingPointType (pLeft) ||
isFloatingPointType (pRight)) {
double left =
coerceToPrimitiveNumber (pLeft, Double.class, pLogger).
doubleValue ();
double right =
coerceToPrimitiveNumber (pRight, Double.class, pLogger).
doubleValue ();
return
PrimitiveObjects.getBoolean (pOperator.apply (left, right));
}
else if (isBigInteger(pLeft) || isBigInteger(pRight)) {
BigInteger left = (BigInteger)
coerceToPrimitiveNumber(pLeft, BigInteger.class, pLogger);
BigInteger right = (BigInteger)
coerceToPrimitiveNumber(pRight, BigInteger.class, pLogger);
return PrimitiveObjects.getBoolean(pOperator.apply(left, right));
}
else if (isIntegerType (pLeft) ||
isIntegerType (pRight)) {
long left =
coerceToPrimitiveNumber (pLeft, Long.class, pLogger).
longValue ();
long right =
coerceToPrimitiveNumber (pRight, Long.class, pLogger).
longValue ();
return
PrimitiveObjects.getBoolean (pOperator.apply (left, right));
}
else if (pLeft instanceof String ||
pRight instanceof String) {
String left = coerceToString (pLeft, pLogger);
String right = coerceToString (pRight, pLogger);
return
PrimitiveObjects.getBoolean (pOperator.apply (left, right));
}
else if (pLeft instanceof Comparable) {
try {
int result = ((Comparable) pLeft).compareTo (pRight);
return
PrimitiveObjects.getBoolean
(pOperator.apply (result, -result));
}
catch (Exception exc) {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.COMPARABLE_ERROR,
exc,
pLeft.getClass ().getName (),
(pRight == null) ? "null" : pRight.getClass ().getName (),
pOperator.getOperatorSymbol ());
}
return Boolean.FALSE;
}
}
else if (pRight instanceof Comparable) {
try {
int result = ((Comparable) pRight).compareTo (pLeft);
return
PrimitiveObjects.getBoolean
(pOperator.apply (-result, result));
}
catch (Exception exc) {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.COMPARABLE_ERROR,
exc,
pRight.getClass ().getName (),
(pLeft == null) ? "null" : pLeft.getClass ().getName (),
pOperator.getOperatorSymbol ());
}
return Boolean.FALSE;
}
}
else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.ARITH_OP_BAD_TYPE,
pOperator.getOperatorSymbol (),
pLeft.getClass ().getName (),
pRight.getClass ().getName ());
}
return Boolean.FALSE;
}
}
//-------------------------------------
/**
*
* Performs all of the necessary type conversions, then calls on the
* appropriate operator.
**/
public static Object applyEqualityOperator
(Object pLeft,
Object pRight,
EqualityOperator pOperator,
Logger pLogger)
throws ELException
{
if (pLeft == pRight) {
return PrimitiveObjects.getBoolean (pOperator.apply (true, pLogger));
}
else if (pLeft == null ||
pRight == null) {
return PrimitiveObjects.getBoolean (pOperator.apply (false, pLogger));
}
else if (isBigDecimal(pLeft) || isBigDecimal(pRight)) {
BigDecimal left = (BigDecimal)
coerceToPrimitiveNumber(pLeft, BigDecimal.class, pLogger);
BigDecimal right = (BigDecimal)
coerceToPrimitiveNumber(pRight, BigDecimal.class, pLogger);
return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right), pLogger));
}
else if (isFloatingPointType (pLeft) ||
isFloatingPointType (pRight)) {
double left =
coerceToPrimitiveNumber (pLeft, Double.class, pLogger).
doubleValue ();
double right =
coerceToPrimitiveNumber (pRight, Double.class, pLogger).
doubleValue ();
return
PrimitiveObjects.getBoolean
(pOperator.apply (left == right, pLogger));
}
else if (isBigInteger(pLeft) || isBigInteger(pRight)) {
BigInteger left = (BigInteger)
coerceToPrimitiveNumber(pLeft, BigInteger.class, pLogger);
BigInteger right = (BigInteger)
coerceToPrimitiveNumber(pRight, BigInteger.class, pLogger);
return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right), pLogger));
}
else if (isIntegerType (pLeft) ||
isIntegerType (pRight)) {
long left =
coerceToPrimitiveNumber (pLeft, Long.class, pLogger).
longValue ();
long right =
coerceToPrimitiveNumber (pRight, Long.class, pLogger).
longValue ();
return
PrimitiveObjects.getBoolean
(pOperator.apply (left == right, pLogger));
}
else if (pLeft instanceof Boolean ||
pRight instanceof Boolean) {
boolean left = coerceToBoolean (pLeft, pLogger).booleanValue ();
boolean right = coerceToBoolean (pRight, pLogger).booleanValue ();
return
PrimitiveObjects.getBoolean
(pOperator.apply (left == right, pLogger));
}
else if (pLeft instanceof String ||
pRight instanceof String) {
String left = coerceToString (pLeft, pLogger);
String right = coerceToString (pRight, pLogger);
return
PrimitiveObjects.getBoolean
(pOperator.apply (left.equals (right), pLogger));
}
else {
try {
return
PrimitiveObjects.getBoolean
(pOperator.apply (pLeft.equals (pRight), pLogger));
}
catch (Exception exc) {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.ERROR_IN_EQUALS,
exc,
pLeft.getClass ().getName (),
pRight.getClass ().getName (),
pOperator.getOperatorSymbol ());
}
return Boolean.FALSE;
}
}
}
//-------------------------------------
/**
*
* Returns true if the given Object is of a floating point type
**/
public static boolean isFloatingPointType (Object pObject)
{
return
pObject != null &&
isFloatingPointType (pObject.getClass ());
}
//-------------------------------------
/**
*
* Returns true if the given class is of a floating point type
**/
public static boolean isFloatingPointType (Class pClass)
{
return
pClass == Float.class ||
pClass == Float.TYPE ||
pClass == Double.class ||
pClass == Double.TYPE;
}
//-------------------------------------
/**
*
* Returns true if the given string might contain a floating point
* number - i.e., it contains ".", "e", or "E"
**/
public static boolean isFloatingPointString (Object pObject)
{
if (pObject instanceof String) {
String str = (String) pObject;
int len = str.length ();
for (int i = 0; i < len; i++) {
char ch = str.charAt (i);
if (ch == '.' ||
ch == 'e' ||
ch == 'E') {
return true;
}
}
return false;
}
else {
return false;
}
}
//-------------------------------------
/**
*
* Returns true if the given Object is of an integer type
**/
public static boolean isIntegerType (Object pObject)
{
return
pObject != null &&
isIntegerType (pObject.getClass ());
}
//-------------------------------------
/**
*
* Returns true if the given class is of an integer type
**/
public static boolean isIntegerType (Class pClass)
{
return
pClass == Byte.class ||
pClass == Byte.TYPE ||
pClass == Short.class ||
pClass == Short.TYPE ||
pClass == Character.class ||
pClass == Character.TYPE ||
pClass == Integer.class ||
pClass == Integer.TYPE ||
pClass == Long.class ||
pClass == Long.TYPE;
}
//-------------------------------------
/**
* Returns true if the given object is BigInteger.
* @param pObject - Object to evaluate
* @return - true if the given object is BigInteger
*/
public static boolean isBigInteger(Object pObject) {
return
pObject != null && pObject instanceof BigInteger;
}
/**
* Returns true if the given object is BigDecimal.
* @param pObject - Object to evaluate
* @return - true if the given object is BigDecimal
*/
public static boolean isBigDecimal(Object pObject) {
return
pObject != null && pObject instanceof BigDecimal;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -