Hi,
On Mon, May 4, 2009 at 3:17 PM, Thomas Müller <[email protected]> wrote:
> I propose to add a new 'generics generator class' called 'New' to
> jackrabbit-jcr-commons. It would contain constructor methods for the
> most commonly used collection objects.
>
> public class New {
> public static <K, V> HashMap<K, V> hashMap() {
> return new HashMap<K, V>();
> }
> public static <T> ArrayList<T> arrayList() {
> return new ArrayList<T>();
> }
> public static <T> WeakReference<T> weakReference(T t) {
> return new WeakReference<T>(t);
> }
> ...
> }
>
> What do you think? Are there better solutions?
I like the idea, removing clutter is good.
A related common annoyance I run into is about creating an initialized
collection:
List<String> abc = Arrays.asList(new String[] { "a", "b", "c" });
or
List<String> abc = new ArrayList() {{ add("a"); add("b"); add("c"); }};
This would be quite a bit more convenient with a utility method like this:
List<String> abc = New.list("a", "b", "c");
Felix has a point about the drawbacks of such utility code, but in
this case I tend to think that it's probably worth it. The potential
savings in extra typing/reading are probably quite a bit larger than
the amount of extra utility code needed.
PS. This might also be a good idea for inclusion in the upcoming
Commons Lang 3.0 version that'll be based on Java 5.
BR,
Jukka Zitting