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

85
LINES

< > BotCompany Repo | #1005583 // getOpt_cached (optimized version of getOpt), getOpt_makeCache

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (10036L) is out of date.

1  
//static final Map<Class, HashMap<S, Field>> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init);
2  
3  
sclass getOpt_Map extends WeakHashMap {
4  
  *() {
5  
    if (getOpt_special == null) getOpt_special = new HashMap;
6  
    clear();
7  
  }
8  
  
9  
  public void clear() {
10  
    super.clear();
11  
    //print("getOpt clear");
12  
    put(Class.class, getOpt_special);
13  
    put(S.class, getOpt_special);
14  
  }
15  
}
16  
17  
static final Map<Class, HashMap<S, Field>> getOpt_cache = 
18  
  ifdef AssumeNoClassUnloading
19  
    syncHashMap();
20  
  endifdef
21  
  ifndef AssumeNoClassUnloading
22  
    _registerDangerousWeakMap(synchroMap(new getOpt_Map));
23  
  endifndef
24  
  
25  
static HashMap getOpt_special; // just a marker
26  
27  
/*static void getOpt_special_init(Map map) {
28  
  map.put(Class.class, getOpt_special);
29  
  map.put(S.class, getOpt_special);
30  
}*/
31  
32  
static Map<S, Field> getOpt_getFieldMap(O o) {
33  
  Class c = _getClass(o);
34  
  HashMap<S, Field> map = getOpt_cache.get(c);
35  
  if (map == null)
36  
    map = getOpt_makeCache(c);
37  
  ret map;
38  
}
39  
40  
static Object getOpt_cached(Object o, String field) ctex {
41  
  if (o == null) ret null;
42  
43  
  Map<S, Field> map = getOpt_getFieldMap(o);
44  
45  
  if (map == getOpt_special) {
46  
    if (o instanceof Class)
47  
      return getOpt((Class) o, field);
48  
    /*if (o instanceof S)
49  
      ret getOpt(getBot((S) o), field);*/
50  
    if (o instanceof Map)
51  
      ret ((Map) o).get(field);
52  
  }
53  
    
54  
  Field f = map.get(field);
55  
  if (f != null) ret f.get(o);
56  
  ifclass DynamicObject
57  
    if (o instanceof DynamicObject)
58  
      ret syncMapGet2(((DynamicObject) o).fieldValues, field);
59  
  endif
60  
  ret null;
61  
}
62  
63  
// used internally - we are in synchronized block
64  
static HashMap<S, Field> getOpt_makeCache(Class c) {
65  
  HashMap<S, Field> map;
66  
  if (isSubtypeOf(c, Map.class))
67  
    map = getOpt_special;
68  
  else {
69  
    map = new HashMap;
70  
    if (!reflection_classesNotToScan().contains(c.getName())) {
71  
      Class _c = c;
72  
      do {
73  
        for (Field f : _c.getDeclaredFields()) {
74  
          makeAccessible(f);
75  
          S name = f.getName();
76  
          if (!map.containsKey(name))
77  
            map.put(name, f);
78  
        }
79  
        _c = _c.getSuperclass();
80  
      } while (_c != null);
81  
    }
82  
  }
83  
  if (getOpt_cache != null) getOpt_cache.put(c, map);
84  
  ret map;
85  
}

Author comment

Began life as a copy of #1001101

download  show line numbers  debug dex  old transpilations   

Travelled to 19 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, veepotesqksf, vouqrxazstgt, whxojlpjdney

No comments. add comment

Snippet ID: #1005583
Snippet name: getOpt_cached (optimized version of getOpt), getOpt_makeCache
Eternal ID of this version: #1005583/32
Text MD5: e020db79a7b00ae589b9fb2e725e3854
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-31 19:45:16
Source code size: 2269 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 794 / 1436
Version history: 31 change(s)
Referenced in: [show references]