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

87
LINES

< > BotCompany Repo | #1012712 // symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol and testers [buggy]

JavaX fragment (include)

ifndef SymbolAsString
sclass symbol_Tester { // compares by text (case-sensitive)
  Symbol symbol;
  
  *() {}
  *(Symbol *symbol) {}
  
  public int hashCode() { ret symbol.text.hashCode(); }
  public bool equals(O o) {
    if (!o instanceof symbol_Tester) false;
    ret eq(symbol.text, o/symbol_Tester.symbol.text);
  }
  
  toString { ret str(symbol); }
}

sclass symbol_MasterTester { // compares by text (case-insensitive)
  MasterSymbol symbol;
  
  *() {}
  *(MasterSymbol *symbol) {}
  
  public int hashCode() { ret symbol.lowerCase.hashCode(); }
  public bool equals(O o) {
    if (!o instanceof symbol_MasterTester) false;
    ret eq(symbol.lowerCase, o/symbol_MasterTester.symbol.lowerCase);
  }
}

static WeakHashMap<symbol_Tester, Bool> symbol_map = unsyncedWeakHashMap();
static WeakHashMap<symbol_MasterTester, Bool> masterSymbol_map = unsyncedWeakHashMap();
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.
    symbol_MasterTester masterTester = symbol_MasterTester(new MasterSymbol(s, true));
    O e = call(masterSymbol_map, 'getEntry, masterTester);
    symbol_MasterTester existingMaster = e == null ? null : ((WeakReference<symbol_MasterTester>) e).get();
    if (existingMaster == null)
      masterSymbol_map.put(existingMaster = masterTester, true);
    ifdef symbol_debug
    if (eqic(s, "Java"))
      print("existing master for " + s + ": " + existingMaster);
    endifdef
    
    // second lookup after master found
    bool isEq = eq(s, existingMaster.symbol.text);
    Symbol symbol = isEq ? existingMaster.symbol : new Symbol(s, existingMaster.symbol);
    ifdef symbol_debug
    //print("existing master text: " + existingMaster.symbol.text + ", eq=" + isEq + " => " + symbol);
    endifdef
    symbol_Tester tester = symbol_Tester(symbol);
    e = call(symbol_map, 'getEntry, tester);
    symbol_Tester sym = e == null ? null : ((WeakReference<symbol_Tester>) e).get();
    if (sym == null)
      symbol_map.put(sym = tester, true);
    ifdef symbol_debug
    if (eqic(s, "Java"))
      print("Symbol for " + s + ": " + symbolToStringFull(sym.symbol);
    endifdef
      
    ret sym.symbol;
  }
  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 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012712
Snippet name: symbol - make Symbol from String or CharSequence - case-insensitive version with MasterSymbol and testers [buggy]
Eternal ID of this version: #1012712/1
Text MD5: 1962d0000fa8112069133ab81e4fec99
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-10 20:42:05
Source code size: 2734 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 399 / 435
Referenced in: [show references]