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

49
LINES

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

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

Libraryless. Click here for Pure Java version (3786L/22K).

1  
ifndef SymbolAsString
2  
3  
static WeakHasherMap<Symbol, Bool> symbol_map = new WeakHasherMap(new Hasher<Symbol>() {
4  
  public int hashCode(Symbol symbol) { ret symbol.text.hashCode(); }
5  
  public bool equals(Symbol a, Symbol b) {
6  
    if (a == null) ret b == null;
7  
    ret b != null && eq(a.text, b.text);
8  
  }
9  
});
10  
11  
endifndef
12  
13  
static Symbol symbol(S s) {
14  
  ifdef SymbolAsString
15  
  ret s;
16  
  endifdef
17  
  ifndef SymbolAsString
18  
  if (s == null) null;
19  
  synchronized(symbol_map) {
20  
    // TODO: avoid object creation by passing the string to findKey
21  
    Symbol symbol = new(s, true);
22  
    Symbol existingSymbol = symbol_map.findKey(symbol);
23  
    if (existingSymbol == null)
24  
      symbol_map.put(existingSymbol = symbol, true);
25  
    ifdef symbol_debug
26  
    if (eqic(s, "Java"))
27  
      print("Symbol for " + s + ": " + symbolToStringFull(existingSymbol);
28  
    endifdef
29  
      
30  
    ret existingSymbol;
31  
  }
32  
  endifndef
33  
}
34  
35  
static Symbol symbol(CharSequence s) {
36  
  if (s == null) null;
37  
  ifdef SymbolAsString
38  
  ret str(s);
39  
  endifdef
40  
  ifndef SymbolAsString
41  
  if (s instanceof Symbol) ret (Symbol) s;
42  
  if (s instanceof S) ret symbol((S) s);
43  
  ret symbol(str(s));
44  
  endifndef
45  
}
46  
47  
static Symbol symbol(O o) {
48  
  ret symbol((CharSequence) o);
49  
}

Author comment

Began life as a copy of #1010609

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1012686
Snippet name: symbol - make Symbol from String or CharSequence
Eternal ID of this version: #1012686/42
Text MD5: e9d6bc4fb0088068d9e1c5b1c6b7b18c
Transpilation MD5: ef3a1e38ca2ad4e5a4f0c5eede709c5d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-08 03:48:33
Source code size: 1259 bytes / 49 lines
Pitched / IR pitched: No / No
Views / Downloads: 446 / 881
Version history: 41 change(s)
Referenced in: [show references]