[collections] Composable Map?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[collections] Composable Map?

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

I have a need for the following Map implementation:

Given 2 (or more) source Maps, iteratively fetch the value for a key
from each of the composed Maps, returning the first non-null value (or,
at if you prefer, the first value for which the key is set). This is
intended to work essentially as a Map which can override some default
values (from a shared Map).

I first tried to use CompositeMap, since it looked like exactly what I
was looking for. I ended up with an IllegalArgumentException since some
keys are defined in both Maps. IMO, the class documentation doesn't make
it clear that the keys most be unique across the source Maps (though the
documentation for the 2-Map constructor does an exception can be thrown
if there is a "key collision").

Next I looked at DefaultedMap, but that provides a single default value
for undefined keys on a single Map.

I poked-around the javadoc but couldn't find anything that looked like
it fit my needs. Am I missing something? Am I looking for something that
is so trivial that nobody has bothered writing it?

Yes, I could simply do this:

Map composedMap = new HashMap();
composedMap.addAll(defaultValuesMap);
composedMap.addAll(overriddenValuesMap);

... but what fun is that?

Thanks,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqCIn0ACgkQ9CaO5/Lv0PDcJwCdEuvCDXKSnDlwxhmRPJvIQ1F7
/NAAnR5F7suSDzeo+/bk8616TfagkAp4
=GS99
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [collections] Composable Map?

Ralph Goers
Is there something wrong with using java.util.Properties? Just do

Properties defaults = new Properties();
.
.
Properties map = new Properties(defaults);

Ralph

On Aug 11, 2009, at 7:01 PM, Christopher Schultz wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> All,
>
> I have a need for the following Map implementation:
>
> Given 2 (or more) source Maps, iteratively fetch the value for a key
> from each of the composed Maps, returning the first non-null value  
> (or,
> at if you prefer, the first value for which the key is set). This is
> intended to work essentially as a Map which can override some default
> values (from a shared Map).
>
> I first tried to use CompositeMap, since it looked like exactly what I
> was looking for. I ended up with an IllegalArgumentException since  
> some
> keys are defined in both Maps. IMO, the class documentation doesn't  
> make
> it clear that the keys most be unique across the source Maps (though  
> the
> documentation for the 2-Map constructor does an exception can be  
> thrown
> if there is a "key collision").
>
> Next I looked at DefaultedMap, but that provides a single default  
> value
> for undefined keys on a single Map.
>
> I poked-around the javadoc but couldn't find anything that looked like
> it fit my needs. Am I missing something? Am I looking for something  
> that
> is so trivial that nobody has bothered writing it?
>
> Yes, I could simply do this:
>
> Map composedMap = new HashMap();
> composedMap.addAll(defaultValuesMap);
> composedMap.addAll(overriddenValuesMap);
>
> ... but what fun is that?
>
> Thanks,
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkqCIn0ACgkQ9CaO5/Lv0PDcJwCdEuvCDXKSnDlwxhmRPJvIQ1F7
> /NAAnR5F7suSDzeo+/bk8616TfagkAp4
> =GS99
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [collections] Composable Map?

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ralph,

On 8/12/2009 2:43 AM, Ralph Goers wrote:
> Is there something wrong with using java.util.Properties? Just do
>
> Properties defaults = new Properties();
> .
> .
> Properties map = new Properties(defaults);

This is definitely possible, but Properties is Hashtable-based so it
includes synchronization (which I don't need) and requires the use of
String values, which I don't always want.

Any other suggestions?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkqC0TAACgkQ9CaO5/Lv0PDlRQCeOj9fwrG2UJcPKBJtY+tvwzoA
R8cAn0NeQgIaIMU1EJBttJvsQ8vd3gMl
=GK3q
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [collections] Composable Map?

ipsi
In reply to this post by Ralph Goers
You could try looking into Commons Configuration, and use something
like CombinedConfiguration. Though if you need actual objects to be
the values (as opposed to Strings/Primitives) then I wouldn't
bother... Apart from that, I can't think of anything, sorry.

- Andrew

On Wed, Aug 12, 2009 at 6:43 PM, Ralph Goers<[hidden email]> wrote:

> Is there something wrong with using java.util.Properties? Just do
>
> Properties defaults = new Properties();
> .
> .
> Properties map = new Properties(defaults);
>
> Ralph
>
> On Aug 11, 2009, at 7:01 PM, Christopher Schultz wrote:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> All,
>>
>> I have a need for the following Map implementation:
>>
>> Given 2 (or more) source Maps, iteratively fetch the value for a key
>> from each of the composed Maps, returning the first non-null value (or,
>> at if you prefer, the first value for which the key is set). This is
>> intended to work essentially as a Map which can override some default
>> values (from a shared Map).
>>
>> I first tried to use CompositeMap, since it looked like exactly what I
>> was looking for. I ended up with an IllegalArgumentException since some
>> keys are defined in both Maps. IMO, the class documentation doesn't make
>> it clear that the keys most be unique across the source Maps (though the
>> documentation for the 2-Map constructor does an exception can be thrown
>> if there is a "key collision").
>>
>> Next I looked at DefaultedMap, but that provides a single default value
>> for undefined keys on a single Map.
>>
>> I poked-around the javadoc but couldn't find anything that looked like
>> it fit my needs. Am I missing something? Am I looking for something that
>> is so trivial that nobody has bothered writing it?
>>
>> Yes, I could simply do this:
>>
>> Map composedMap = new HashMap();
>> composedMap.addAll(defaultValuesMap);
>> composedMap.addAll(overriddenValuesMap);
>>
>> ... but what fun is that?
>>
>> Thanks,
>> - -chris
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.9 (MingW32)
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iEYEARECAAYFAkqCIn0ACgkQ9CaO5/Lv0PDcJwCdEuvCDXKSnDlwxhmRPJvIQ1F7
>> /NAAnR5F7suSDzeo+/bk8616TfagkAp4
>> =GS99
>> -----END PGP SIGNATURE-----
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]