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).

sclass DefaultValueConverterForField implements ValueConverterForField {
  public OrError<O> convertValue(O object, Field field, O value) {
    Class actualType = field.getType();
    Class type = primitiveToBoxedTypeOpt(actualType);

    // nothing to do
    
    if (isInstance(type, value))
      ret OrError(value);
    
    // int magic
    
    if (type == Int.class && !value instanceof Int)
      ret OrError(toInt(trimIfString(value));

    // double magic
    
    if (type == Double.class && !value instanceof Double)
      ret OrError(toDouble(trimIfString(value));

    // bool magic
    
    if (type == Bool.class && value instanceof S)
      if (actualType == Bool.class && eq(value, ""))
        ret OrError(null);
      else
        ret OrError(eqicOneOf((S) value, "1", "true", "yes"));

    // GlobalID magic
    
    ifclass GlobalID
    if (type == GlobalID.class && value instanceof S)
      ret OrError(GlobalID((S) value));
    endif
    
    ret OrError(value);
  }
}

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: 201 / 465
Version history: 5 change(s)
Referenced in: [show references]