commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitri Plotnikov <dplot...@yahoo.com>
Subject Re: [JXPath] Write my own sumBigDecimal()-function
Date Tue, 28 Jan 2003 18:45:04 GMT

Andreas,

Here's how the extension function you need can be written:

public class BugDecimalMath {
  public static BigDecimal sumBigDecimal(Collection data) {
    BigDecimal sum = new BigDecimal("0");
    Iterator iter = data.iterator();
    while (iter.hasNext()) {
      Object obj = iter.next();
      if (obj instanceof BigDecimal) {
         sum = sum.add((BigDecimal) obj);
      }
    } 
    return sum;
  }
}

Here's how it is registered:

  context.setFunctions(
    new ClassFunctions(BugDecimalMath .class, "myf"));

If you have several classes implementing different extension functions,
you might want to look into using a FunctionLibarary.

Let me know if this does not work for some reason.

- Dmitri


--- Dmitri Plotnikov <dplotnik@yahoo.com> wrote:
> Andreas,
> 
> You should try to use an ExtensionFunction.  What exactly is the
> issue
> with iteration?
> 
> - Dmitri
> 
> 
> --- "Geerling, Andreas" <andreas.geerling@sap.com> wrote:
> > I am trying to write a function sumBigDecimal() similar to the
> sum()
> > function. 
> > First I tried to create a ExtensionFunction, but i have the problem
> > to iterate the values.
> > 
> > Only with Java, i would do it like this:
> > 
> > public static BigDecimal sumBigDecimal(JXPathContext context,String
> > xpath) {
> > 	Object obj = null;
> > 	BigDecimal sum = new BigDecimal("0");
> > 	Iterator iter = context.iterate(xpath);
> > 	while (iter.hasNext()) {
> > 		obj = iter.next();
> > 		if (obj instanceof BigDecimal) {
> > 			sum = sum.add((BigDecimal) obj);
> > 		}
> > 	} return sum;
> > }
> > 
> > But I would like to use it "within" JXPath as a Function. 
> > Examples:
> > 
> > BigDecimal myBetrag = (BigDecimal) context.getValue(
> > 
> >
>
"myf:sumBigDecimal(positionen[freieAttribute/warengruppe='AA']/vbetrag)");
> > 
> > String artikelbezeichnung = (String) context.getValue(
> >      "positionen[sumBigDecimal(vbetrag) >
> $minBetrag]/bezeichnung)");
> > 
> > Can someone help me?
> > 
> > Andreas Geerling
> > 
> > 
> > --
> > To unsubscribe, e-mail:  
> > <mailto:commons-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:commons-user-help@jakarta.apache.org>
> > 
> 
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
> 


__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com

Mime
View raw message