Download Jar. Libraryless. Click here for Pure Java version (542L/4K).
| 1 | !7 | 
| 2 | |
| 3 | //!include #1010605 // WeakHashMap2 | 
| 4 | |
| 5 | sclass Symbol {
 | 
| 6 | S text; | 
| 7 | bool test; | 
| 8 | |
| 9 |   *() {}
 | 
| 10 |   *(S *text, bool *test) {} // weird signature to prevent accidental calling
 | 
| 11 | |
| 12 |   public int hashCode() { ret main.hashCode(text); }
 | 
| 13 |   toString { ret text; }
 | 
| 14 |   public bool equals(O o) {
 | 
| 15 | if (!test) ret this == o; | 
| 16 | ret o instanceof Symbol && eq(text, o/Symbol.text); | 
| 17 | } | 
| 18 | } | 
| 19 | |
| 20 | static new WeakHashMap<Symbol, Bool> symbolMap; | 
| 21 | |
| 22 | static Symbol symbol(S s) {
 | 
| 23 | if (s == null) null; | 
| 24 |   synchronized(symbolMap) {
 | 
| 25 | Symbol test = Symbol(s, true); | 
| 26 | //WeakHashMap2.Entry<Symbol, Bool> e = symbolMap.getEntry(test); | 
| 27 | //Symbol sym = e == null ? null : e.get(); | 
| 28 | O e = call(symbolMap, 'getEntry, test); | 
| 29 | Symbol sym = cast callOpt(e, 'get); | 
| 30 | if (sym == null) | 
| 31 | symbolMap.put(sym = Symbol(s, false), Bool.TRUE); | 
| 32 | ret sym; | 
| 33 | } | 
| 34 | } | 
| 35 | |
| 36 | p {
 | 
| 37 |   Symbol a = symbol("a");
 | 
| 38 |   Symbol a2 = symbol("a");
 | 
| 39 |   assertEquals("a", str(a));
 | 
| 40 |   assertEquals("a", str(a2));
 | 
| 41 | assertIdentical(a, a2); | 
| 42 |   Symbol b = symbol("b");
 | 
| 43 |   assertEquals("b", str(b));
 | 
| 44 | } | 
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: | 810 / 1798 | 
| Version history: | 8 change(s) | 
| Referenced in: | [show references] |