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)

ifndef SymbolAsString
sclass symbol_Tester {
  S text;
  
  *(S *text) {}
  
  public int hashCode() { ret main hashCode(text); }
  public bool equals(O o) {
    ret o instanceof Symbol && eq(text, o/Symbol.text);
  }
}

static WeakHashMap<Symbol, Bool> symbol_map = newWeakHashMap();
endifndef

static Symbol symbol(S s) {
  ifdef SymbolAsString
  ret s;
  endifdef
  ifndef SymbolAsString
  if (s == null) null;
  synchronized(symbol_map) {
    // This is a trick that works because of how WeakHashMap compares keys.
    O e = call(symbol_map, 'getEntry, symbol_Tester(s));
    Symbol sym = e == null ? null : ((WeakReference<Symbol>) e).get();
    if (sym == null)
      symbol_map.put(sym = Symbol(s, false), Bool.TRUE);
    ret sym;
  }
  endifndef
}

static Symbol symbol(CharSequence s) {
  if (s == null) null;
  ifdef SymbolAsString
  ret str(s);
  endifdef
  ifndef SymbolAsString
  if (s instanceof Symbol) ret (Symbol) s;
  if (s instanceof S) ret symbol((S) s);
  ret symbol(str(s));
  endifndef
}

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: 347 / 385
Version history: 6 change(s)
Referenced in: [show references]