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

43
LINES

< > BotCompany Repo | #1010609 // symbol - make Symbol from String or CharSequence

JavaX fragment (include)

1  
ifndef SymbolAsString
2  
sclass symbol_Tester {
3  
  S text;
4  
  
5  
  *(S *text) {}
6  
  
7  
  public int hashCode() { ret main hashCode(text); }
8  
  public bool equals(O o) {
9  
    ret o instanceof Symbol && eq(text, o/Symbol.text);
10  
  }
11  
}
12  
13  
static WeakHashMap<Symbol, Bool> symbol_map = newWeakHashMap();
14  
endifndef
15  
16  
static Symbol symbol(S s) {
17  
  ifdef SymbolAsString
18  
  ret s;
19  
  endifdef
20  
  ifndef SymbolAsString
21  
  if (s == null) null;
22  
  synchronized(symbol_map) {
23  
    // This is a trick that works because of how WeakHashMap compares keys.
24  
    O e = call(symbol_map, 'getEntry, symbol_Tester(s));
25  
    Symbol sym = e == null ? null : ((WeakReference<Symbol>) e).get();
26  
    if (sym == null)
27  
      symbol_map.put(sym = Symbol(s, false), Bool.TRUE);
28  
    ret sym;
29  
  }
30  
  endifndef
31  
}
32  
33  
static Symbol symbol(CharSequence s) {
34  
  if (s == null) null;
35  
  ifdef SymbolAsString
36  
  ret str(s);
37  
  endifdef
38  
  ifndef SymbolAsString
39  
  if (s instanceof Symbol) ret (Symbol) s;
40  
  if (s instanceof S) ret symbol((S) s);
41  
  ret symbol(str(s));
42  
  endifndef
43  
}

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: #1010609
Snippet name: symbol - make Symbol from String or CharSequence
Eternal ID of this version: #1010609/7
Text MD5: 4cde3df43c585ace2d6a658128226614
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-23 05:21:39
Source code size: 1052 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 350 / 389
Version history: 6 change(s)
Referenced in: [show references]