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).

ifndef SymbolAsString

static WeakHasherMap<Symbol, Bool> symbol_map = new WeakHasherMap(new Hasher<Symbol>() {
  public int hashCode(Symbol symbol) { ret symbol.text.hashCode(); }
  public bool equals(Symbol a, Symbol b) {
    if (a == null) ret b == null;
    ret b != null && eq(a.text, b.text);
  }
});

endifndef

static Symbol symbol(S s) {
  ifdef SymbolAsString
  ret s;
  endifdef
  ifndef SymbolAsString
  if (s == null) null;
  synchronized(symbol_map) {
    // TODO: avoid object creation by passing the string to findKey
    Symbol symbol = new(s, true);
    Symbol existingSymbol = symbol_map.findKey(symbol);
    if (existingSymbol == null)
      symbol_map.put(existingSymbol = symbol, true);
    ifdef symbol_debug
    if (eqic(s, "Java"))
      print("Symbol for " + s + ": " + symbolToStringFull(existingSymbol);
    endifdef
      
    ret existingSymbol;
  }
  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
}

static Symbol symbol(O o) {
  ret symbol((CharSequence) o);
}

Author comment

Began life as a copy of #1010609

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gjtlkbvenryc, 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: 579 / 1063
Version history: 41 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1012712 - symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol and testers [buggy]
#1031625 - symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol [backup]