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_map = unsyncedWeakHashMap(); static WeakHashMap 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) 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) 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); }