java - Efficient way of mimicking hibernate criteria on cached map -
i have wrote code cach table in memory (simple java hashmap). 1 of code trying replace find objects based on criteria. receives multiple field parameters , if fields not empty , not null, being added part of hibernate query criteria.
to replace this, thinking is
- for each valid param (not null , no empty) create hashset satisfy criteria.
- once done making hashsets valid criteria, call
set.retainall(second_set)on sets. @ end, have set intersection of valid criteria.
does sound best approach or there better way implement ?
edit
though, original post still valid , looking answer. ended implementing in following way. reason kind cumbersome sets since after creating sets, had first figure out set non empty retainall called. resulting in lots of if-else statements. current implementation this
private list<myobj> getcachedobjs(long criteria1, string criteria2, string criteria3) { list<myobj> results = new arraylist<>(); int totalactivefilters = 0; if (criteria1 != null){ totalactivefilters++; } if (!stringutil.isblank(criteria2)){ totalactivefilters++; } if (!stringutil.isblank(criteria3)){ totalactivefilters++; } (map.entry<long, myobj> objentry : objcache.entryset()){ myobj obj = objentry.getvalue(); int matchedfilters = 0; if (criteria1 != null) { if (obj.getcriteria1().equals(criteria1)) { matchedfilters++; } } if (!stringutil.isblank(criteria2)){ if (obj.getcriteria2().equals(criteria2)){ matchedfilters++; } } if (!stringutil.isblank(criteria3)){ if (game.getcriteria3().equals(criteria3)){ matchedfilters++; } } if (matchedfilters == totalactivefilters){ results.add(obj); } } return results; }
Comments
Post a Comment