commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <niall.pember...@gmail.com>
Subject Re: [beanutils] Mixing static and dynamic properties in the same DynaBean
Date Mon, 20 Mar 2006 14:36:30 GMT
On 3/19/06, Bruce Alspaugh <compulinkltd@gmail.com> wrote:
> Newbie here.  Is it possible to define a DynaBean that has both dynamic
> properties that are lazily added at runtime as well as static properties
> that are defined at compile time?

Its possible to define it that way, but the rest of BeanUtils won't
recoginze the "static" properties. So effectively the answer is no. To
achieve this using DynaBeans you would need something that caculates
these values and stores them in the DynaBean as actual values.

Niall

> For example, I would like to be able to do something like this:
>
>    DynaBean order = new LazyDynaBean()  {
>        public double getTotal() {
>            double price = ((Double)get("price")).doubleValue();
>            double quantity = ((Double)get("quantity")).doubleValue();
>            return price * quantity;
>        }
>    };
>
> And then access the properties this way:
>
>    PropertyUtils.setSimpleProperty(order, "price", Double.valueOf(5.0));
>    System.out.println("Price: " +
>            PropertyUtils.getSimpleProperty(order, "price"));
>
>    PropertyUtils.setSimpleProperty(order, "quantity", Double.valueOf(10.0
> ));
>    System.out.println("Quantity: " +
>            PropertyUtils.getSimpleProperty(order, "quantity"));
>
>    System.out.println("Total: " +
>            PropertyUtils.getSimpleProperty(order, "total"));
>
> The output is:
>
> Price:  5.0
> Quantity:  10.0
> Total: null
>
> How do I get BeanUtils to use my getTotal() method to output the product of
> price times quantity?
>
> Ultimately, I would like to get the dynamic properties from a JDBC result
> set, and use the statically defined properties to compute totals and the
> like for the dynamic properties.
>
> - Bruce

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message