You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Christopher Lenz <cm...@gmx.de> on 2002/09/05 14:30:19 UTC

[collections] [PATCH] Adding singleton List, Set & Map

Hi all,

attached is a patch that introduces three new adapters to collections. 
These are implementations of the interfaces Set, List and Map that wrap 
around a single object (or a single key/value pair for the Map), similar 
to how SingletonIterator and SingletonListIterator work.

I've added them as package private static inner classes to the 
corresponding *Utils classes, so you have;

   Set SetUtils.singletonSet(Object o);
   List ListUtils.singletonList(Object o);
   Map MapUtils.singletonMap(Object key, Object value);

Motivation: I've encountered many situations where I need to stuff a 
single object into a collection just because some interface wants a 
collection (as argument or return value or whatever). These adapter 
classes provide an easy way to satisfy such interfaces without paying 
the extra overhead of using "real" collections.

Unit tests are included.

Thoughts?
-- 
Christopher Lenz
/=/ cmlenz at gmx.de

Re: [collections] [PATCH] Adding singleton List, Set & Map

Posted by Christopher Lenz <cm...@gmx.de>.
Michael A. Smith wrote:
> Christopher Lenz wrote:
> 
>> Hi all,
>>
>> attached is a patch that introduces three new adapters to collections. 
>> These are implementations of the interfaces Set, List and Map that 
>> wrap around a single object (or a single key/value pair for the Map), 
>> similar to how SingletonIterator and SingletonListIterator work.
> 
> [snip]
> 
>> Thoughts?
> 
> 
> Singleton for a set has existed in the JDK since 1.2:
> java.util.Collections#singleton(Object)
> 
> Singletons for List and Set have existed in the JDK since 1.3:
> java.util.Collections#singletonList(Object)
> java.util.Collections#singletonMap(Object,Object)

<blush>
Whoa, I've completely missed those! I can't believe this :P

> In the past we've tried avoiding duplicating functionality provided in 
> the JDK because the collections is supposed to "fill the holes" that Sun 
> left, not duplicate the stuff they had already implemented.  Since two 
> of these only exist in 1.3, and we try to target 1.2, maybe we should 
> revisit the issue?

It might be a good idea, a lot of projects are still targetting 1.2.

(note me not closing the blush tag ;-))

-- 
Christopher Lenz
/=/ cmlenz at gmx.de


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [collections] [PATCH] Adding singleton List, Set & Map

Posted by "Michael A. Smith" <ma...@apache.org>.
Christopher Lenz wrote:
> Hi all,
> 
> attached is a patch that introduces three new adapters to collections. 
> These are implementations of the interfaces Set, List and Map that wrap 
> around a single object (or a single key/value pair for the Map), similar 
> to how SingletonIterator and SingletonListIterator work.
[snip]
> Thoughts?

Singleton for a set has existed in the JDK since 1.2:
java.util.Collections#singleton(Object)

Singletons for List and Set have existed in the JDK since 1.3:
java.util.Collections#singletonList(Object)
java.util.Collections#singletonMap(Object,Object)

In the past we've tried avoiding duplicating functionality provided in 
the JDK because the collections is supposed to "fill the holes" that Sun 
left, not duplicate the stuff they had already implemented.  Since two 
of these only exist in 1.3, and we try to target 1.2, maybe we should 
revisit the issue?

regards,
michael


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [collections] [PATCH] Adding singleton List, Set & Map

Posted by Tomasz Pik <pi...@ais.pl>.
Christopher Lenz wrote:

> Hi all,
> 
> attached is a patch that introduces three new adapters to collections. 
> These are implementations of the interfaces Set, List and Map that wrap 
> around a single object (or a single key/value pair for the Map), similar 
> to how SingletonIterator and SingletonListIterator work.
> 
> I've added them as package private static inner classes to the 
> corresponding *Utils classes, so you have;
> 
>   Set SetUtils.singletonSet(Object o);
>   List ListUtils.singletonList(Object o);
>   Map MapUtils.singletonMap(Object key, Object value);


Is it not the same as in java.util.Collections:

  public static java.util.Set singleton(java.lang.Object);
  public static java.util.List singletonList(java.lang.Object);
  public static java.util.Map singletonMap(java.lang.Object,
     java.lang.Object)


Regards
Tomek Pik
pikus@ais.pl

 > --
 > Christopher Lenz
 > /=/ cmlenz at gmx.de




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>