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

77
LINES

< > BotCompany Repo | #1005583 - getOpt_cached (optimized version of getOpt)

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

Libraryless. Click here for Pure Java version (2056L/13K).

//static final Map<Class, HashMap<S, Field>> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init);

sclass getOpt_Map extends WeakHashMap {
  *() {
    if (getOpt_special == null) getOpt_special = new HashMap;
    clear();
  }
  
  public void clear() {
    super.clear();
    //print("getOpt clear");
    put(Class.class, getOpt_special);
    put(S.class, getOpt_special);
  }
}

static final Map<Class, HashMap<S, Field>> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map));
//static final Map<Class, HashMap<S, Field>> getOpt_cache = _registerWeakMap(synchroMap(new getOpt_Map));
static HashMap getOpt_special; // just a marker

/*static void getOpt_special_init(Map map) {
  map.put(Class.class, getOpt_special);
  map.put(S.class, getOpt_special);
}*/

static Object getOpt_cached(Object o, String field) ctex {
  if (o == null) ret null;

  Class c = o.getClass();
  HashMap<S, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  }
  
  if (map == getOpt_special) {
    if (o instanceof Class)
      return getOpt((Class) o, field);
    /*if (o instanceof S)
      ret getOpt(getBot((S) o), field);*/
    if (o instanceof Map)
      ret ((Map) o).get(field);
  }
    
  Field f = map.get(field);
  if (f != null) ret f.get(o);
  ifclass DynamicObject
    if (o instanceof DynamicObject)
      ret mapGet2(((DynamicObject) o).fieldValues, field);
  endif
  ret null;
}

// used internally - we are in synchronized block
static HashMap<S, Field> getOpt_makeCache(Class c) {
  HashMap<S, Field> map;
  if (isSubtypeOf(c, Map.class))
    map = getOpt_special;
  else {
    map = new HashMap;
    if (!reflection_classesNotToScan().contains(c.getName())) {
      Class _c = c;
      do {
        for (Field f : _c.getDeclaredFields()) {
          makeAccessible(f);
          S name = f.getName();
          if (!map.containsKey(name))
            map.put(name, f);
        }
        _c = _c.getSuperclass();
      } while (_c != null);
    }
  }
  if (getOpt_cache != null) getOpt_cache.put(c, map);
  ret map;
}

Author comment

Began life as a copy of #1001101

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, sawdedvomwva, tslmcundralx, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1005583
Snippet name: getOpt_cached (optimized version of getOpt)
Eternal ID of this version: #1005583/21
Text MD5: f7c03763b659facce6233bd458741a71
Transpilation MD5: f250a0cdc1a5fd30830bcde94f95550d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-31 14:16:27
Source code size: 2194 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 488 / 888
Version history: 20 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1005583 & http://1005583.tinybrain.de