📄 ognlops.java
字号:
{
Object result = null;
if (value != null) {
/* If array -> array then convert components of array individually */
if ( value.getClass().isArray() && toType.isArray() ) {
Class componentType = toType.getComponentType();
result = Array.newInstance(componentType, Array.getLength(value));
for (int i = 0, icount = Array.getLength(value); i < icount; i++) {
Array.set(result, i, convertValue(Array.get(value, i), componentType));
}
} else {
if ( ( toType == Integer.class ) || ( toType == Integer.TYPE ) ) result = new Integer((int)longValue(value));
if ( ( toType == Double.class ) || ( toType == Double.TYPE ) ) result = new Double(doubleValue(value));
if ( ( toType == Boolean.class ) || ( toType == Boolean.TYPE ) ) result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE;
if ( ( toType == Byte.class ) || ( toType == Byte.TYPE ) ) result = new Byte((byte)longValue(value));
if ( ( toType == Character.class ) || ( toType == Character.TYPE ) ) result = new Character((char)longValue(value));
if ( ( toType == Short.class ) || ( toType == Short.TYPE ) ) result = new Short((short)longValue(value));
if ( ( toType == Long.class ) || ( toType == Long.TYPE ) ) result = new Long(longValue(value));
if ( ( toType == Float.class ) || ( toType == Float.TYPE ) ) result = new Float(doubleValue(value));
if ( toType == BigInteger.class ) result = bigIntValue(value);
if ( toType == BigDecimal.class ) result = bigDecValue(value);
if ( toType == String.class ) result = stringValue(value);
}
} else {
if (toType.isPrimitive()) {
result = OgnlRuntime.getPrimitiveDefaultValue(toType);
}
}
return result;
}
/**
* Returns the constant from the NumericTypes interface that best expresses the type
* of a numeric operation on the two given objects.
*
* @param v1 one argument to a numeric operator
* @param v2 the other argument
* @return the appropriate constant from the NumericTypes interface
*/
public static int getNumericType( Object v1, Object v2 )
{
return getNumericType( v1, v2, false );
}
/**
* Returns the constant from the NumericTypes interface that best expresses the type
* of an operation, which can be either numeric or not, on the two given types.
*
* @param t1 type of one argument to an operator
* @param t2 type of the other argument
* @param canBeNonNumeric whether the operator can be interpreted as non-numeric
* @return the appropriate constant from the NumericTypes interface
*/
public static int getNumericType( int t1, int t2, boolean canBeNonNumeric )
{
if ( t1 == t2 )
return t1;
if ( canBeNonNumeric && (t1 == NONNUMERIC || t2 == NONNUMERIC || t1 == CHAR || t2 == CHAR) )
return NONNUMERIC;
if ( t1 == NONNUMERIC ) t1 = DOUBLE; // Try to interpret strings as doubles...
if ( t2 == NONNUMERIC ) t2 = DOUBLE; // Try to interpret strings as doubles...
if ( t1 >= MIN_REAL_TYPE )
{
if ( t2 >= MIN_REAL_TYPE )
return Math.max(t1,t2);
if ( t2 < INT )
return t1;
if ( t2 == BIGINT )
return BIGDEC;
return Math.max(DOUBLE,t1);
}
else if ( t2 >= MIN_REAL_TYPE )
{
if ( t1 < INT )
return t2;
if ( t1 == BIGINT )
return BIGDEC;
return Math.max(DOUBLE,t2);
}
else
return Math.max(t1,t2);
}
/**
* Returns the constant from the NumericTypes interface that best expresses the type
* of an operation, which can be either numeric or not, on the two given objects.
*
* @param v1 one argument to an operator
* @param v2 the other argument
* @param canBeNonNumeric whether the operator can be interpreted as non-numeric
* @return the appropriate constant from the NumericTypes interface
*/
public static int getNumericType( Object v1, Object v2, boolean canBeNonNumeric )
{
return getNumericType(getNumericType(v1), getNumericType(v2), canBeNonNumeric);
}
/**
* Returns a new Number object of an appropriate type to hold the given integer
* value. The type of the returned object is consistent with the given type
* argument, which is a constant from the NumericTypes interface.
*
* @param type the nominal numeric type of the result, a constant from the NumericTypes interface
* @param value the integer value to convert to a Number object
* @return a Number object with the given value, of type implied by the type argument
*/
public static Number newInteger( int type, long value )
{
switch ( type )
{
case BOOL:
case CHAR:
case INT:
return new Integer( (int)value );
case FLOAT:
if ( (long)(float)value == value ) {
return new Float( (float)value );
}
// else fall through:
case DOUBLE:
if ( (long)(double)value == value ) {
return new Double( (double)value );
}
// else fall through:
case LONG:
return new Long( value );
case BYTE:
return new Byte( (byte)value );
case SHORT:
return new Short( (short)value );
default:
return BigInteger.valueOf( value );
}
}
/**
* Returns a new Number object of an appropriate type to hold the given real value.
* The type of the returned object is always either Float or Double, and is only
* Float if the given type tag (a constant from the NumericTypes interface) is
* FLOAT.
*
* @param type the nominal numeric type of the result, a constant from the NumericTypes interface
* @param value the real value to convert to a Number object
* @return a Number object with the given value, of type implied by the type argument
*/
public static Number newReal( int type, double value )
{
if ( type == FLOAT )
return new Float( (float)value );
return new Double( value );
}
public static Object binaryOr( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
if ( type == BIGINT || type == BIGDEC )
return bigIntValue(v1).or(bigIntValue(v2));
return newInteger( type, longValue(v1) | longValue(v2) );
}
public static Object binaryXor( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
if ( type == BIGINT || type == BIGDEC )
return bigIntValue(v1).xor(bigIntValue(v2));
return newInteger( type, longValue(v1) ^ longValue(v2) );
}
public static Object binaryAnd( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
if ( type == BIGINT || type == BIGDEC )
return bigIntValue(v1).and(bigIntValue(v2));
return newInteger( type, longValue(v1) & longValue(v2) );
}
public static boolean equal( Object v1, Object v2 )
{
if ( v1 == null )
return v2 == null;
if ( v1 == v2 || isEqual(v1, v2) )
return true;
if ( v1 instanceof Number && v2 instanceof Number )
return ((Number)v1).doubleValue() == ((Number)v2).doubleValue();
return false;
}
public static boolean less( Object v1, Object v2 )
{
return compareWithConversion(v1, v2) < 0;
}
public static boolean greater( Object v1, Object v2 )
{
return compareWithConversion(v1, v2) > 0;
}
public static boolean in( Object v1, Object v2 ) throws OgnlException
{
if ( v2 == null ) // A null collection is always treated as empty
return false;
ElementsAccessor elementsAccessor = OgnlRuntime.getElementsAccessor(OgnlRuntime.getTargetClass(v2));
for ( Enumeration e = elementsAccessor.getElements(v2); e.hasMoreElements(); ) {
Object o = e.nextElement();
if ( equal(v1, o) )
return true;
}
return false;
}
public static Object shiftLeft( Object v1, Object v2 )
{
int type = getNumericType(v1);
if ( type == BIGINT || type == BIGDEC )
return bigIntValue(v1).shiftLeft( (int)longValue(v2) );
return newInteger( type, longValue(v1) << (int)longValue(v2) );
}
public static Object shiftRight( Object v1, Object v2 )
{
int type = getNumericType(v1);
if ( type == BIGINT || type == BIGDEC )
return bigIntValue(v1).shiftRight( (int)longValue(v2) );
return newInteger( type, longValue(v1) >> (int)longValue(v2) );
}
public static Object unsignedShiftRight( Object v1, Object v2 )
{
int type = getNumericType(v1);
if ( type == BIGINT || type == BIGDEC )
return bigIntValue(v1).shiftRight( (int)longValue(v2) );
if ( type <= INT )
return newInteger( INT, ((int)longValue(v1)) >>> (int)longValue(v2) );
return newInteger( type, longValue(v1) >>> (int)longValue(v2) );
}
public static Object add( Object v1, Object v2 )
{
int type = getNumericType(v1,v2,true);
switch ( type )
{
case BIGINT: return bigIntValue(v1).add(bigIntValue(v2));
case BIGDEC: return bigDecValue(v1).add(bigDecValue(v2));
case FLOAT:
case DOUBLE: return newReal( type, doubleValue(v1) + doubleValue(v2) );
case NONNUMERIC:
int t1 = getNumericType(v1),
t2 = getNumericType(v2);
if (((t1 != NONNUMERIC) && (v2 == null)) || ((t2 != NONNUMERIC) && (v1 == null))) {
throw new NullPointerException();
}
return stringValue(v1) + stringValue(v2);
default: return newInteger( type, longValue(v1) + longValue(v2) );
}
}
public static Object subtract( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
switch ( type )
{
case BIGINT: return bigIntValue(v1).subtract(bigIntValue(v2));
case BIGDEC: return bigDecValue(v1).subtract(bigDecValue(v2));
case FLOAT:
case DOUBLE: return newReal( type, doubleValue(v1) - doubleValue(v2) );
default: return newInteger( type, longValue(v1) - longValue(v2) );
}
}
public static Object multiply( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
switch ( type )
{
case BIGINT: return bigIntValue(v1).multiply(bigIntValue(v2));
case BIGDEC: return bigDecValue(v1).multiply(bigDecValue(v2));
case FLOAT:
case DOUBLE: return newReal( type, doubleValue(v1) * doubleValue(v2) );
default: return newInteger( type, longValue(v1) * longValue(v2) );
}
}
public static Object divide( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
switch ( type )
{
case BIGINT: return bigIntValue(v1).divide(bigIntValue(v2));
case BIGDEC: return bigDecValue(v1).divide( bigDecValue(v2), BigDecimal.ROUND_HALF_EVEN );
case FLOAT:
case DOUBLE: return newReal( type, doubleValue(v1) / doubleValue(v2) );
default: return newInteger( type, longValue(v1) / longValue(v2) );
}
}
public static Object remainder( Object v1, Object v2 )
{
int type = getNumericType(v1,v2);
switch ( type )
{
case BIGDEC:
case BIGINT: return bigIntValue(v1).remainder(bigIntValue(v2));
default: return newInteger( type, longValue(v1) % longValue(v2) );
}
}
public static Object negate( Object value )
{
int type = getNumericType(value);
switch ( type )
{
case BIGINT: return bigIntValue(value).negate();
case BIGDEC: return bigDecValue(value).negate();
case FLOAT:
case DOUBLE: return newReal( type, -doubleValue(value) );
default: return newInteger( type, -longValue(value) );
}
}
public static Object bitNegate( Object value )
{
int type = getNumericType(value);
switch ( type )
{
case BIGDEC:
case BIGINT: return bigIntValue(value).not();
default: return newInteger( type, ~longValue(value) );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -