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

38
LINES

< > BotCompany Repo | #1029593 // DefaultValueConverterForField

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

Libraryless. Click here for Pure Java version (3721L/24K).

1  
sclass DefaultValueConverterForField implements ValueConverterForField {
2  
  public OrError<O> convertValue(O object, Field field, O value) {
3  
    Class actualType = field.getType();
4  
    Class type = primitiveToBoxedTypeOpt(actualType);
5  
6  
    // nothing to do
7  
    
8  
    if (isInstance(type, value))
9  
      ret OrError(value);
10  
    
11  
    // int magic
12  
    
13  
    if (type == Int.class && !value instanceof Int)
14  
      ret OrError(toInt(trimIfString(value));
15  
16  
    // double magic
17  
    
18  
    if (type == Double.class && !value instanceof Double)
19  
      ret OrError(toDouble(trimIfString(value));
20  
21  
    // bool magic
22  
    
23  
    if (type == Bool.class && value instanceof S)
24  
      if (actualType == Bool.class && eq(value, ""))
25  
        ret OrError(null);
26  
      else
27  
        ret OrError(eqicOneOf((S) value, "1", "true", "yes"));
28  
29  
    // GlobalID magic
30  
    
31  
    ifclass GlobalID
32  
    if (type == GlobalID.class && value instanceof S)
33  
      ret OrError(GlobalID((S) value));
34  
    endif
35  
    
36  
    ret OrError(value);
37  
  }
38  
}

Author comment

Began life as a copy of #1026010

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1029593
Snippet name: DefaultValueConverterForField
Eternal ID of this version: #1029593/6
Text MD5: 2a5b7dc0a7626d5437e8cdefbcd71818
Transpilation MD5: 1c1aeeab3603de4d056dcbc317ed9be1
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-09-27 16:42:35
Source code size: 1030 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 283 / 569
Version history: 5 change(s)
Referenced in: [show references]