Sunday, June 04, 2006

Stupid Tiger Tricks

[EDIT 22-09-2006: There is a bug on the mapWith code below. I explain what happened here.]

One of the things that bugs me about Java is that it lacks literal constructs for creating lists and maps*. This, coupled with the profusion of casts, can lead to APIs prefering arrays over collections, which are otherwise much easier to handle. Java 5 (Tiger) brought generics, helping to minimize all those annoying casts, but the verbosity for initializing collections is still an issue. Here is the result of a quick hack to try and improve things a bit:

public class Web2dot0 {
private List<String> buzzwords =
listWith("mashup", "AJAX", "bottom-up", "tagging");
private Map<Integer, String> strategy =
mapWith(pair(1, "Pretty AJAX UI"), pair(3, "profit!"));
...
}

This was implemented using an utility class with the following static members:

public class CollectionUtils {
public static <E> List<E> listWith(E... items) {
return Arrays.asList(items);
}
public static <K, V> Map<K, V> mapWith(Map.Entry<K, V>... entries) {
HashMap<K, V> map = new HashMap<K, V>();
for (Map.Entry<K, V> eachEntry : entries)
map.put(eachEntry.getKey(), eachEntry.getValue());
return map;
}
public static <K, V> Map.Entry<K, V> pair(K key, V val) {
return new ImmutablePair<K, V>(key, val);
}
public static final class ImmutablePair<K, V> implements Map.Entry<K,V> {...}
...
}

The listWith(), mapWith() and pair() methods were then static imported in the client class.

* And now the geniouses at the JCP want to create XML literals. That reminds me of a comment I read in Taenembaun's computer organization book, saying that if programmers could have their way in the chip design process, opperations like branchAndDoPayroll would be part of the ISA.

4 comments:

Unknown said...

In my opinion the Oris TT1 Worldtimer is the best rolex replica in this series. Recently Oris teamed up with the Williams F1 Team to produce the Oris Williams F1 Team sports watch. I'm not a tremendous fan of this rolex replica uk. Call me a snob, but I wouldn't want a watch I owned to be known by any other brand except the watch brand. Also, I think if you are looking for a sports watch they are other brands of rolex replica that offer better styling. The Oris Big Crown Collection belongs to their aviation series. The big crown being a reminder of the early days of aviation when pilots had to adjust their rolex replica uk whilst wearing thick gloves. This collection of watches is more like what I'd expect from Oris. The Oris Big Crown comes with mechanical automatic movement with hublot replica uk of hours and minutes from the centre, small permanent second at replica watches sale, decentralised minute and hour counters, second counter from the centre, date at three o’clock.

opbest said...


Hello, i think that i saw you visited my website thus i came to “return the favor”.
I’m trying to find things to improve my website!I suppose its ok to use some of your ideas!!

Have a look at my homepage; 오피

(freaky)

koe61831 said...

Until recently, the content correctness of the information contained in the resume was the most important, and now it is the same, but fortunately there is this site https://www.builtinnyc.com/event/why-well-written-cv-so-important/111644, with information about why the resume is so important

Amy Jhons said...

Encontrar un abogado de divorcio en Charlottesville es significant para enfrentar este proceso con confianza. Busque un profesional con experiencia en derecho de familia, reputación sólida y empatía hacia su situación. Estos abogados manejan procedimientos legales complejos, asegurando la protección de sus derechos en divisiones de bienes, custodia de hijos y negociaciones de manutención. Elija un abogado con historial de casos exitosos y atención personalizada al cliente para asegurar que sus intereses estén representados con habilidad y dedicación.Divorcio Abogado Charlottesville