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)

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);
  }
});

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

endifndef

static Symbol symbol(S s) {
  ifdef SymbolAsString
  ret s;
  endifdef
  ifndef SymbolAsString
  if (s == null) null;
  synchronized(symbol_map) {
    MasterSymbol master = new MasterSymbol(s, true);
    MasterSymbol existingMaster = masterSymbol_map.findKey(master);
    if (existingMaster == null)
      masterSymbol_map.put(existingMaster = master, true);
    ifdef symbol_debug
    if (eqic(s, "Java"))
      print("existing master for " + s + ": " + existingMaster);
    endifdef
    
    // directly return master if exact match
    bool isEq = eq(s, existingMaster.text);
    if (isEq) ret existingMaster;
    
    // second lookup after master found
    Symbol symbol = new Symbol(s, existingMaster);
    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 #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: 59 / 81
Referenced in: [show references]