Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

67
LINES

< > BotCompany Repo | #1034747 // typeConversionScore

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (123L) is out of date.

// 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 -2 = primitive widening needed

static int typeConversionScore(Class a, Class b) {
  if (a == b) ret 0;
  
  if (b.isPrimitive()) {
    // target type is primitive
    
    if (a.isPrimitive()) {
      // both types are primitive
      
      // No widenings to bool
      if (b == bool.class) ret Int.MAX_VALUE;
      
      // No widenings to byte
      if (b == byte.class) ret Int.MAX_VALUE;
      
      // byte can be converted to char
      if (b == char.class) ret a == byte.class ? -2 : Int.MAX_VALUE;
      
      // byte can be converted to short
      if (b == short.class) ret a == byte.class ? -2 : Int.MAX_VALUE;
      
      // byte, char and short can be converted to int
      if (b == int.class) ret a == byte.class || a == char.class || a == short.class ? -2 : Int.MAX_VALUE;
      
      // byte, char, short and int can be converted to long
      if (b == long.class) ret a == byte.class || a == char.class || a == short.class || a == int.class ? -2 : Int.MAX_VALUE;
      
      // byte, char, short and int can be converted to float
      if (b == float.class) ret a == byte.class || a == char.class || a == short.class || a == int.class ? -2 : Int.MAX_VALUE;
      
      // all primitive types except bool can be converted to double
      ret a != bool.class ? -2 : Int.MAX_VALUE;
    } else {
      // source type is boxed - check if they're a match 
      ret primitiveToBoxedType(b) == a ?  1 : Int.MAX_VALUE;
    }
  } else {
    // Target type b is not primitive.
    
    // Object type a is primitive - check for exact match
    if (a.isPrimitive())
      ret primitiveToBoxedType(a) == b ? 1 : Int.MAX_VALUE;

    // BOTH TYPES ARE NOT PRIMITIVE.
    
    // if not assignable, no match
    if (!b.isAssignableFrom(a)) ret Int.MAX_VALUE;
    
    // if assignable, get precise score
    
    // if any is an interface, just get score 1
    if (a.isInterface() || b.isInterface()) ret 1;
    
    if (a.isArray() && b.isArray())
      ret typeConversionScore(a.getComponentType(), b.getComponentType())/2;
    
    // for classes, count subclass distance
    ret subclassDistance(a, b)*2;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, ekrmjmnbrukm, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034747
Snippet name: typeConversionScore
Eternal ID of this version: #1034747/12
Text MD5: 7d1970f2d3ababee65e6c8e935afd6df
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-11 23:16:31
Source code size: 2364 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 170 / 269
Version history: 11 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1034755 - typeConversionScoreWithUnboxing
#1035459 - typeConversionScore - backup before precise subtype dispatch