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

34
LINES

< > BotCompany Repo | #1017037 // tok_genEqualsAndHashCode

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

Libraryless. Click here for Pure Java version (2723L/17K).

// fields: type + name
sS tok_genEqualsAndHashCode(S className, LPairS fields, O... _) {
  optPar S qualifier = "";
  optPar bool flexEq;
  S argName = "o";
  while (pairsB(fields).contains(argName)) argName = "_" + argName;
  StringBuilder buf = new("\n\npublic bool equals(O \*argName*/) {\n");
  S instanceCheck = flexEq
    ? "_getClass(\*argName*/) == getClass()"
    : "\*argName*/ instanceof " + qualifier + className;
  if (empty(fields))
    buf.append("ret \*instanceCheck*/;\n");
  else {
    S x = makeVar();
    buf.append("if (!(" + instanceCheck + [[)) false;
    ]] + className + " \*x*/ = cast " + argName + [[;
    ret ]] + join(" && ", map(fields, func(PairS p) -> S {
      tok_isPrimitiveType(p.a)
        ? p.b + " == \*x*/." + p.b
        : "eq(" + p.b + ", \*x*/." + p.b + ")"
    })) + ";\n");
  }
  buf.append("}\n");
  
  buf.append([[
  public int hashCode() {
    int h = ]] + hashCode(className) + [[;
    ]] + concatMap_strings(fields, func(Pair<S> p) {
      "h = boostHashCombine(h, _hashCode(" + p.b + [[));
    ]]}) + [[ret h;
  }
  ]]);
  ret str(buf);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1017037
Snippet name: tok_genEqualsAndHashCode
Eternal ID of this version: #1017037/18
Text MD5: b379d269d52bb3aed4bca875d2c6804d
Transpilation MD5: 15fc7a6f64c73ee20641468e4c742e03
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-24 15:13:56
Source code size: 1123 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 452 / 607
Version history: 17 change(s)
Referenced in: [show references]