How to store predicates in configuration file in java -
i have list
of hashmap<string, real>
. want access each element on list, apply predicate
on , add together element list if predicate returns true.
for illustration suppose have list of hashmap - {(a, 15), (b, 17), (c, 12)}, {(a, 12), (b, 13), (d, 90)}
. predicate a > 7 & b < 15
. apply predicate on each element of list , find sec element satisfy predicate. applying predicate should easy have access element , compare value values supplied predicate.
the part stuck how save such predicate
in configuration
file, such can converted class , new predicates can added without fiddling core code.
one way utilize nashorn
javascript engine. write java script validation function each predicate, , store in java script file.
function pred1(a,b) { if(a>7 && b<15) homecoming true; else homecoming false; }
create , load predicate file. need mention predicate file paths in configuration file, , lookup paths configuration file.
properties property = new properties(); property.load(configuration file path goes here);
sample configuration
file property:
predicatefilelist:test1.js,...
get property,
string[] filepaths = ((string)property.get("predicatefilelist")).split(",");
initialize , utilize script engine.
scriptenginemanager mill = new scriptenginemanager(); scriptengine engine = factory.getenginebyname("nashorn"); // utilize filepaths here instead of file name. scriptobjectmirror s = (scriptobjectmirror)engine.eval("load(\"test1.js" + "\");");
for each map
in list
, phone call predicate function
passing arguments
predicate
function:
list.foreach(map->{ system.out.println(s.call(map.get("a"), m.get("b"))); });
java
No comments:
Post a Comment