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

44
LINES

< > BotCompany Repo | #1010604 // Symbol test (symbols are strings comparable with ==) [OK]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (542L/4K).

!7

//!include #1010605 // WeakHashMap2

sclass Symbol {
  S text;
  bool test;

  *() {}
  *(S *text, bool *test) {} // weird signature to prevent accidental calling
  
  public int hashCode() { ret main.hashCode(text); }
  toString { ret text; }
  public bool equals(O o) {
    if (!test) ret this == o;
    ret o instanceof Symbol && eq(text, o/Symbol.text);
  }
}

static new WeakHashMap<Symbol, Bool> symbolMap;

static Symbol symbol(S s) {
  if (s == null) null;
  synchronized(symbolMap) {
    Symbol test = Symbol(s, true);
    //WeakHashMap2.Entry<Symbol, Bool> e = symbolMap.getEntry(test);
    //Symbol sym = e == null ? null : e.get();
    O e = call(symbolMap, 'getEntry, test);
    Symbol sym = cast callOpt(e, 'get);
    if (sym == null)
      symbolMap.put(sym = Symbol(s, false), Bool.TRUE);
    ret sym;
  }
}

p {
  Symbol a = symbol("a");
  Symbol a2 = symbol("a");
  assertEquals("a", str(a));
  assertEquals("a", str(a2));
  assertIdentical(a, a2);
  Symbol b = symbol("b");
  assertEquals("b", str(b));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1010604
Snippet name: Symbol test (symbols are strings comparable with ==) [OK]
Eternal ID of this version: #1010604/9
Text MD5: 6be029f471a3cef824a864e47d75b250
Transpilation MD5: 0cc1d26e533f302fbe603f0e2b0de23d
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-23 06:54:43
Source code size: 1070 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 539 / 1105
Version history: 8 change(s)
Referenced in: [show references]