static Object castNumberStrictJava(Class<?> toType, Number number) { if (toType == Byte.class || toType == Byte.TYPE) return number.byteValue(); if (toType == Short.class || toType == Short.TYPE) return number.shortValue(); if (toType == Character.class || toType == Character.TYPE) return (char) number.intValue(); if (toType == Integer.class || toType == Integer.TYPE) return number.intValue(); if (toType == Long.class || toType == Long.TYPE) return number.longValue(); if (toType == Float.class || toType == Float.TYPE) return number.floatValue(); if (toType == Double.class || toType == Double.TYPE) return number.doubleValue(); return castNumber(toType, number); }
rhs = Primitive.castNumber(BigDecimal.class, rnum); } else if ( rhs instanceof BigDecimal ) { lhs = Primitive.castNumber(BigDecimal.class, lnum); } else if ( Types.isFloatingpoint(lhs) || Types.isFloatingpoint(rhs)) { if ( !(lhs instanceof Double) ) } else if ( lhs instanceof BigInteger ) { if ( !(rhs instanceof BigInteger) ) rhs = Primitive.castNumber(BigInteger.class, rnum); } else if ( rhs instanceof BigInteger ) { lhs = Primitive.castNumber(BigInteger.class, lnum); } else { if ( !(lhs instanceof Long) )
/** Primitives compare equal with other Primitives containing an equal wrapped value. */ public boolean equals( Object obj ) { if ( !( obj instanceof Primitive ) ) if ( wrapperMap.containsKey(obj.getClass()) ) obj = new Primitive(obj); else return false; Primitive pobj = (Primitive) obj; if ( pobj.isNumber() && this.isNumber() ) { if ( this.getType() == BigDecimal.class ) return this.value.equals(castNumber(BigDecimal.class, pobj.numberValue())); if ( pobj.getType() == BigDecimal.class ) return pobj.value.equals(castNumber(BigDecimal.class, this.numberValue())); if (Types.isFloatingpoint(this.value) || Types.isFloatingpoint(pobj.value)) return this.numberValue().doubleValue() == pobj.numberValue().doubleValue(); if ( this.getType() == BigInteger.class ) return this.value.equals(castNumber(BigInteger.class, pobj.numberValue())); if ( pobj.getType() == BigInteger.class ) return pobj.value.equals(castNumber(BigInteger.class, this.numberValue())); return this.numberValue().longValue() == pobj.numberValue().longValue(); } return this.value.equals( pobj.value ); }
/** Cast a primitive value represented by its java.lang wrapper type to the specified java.lang wrapper type. e.g. Byte(5) to Integer(5) or Integer(5) to Byte(5) @param toType is the java TYPE type @param value is the value in java.lang wrapper. value may not be null. */ static Object castWrapper( Class<?> toType, Object value ) { value = Primitive.unwrap(value); if ( !(Primitive.isWrapperType(toType) || toType.isPrimitive()) ) throw new InterpreterError("invalid type in castWrapper: "+toType); if ( value == null ) throw new InterpreterError("null value in castWrapper, guard"); if ( value instanceof Boolean ) { if ( toType != Boolean.TYPE ) throw new InterpreterError("bad wrapper cast of boolean"); return value; } // first promote char to Number type to avoid duplicating code if ( value instanceof Character ) value = Integer.valueOf(((Character)value).charValue()); if ( !(value instanceof Number) ) throw new InterpreterError("bad type in cast "+StringUtil.typeValueString(value)); return castNumber(toType, (Number) value); }
return new Primitive( castNumber(toType, fromValue.numberValue()) );