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.

1  
// Can a be converted to b?
2  
3  
// score 0 = exact match
4  
// score Int.MAX_VALUE = no match
5  
// score 1 = conversion needed (boxing/unboxing or non-primitve widening)
6  
// score -2 = primitive widening needed
7  
8  
static int typeConversionScore(Class a, Class b) {
9  
  if (a == b) ret 0;
10  
  
11  
  if (b.isPrimitive()) {
12  
    // target type is primitive
13  
    
14  
    if (a.isPrimitive()) {
15  
      // both types are primitive
16  
      
17  
      // No widenings to bool
18  
      if (b == bool.class) ret Int.MAX_VALUE;
19  
      
20  
      // No widenings to byte
21  
      if (b == byte.class) ret Int.MAX_VALUE;
22  
      
23  
      // byte can be converted to char
24  
      if (b == char.class) ret a == byte.class ? -2 : Int.MAX_VALUE;
25  
      
26  
      // byte can be converted to short
27  
      if (b == short.class) ret a == byte.class ? -2 : Int.MAX_VALUE;
28  
      
29  
      // byte, char and short can be converted to int
30  
      if (b == int.class) ret a == byte.class || a == char.class || a == short.class ? -2 : Int.MAX_VALUE;
31  
      
32  
      // byte, char, short and int can be converted to long
33  
      if (b == long.class) ret a == byte.class || a == char.class || a == short.class || a == int.class ? -2 : Int.MAX_VALUE;
34  
      
35  
      // byte, char, short and int can be converted to float
36  
      if (b == float.class) ret a == byte.class || a == char.class || a == short.class || a == int.class ? -2 : Int.MAX_VALUE;
37  
      
38  
      // all primitive types except bool can be converted to double
39  
      ret a != bool.class ? -2 : Int.MAX_VALUE;
40  
    } else {
41  
      // source type is boxed - check if they're a match 
42  
      ret primitiveToBoxedType(b) == a ?  1 : Int.MAX_VALUE;
43  
    }
44  
  } else {
45  
    // Target type b is not primitive.
46  
    
47  
    // Object type a is primitive - check for exact match
48  
    if (a.isPrimitive())
49  
      ret primitiveToBoxedType(a) == b ? 1 : Int.MAX_VALUE;
50  
51  
    // BOTH TYPES ARE NOT PRIMITIVE.
52  
    
53  
    // if not assignable, no match
54  
    if (!b.isAssignableFrom(a)) ret Int.MAX_VALUE;
55  
    
56  
    // if assignable, get precise score
57  
    
58  
    // if any is an interface, just get score 1
59  
    if (a.isInterface() || b.isInterface()) ret 1;
60  
    
61  
    if (a.isArray() && b.isArray())
62  
      ret typeConversionScore(a.getComponentType(), b.getComponentType())/2;
63  
    
64  
    // for classes, count subclass distance
65  
    ret subclassDistance(a, b)*2;
66  
  }
67  
}

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: 117 / 202
Version history: 11 change(s)
Referenced in: [show references]