commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nentwig, Timo" <timo.nent...@jamba-ag.de>
Subject AW: [collections] Modify getter result
Date Tue, 21 Dec 2004 08:24:44 GMT
"When the get(Object) method is called with a key that does not exist in the map, the factory
is used to create the object. The created object will be added to the map using the requested
key."

Well, this would do the trick but isn't quite what I am looking it though.

After calling

	currency.get("AB");
	currency.get("BC");

I would have two new item in the HashMap, AB and BC.

Consider this:

Factory factory = new Factory() {
     public Object create() {
	   LOG.error("non-existing object was requested at " + System.currentTimeMillis());
         return new Date();
     }
 }
 Map lazy = Lazy.map(new HashMap(), factory);
 Object obj = lazy.get("AB");
 wait();
 Object obj2 = lazy.get("BC");
 wait();
 Object obj = lazy.get("AB");

Getting AB the second time would actually return the object created in first instance. Consider
that the Map may likely be filled by data from a persistence store which likely must be updated
manually and cannot or must not be done in create().

-----Urspr√ľngliche Nachricht-----
Von: Stephen Colebourne [mailto:scolebourne@btopenworld.com] 
Gesendet: Montag, 20. Dezember 2004 21:17
An: Jakarta Commons Users List
Betreff: Re: [collections] Modify getter result

See LazyMap.
Stephen

----- Original Message -----
From: "Nentwig, Timo" <timo.nentwig@jamba-ag.de> I'm not sure whether this can be done
with commons-collections. 

I need to hook in a collection and return a default value when the requested key does not
exist in the collection. E.g.

Map currency = new HashMap()
{
{
put("DE", "EUR");
put("US", "USD");
}
};

And currency.get("XY") is supposed to return a default value (this may require additional
logic and e.g. may write to the logger that an yet unsupported ISOCODE was requested).

Can this be done?

Kind regards
Timo

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




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


---------------------------------------------------------------------
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