Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 75505 invoked from network); 21 Dec 2004 02:38:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 21 Dec 2004 02:38:31 -0000 Received: (qmail 37951 invoked by uid 500); 21 Dec 2004 02:38:19 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 37905 invoked by uid 500); 21 Dec 2004 02:38:18 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 37886 invoked by uid 99); 21 Dec 2004 02:38:18 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: neutral (hermes.apache.org: local policy) Received: from ms06.mse1.mailstreet.com (HELO ms06.mse1.mailstreet.com) (69.25.50.147) by apache.org (qpsmtpd/0.28) with ESMTP; Mon, 20 Dec 2004 18:38:15 -0800 X-MimeOLE: Produced By Microsoft Exchange V6.5.6944.0 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: Modify getter result Date: Mon, 20 Dec 2004 21:38:06 -0500 Message-ID: <8E2D018A78A5A04095667ADF098FF29A03DEAFC2@ms06> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Modify getter result Thread-Index: AcTmnV/DXXWu2tVdRfqJkH0LKHoG4wAZx/cg From: "Tim O'Brien" To: "Jakarta Commons Users List" X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Stephen already responded, but here is the code you would need: import org.apache.commons.collections.Factory; import org.apache.commons.collections.functors.ConstantFactory; import org.apache.commons.collections.map.LazyMap; Import java.util.*; Map currency =3D new HashMap() { { put("DE", "EUR"); put("US", "USD"); } }; Factory factory =3D new ConstantFactory("XY"); Map lazy =3D Lazy.map(currency, factory); String blah =3D (String) lazy.get("XY");=20 ConstantFactory is just a factory which returns a constant Object, in this case a request for "XY" would return the default value. If you wanted to add your logging code, you could do this instead: Factory factory =3D new Factory() { public Object create() { // logging stuff goes here.... return "default"; } } But, you should know that after you get "XY", the Map will then contain "XY" as a key. If you wanted do this without side-effects (i.e. without adding a value to the Map), you could mess around with decorating the original Map with the FixedSizeMap, but the problem here is that adding a new key will throw IllegalArgumentException. Tim O'Brien > -----Original Message----- > From: Nentwig, Timo [mailto:timo.nentwig@jamba-ag.de]=20 > Sent: Monday, December 20, 2004 8:08 AM > To: commons-user@jakarta.apache.org > Subject: Modify getter result >=20 > Hi! >=20 > I'm not sure whether this can be done with commons-collections.=20 >=20 > I need to hook in a collection and return a default value=20 > when the requested key does not exist in the collection. E.g. >=20 > Map currency =3D new HashMap() > { > { > put("DE", "EUR"); > put("US", "USD"); > } > }; >=20 > And currency.get("XY") is supposed to return a default value=20 > (this may require additional logic and e.g. may write to the=20 > logger that an yet unsupported ISOCODE was requested). >=20 > Can this be done? >=20 > Kind regards > Timo >=20 > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org >=20 >=20 >=20 --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org