@@ -264,7 +264,7 @@ public static <T> T convertProcToInterface(ThreadContext context, RubyBasicObjec
264
264
return (T ) javaObject .getValue ();
265
265
}
266
266
267
- public static NumericConverter getNumericConverter (Class target ) {
267
+ public static < T > NumericConverter < T > getNumericConverter (Class < T > target ) {
268
268
final NumericConverter converter = NUMERIC_CONVERTERS .get (target );
269
269
return converter == null ? NUMERIC_TO_OTHER : converter ;
270
270
}
@@ -587,8 +587,8 @@ public static abstract class JavaConverter {
587
587
public String toString () {return type .getName () + " converter" ;}
588
588
}
589
589
590
- public interface NumericConverter {
591
- public Object coerce (RubyNumeric numeric , Class target );
590
+ public interface NumericConverter < T > {
591
+ T coerce (RubyNumeric numeric , Class < T > target );
592
592
}
593
593
594
594
private static IRubyObject trySimpleConversions (Ruby runtime , Object object ) {
@@ -910,90 +910,61 @@ public void set(Ruby runtime, Object array, int i, IRubyObject value) {
910
910
JAVA_CONVERTERS .put (BigInteger .class , JAVA_BIGINTEGER_CONVERTER );
911
911
}
912
912
913
- private static final NumericConverter NUMERIC_TO_BYTE = new NumericConverter () {
914
- public Object coerce (RubyNumeric numeric , Class target ) {
915
- final long value = numeric .getLongValue ();
916
- if ( isLongByteable (value ) ) return (byte ) value ;
917
- throw numeric .getRuntime ().newRangeError ("too big for byte: " + numeric );
918
- }
919
- };
920
- private static final NumericConverter NUMERIC_TO_SHORT = new NumericConverter () {
921
- public Object coerce (RubyNumeric numeric , Class target ) {
922
- final long value = numeric .getLongValue ();
923
- if ( isLongShortable (value ) ) return (short ) value ;
924
- throw numeric .getRuntime ().newRangeError ("too big for short: " + numeric );
925
- }
926
- };
927
- private static final NumericConverter NUMERIC_TO_CHARACTER = new NumericConverter () {
928
- public Object coerce (RubyNumeric numeric , Class target ) {
929
- final long value = numeric .getLongValue ();
930
- if ( isLongCharable (value ) ) return (char ) value ;
931
- throw numeric .getRuntime ().newRangeError ("too big for char: " + numeric );
932
- }
913
+ private static final NumericConverter <Byte > NUMERIC_TO_BYTE = (numeric , target ) -> {
914
+ final long value = numeric .getLongValue ();
915
+ if ( isLongByteable (value ) ) return (byte ) value ;
916
+ throw numeric .getRuntime ().newRangeError ("too big for byte: " + numeric );
933
917
};
934
- private static final NumericConverter NUMERIC_TO_INTEGER = new NumericConverter () {
935
- public Object coerce (RubyNumeric numeric , Class target ) {
936
- final long value = numeric .getLongValue ();
937
- if ( isLongIntable (value ) ) return (int ) value ;
938
- throw numeric .getRuntime ().newRangeError ("too big for int: " + numeric );
939
- }
918
+ private static final NumericConverter <Short > NUMERIC_TO_SHORT = (numeric , target ) -> {
919
+ final long value = numeric .getLongValue ();
920
+ if ( isLongShortable (value ) ) return (short ) value ;
921
+ throw numeric .getRuntime ().newRangeError ("too big for short: " + numeric );
940
922
};
941
- private static final NumericConverter NUMERIC_TO_LONG = new NumericConverter () {
942
- public Object coerce ( RubyNumeric numeric , Class target ) {
943
- return numeric . getLongValue () ;
944
- }
923
+ private static final NumericConverter < Character > NUMERIC_TO_CHARACTER = ( numeric , target ) -> {
924
+ final long value = numeric . getLongValue ();
925
+ if ( isLongCharable ( value ) ) return ( char ) value ;
926
+ throw numeric . getRuntime (). newRangeError ( "too big for char: " + numeric );
945
927
};
946
- private static final NumericConverter NUMERIC_TO_FLOAT = new NumericConverter () {
947
- public Object coerce (RubyNumeric numeric , Class target ) {
948
- final double value = numeric .getDoubleValue ();
949
- // many cases are ok to convert to float; if not one of these, error
950
- if ( isDoubleFloatable (value ) ) return (float ) value ;
951
- throw numeric .getRuntime ().newTypeError ("too big for float: " + numeric );
952
- }
928
+ private static final NumericConverter <Integer > NUMERIC_TO_INTEGER = (numeric , target ) -> {
929
+ final long value = numeric .getLongValue ();
930
+ if ( isLongIntable (value ) ) return (int ) value ;
931
+ throw numeric .getRuntime ().newRangeError ("too big for int: " + numeric );
953
932
};
954
- private static final NumericConverter NUMERIC_TO_DOUBLE = new NumericConverter () {
955
- public Object coerce (RubyNumeric numeric , Class target ) {
956
- return numeric .getDoubleValue ();
957
- }
933
+ private static final NumericConverter <Long > NUMERIC_TO_LONG = (numeric , target ) -> numeric .getLongValue ();
934
+ private static final NumericConverter <Float > NUMERIC_TO_FLOAT = (numeric , target ) -> {
935
+ final double value = numeric .getDoubleValue ();
936
+ // many cases are ok to convert to float; if not one of these, error
937
+ if ( isDoubleFloatable (value ) ) return (float ) value ;
938
+ throw numeric .getRuntime ().newTypeError ("too big for float: " + numeric );
958
939
};
959
- private static final NumericConverter NUMERIC_TO_BIGINTEGER = new NumericConverter () {
960
- public Object coerce (RubyNumeric numeric , Class target ) {
961
- return numeric .getBigIntegerValue ();
962
- }
963
- };
964
- private static final NumericConverter NUMERIC_TO_OBJECT = new NumericConverter () {
965
- public Object coerce (RubyNumeric numeric , Class target ) {
966
- // for Object, default to natural wrapper type
967
- if (numeric instanceof RubyFixnum ) {
968
- long value = numeric .getLongValue ();
969
- return Long .valueOf (value );
970
- } else if (numeric instanceof RubyFloat ) {
971
- double value = numeric .getDoubleValue ();
972
- return Double .valueOf (value );
973
- } else if (numeric instanceof RubyBignum ) {
974
- return ((RubyBignum )numeric ).getValue ();
975
- } else if (numeric instanceof RubyBigDecimal ) {
976
- return ((RubyBigDecimal )numeric ).getValue ();
977
- } else {
978
- return NUMERIC_TO_OTHER .coerce (numeric , target );
979
- }
980
- }
981
- };
982
- private static final NumericConverter NUMERIC_TO_OTHER = new NumericConverter () {
983
- public Object coerce (RubyNumeric numeric , Class target ) {
984
- if (target .isAssignableFrom (numeric .getClass ())) {
985
- // just return as-is, since we can't do any coercion
986
- return numeric ;
987
- }
988
- // otherwise, error; no conversion available
989
- throw numeric .getRuntime ().newTypeError ("could not coerce " + numeric .getMetaClass () + " to " + target );
940
+ private static final NumericConverter <Double > NUMERIC_TO_DOUBLE = (numeric , target ) -> numeric .getDoubleValue ();
941
+ private static final NumericConverter <BigInteger > NUMERIC_TO_BIGINTEGER = (numeric , target ) -> numeric .getBigIntegerValue ();
942
+
943
+ private static final NumericConverter NUMERIC_TO_OTHER = (numeric , target ) -> {
944
+ if (target .isAssignableFrom (numeric .getClass ())) {
945
+ // just return as-is, since we can't do any coercion
946
+ return numeric ;
990
947
}
948
+ // otherwise, error; no conversion available
949
+ throw numeric .getRuntime ().newTypeError ("could not coerce " + numeric .getMetaClass () + " to " + target );
991
950
};
992
- private static final NumericConverter NUMERIC_TO_VOID = new NumericConverter () {
993
- public Object coerce (RubyNumeric numeric , Class target ) {
994
- return null ;
951
+ private static final NumericConverter <Object > NUMERIC_TO_OBJECT = (numeric , target ) -> {
952
+ // for Object, default to natural wrapper type
953
+ if (numeric instanceof RubyFixnum ) {
954
+ long value = numeric .getLongValue ();
955
+ return Long .valueOf (value );
956
+ } else if (numeric instanceof RubyFloat ) {
957
+ double value = numeric .getDoubleValue ();
958
+ return Double .valueOf (value );
959
+ } else if (numeric instanceof RubyBignum ) {
960
+ return ((RubyBignum )numeric ).getValue ();
961
+ } else if (numeric instanceof RubyBigDecimal ) {
962
+ return ((RubyBigDecimal )numeric ).getValue ();
963
+ } else {
964
+ return NUMERIC_TO_OTHER .coerce (numeric , target );
995
965
}
996
966
};
967
+ private static final NumericConverter NUMERIC_TO_VOID = (numeric , target ) -> null ;
997
968
private static boolean isDoubleFloatable (double value ) {
998
969
return true ;
999
970
}
0 commit comments