import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import java.util.function.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.geom.*; import javax.imageio.*; import java.math.*; // Can a be converted to b? // score 0 = exact match // score Int.MAX_VALUE = no match // score 1 = conversion needed (boxing/unboxing or non-primitve widening) // score -1 = primitive widening needed class main { static int typeConversionScoreWithUnboxing(Class a, Class b) { int score = typeConversionScore(a, b); if (score == 0 || score == 1) return score; Class aPrim = boxedToPrimitiveType(a); if (aPrim != null) { int score2 = typeConversionScore(aPrim, b); if (score2 == 0) return 1; else if (score2 != Integer.MAX_VALUE) return score2; } return score; } // Can a be converted to b? // score 0 = exact match // score Int.MAX_VALUE = no match // score 1 = conversion needed (boxing/unboxing or non-primitve widening) // score -1 = primitive widening needed static int typeConversionScore(Class a, Class b) { if (a == b) return 0; if (b.isPrimitive()) { // target type is primitive if (a.isPrimitive()) { // both types are primitive // No widenings to bool if (b == boolean.class) return Integer.MAX_VALUE; // No widenings to byte if (b == byte.class) return Integer.MAX_VALUE; // byte can be converted to char if (b == char.class) return a == byte.class ? -1 : Integer.MAX_VALUE; // byte can be converted to short if (b == short.class) return a == byte.class ? -1 : Integer.MAX_VALUE; // byte, char and short can be converted to int if (b == int.class) return a == byte.class || a == char.class || a == short.class ? -1 : Integer.MAX_VALUE; // byte, char, short and int can be converted to long if (b == long.class) return a == byte.class || a == char.class || a == short.class || a == int.class ? -1 : Integer.MAX_VALUE; // byte, char, short and int can be converted to float if (b == float.class) return a == byte.class || a == char.class || a == short.class || a == int.class ? -1 : Integer.MAX_VALUE; // all primitive types except bool can be converted to double return a != boolean.class ? -1 : Integer.MAX_VALUE; } else { // source type is boxed - check if they're a match return primitiveToBoxedType(b) == a ? 1 : Integer.MAX_VALUE; } } else { // target type is not primitive // object type is primitive - check for exact match if (a.isPrimitive()) return primitiveToBoxedType(a) == b ? 1 : Integer.MAX_VALUE; // both types are not primitive return b.isAssignableFrom(a) ? 1 : Integer.MAX_VALUE; } } static Class boxedToPrimitiveType(Class type) { if (type == Boolean.class) return boolean.class; if (type == Integer.class) return int.class; if (type == Long.class) return long.class; if (type == Float.class) return float.class; if (type == Short.class) return short.class; if (type == Character.class) return char.class; if (type == Byte.class) return byte.class; if (type == Double.class) return double.class; return null; } static Class primitiveToBoxedType(Class type) { if (type == boolean.class) return Boolean.class; if (type == int.class) return Integer.class; if (type == long.class) return Long.class; if (type == float.class) return Float.class; if (type == short.class) return Short.class; if (type == char.class) return Character.class; if (type == byte.class) return Byte.class; if (type == double.class) return Double.class; return null; } }