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

1  
// fields: type + name
2  
sS tok_genEqualsAndHashCode(S className, LPairS fields, O... _) {
3  
  optPar S qualifier = "";
4  
  optPar bool flexEq;
5  
  S argName = "o";
6  
  while (pairsB(fields).contains(argName)) argName = "_" + argName;
7  
  StringBuilder buf = new("\n\npublic bool equals(O \*argName*/) {\n");
8  
  S instanceCheck = flexEq
9  
    ? "_getClass(\*argName*/) == getClass()"
10  
    : "\*argName*/ instanceof " + qualifier + className;
11  
  if (empty(fields))
12  
    buf.append("ret \*instanceCheck*/;\n");
13  
  else {
14  
    S x = makeVar();
15  
    buf.append("if (!(" + instanceCheck + [[)) false;
16  
    ]] + className + " \*x*/ = cast " + argName + [[;
17  
    ret ]] + join(" && ", map(fields, func(PairS p) -> S {
18  
      tok_isPrimitiveType(p.a)
19  
        ? p.b + " == \*x*/." + p.b
20  
        : "eq(" + p.b + ", \*x*/." + p.b + ")"
21  
    })) + ";\n");
22  
  }
23  
  buf.append("}\n");
24  
  
25  
  buf.append([[
26  
  public int hashCode() {
27  
    int h = ]] + hashCode(className) + [[;
28  
    ]] + concatMap_strings(fields, func(Pair<S> p) {
29  
      "h = boostHashCombine(h, _hashCode(" + p.b + [[));
30  
    ]]}) + [[ret h;
31  
  }
32  
  ]]);
33  
  ret str(buf);
34  
}

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: 453 / 610
Version history: 17 change(s)
Referenced in: [show references]