Java: How to aggregate (min,max,avg) of collection element attributes with an expression language approach? -
i looking simple way aggregate functions on java collections determine e.g. minimum price of collection of products. don't want in pure java, kind of dsl / scripting / expression language can entered user , needs simple possible.
assume have following object structure:
product: id: product1 offers: [offer1, offer2] offer1: id: offer1 data: price: 10.99 shipcost: 3.99 offer2: id: offer2 data: price: 5.99 shipcost: 3.99  in example above end result this:
minpriceofproduct1 = 5.99  now user of application can display list of products. each product wants minimum price minimum of offers. user not have access underlying datastore, sql not option. thing have java objects. user use kind of expression language exress this.
currently have ability apply snippet of freemarker code each product data or bit more compute new values based on attributes this:
<#if (item.isproduct() && item.offers??) > <#assign offerminprice = -1> <#list item.offers o> <#if (offerminprice == -1 || ( o.data.pricecents?? && o.data.pricecents < offerminprice ) )> <#assign offerminprice=o.data.pricecents! > </#if> </#list> <#if offerminprice != -1> ${offerminprice} <#else> ${pricecents} </#if> <#else> ${pricecents!} </#if>  this works ugly code makes not brain bleed. i'd rather have kind of simpler expression language approach this:
minoffersprice = min(product.offers.data.price)  this looks simpler user , should same aggregation under hood.
what approach come mind? searching web following things come mind:
- mvel expression language http://mvel.codehaus.org/mvel+2.0+operators (does not seem solve aggregate functions)
 - josql http://josql.sourceforge.net/
 - things lambda expressions or closures
 - rhino javascript in java
 - some manual parsing in combination java reflections or apache commons beanutils
 
thanks christoph
lambdaj library addresses using plain java api: https://code.google.com/p/lambdaj/wiki/lambdajfeatures
person maxageperson = selectmax(personslist, on(person.class).getage() );  where selectmax , on static imports lambda class.
Comments
Post a Comment