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

70
LINES

< > BotCompany Repo | #1031625 // symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol [backup]

JavaX fragment (include)

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  
static WeakHasherMap<MasterSymbol, Bool> masterSymbol_map = new WeakHasherMap(new Hasher<MasterSymbol>() {
12  
  public int hashCode(MasterSymbol symbol) { ret symbol.lowerCaseHash(); }
13  
  public bool equals(MasterSymbol a, MasterSymbol b) {
14  
    if (a == null) ret b == null;
15  
    ret b != null && eq(a.lowerCase(), b.lowerCase());
16  
  }
17  
});
18  
19  
endifndef
20  
21  
static Symbol symbol(S s) {
22  
  ifdef SymbolAsString
23  
  ret s;
24  
  endifdef
25  
  ifndef SymbolAsString
26  
  if (s == null) null;
27  
  synchronized(symbol_map) {
28  
    MasterSymbol master = new MasterSymbol(s, true);
29  
    MasterSymbol existingMaster = masterSymbol_map.findKey(master);
30  
    if (existingMaster == null)
31  
      masterSymbol_map.put(existingMaster = master, true);
32  
    ifdef symbol_debug
33  
    if (eqic(s, "Java"))
34  
      print("existing master for " + s + ": " + existingMaster);
35  
    endifdef
36  
    
37  
    // directly return master if exact match
38  
    bool isEq = eq(s, existingMaster.text);
39  
    if (isEq) ret existingMaster;
40  
    
41  
    // second lookup after master found
42  
    Symbol symbol = new Symbol(s, existingMaster);
43  
    Symbol existingSymbol = symbol_map.findKey(symbol);
44  
    if (existingSymbol == null)
45  
      symbol_map.put(existingSymbol = symbol, true);
46  
    ifdef symbol_debug
47  
    if (eqic(s, "Java"))
48  
      print("Symbol for " + s + ": " + symbolToStringFull(existingSymbol);
49  
    endifdef
50  
      
51  
    ret existingSymbol;
52  
  }
53  
  endifndef
54  
}
55  
56  
static Symbol symbol(CharSequence s) {
57  
  if (s == null) null;
58  
  ifdef SymbolAsString
59  
  ret str(s);
60  
  endifdef
61  
  ifndef SymbolAsString
62  
  if (s instanceof Symbol) ret (Symbol) s;
63  
  if (s instanceof S) ret symbol((S) s);
64  
  ret symbol(str(s));
65  
  endifndef
66  
}
67  
68  
static Symbol symbol(O o) {
69  
  ret symbol((CharSequence) o);
70  
}

Author comment

Began life as a copy of #1012686

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031625
Snippet name: symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol [backup]
Eternal ID of this version: #1031625/1
Text MD5: 4d8d74b3f5e9afba5cba41ef026f3c11
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-22 13:05:10
Source code size: 2075 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 152 / 176
Referenced in: [show references]