Sunday, May 4, 2008

Implementation of My Java7 Wishlist

Last month I blogged about my java7 wishlist regarding collections. There I was redirected to a similar post by Stephen Colebourne and also introduced to an open source project kijaro where others had implemented their wishlists and I could try out mine.

After three weekends I am now pleased to announce that I have implemented my wishlist. My implementation at a branch of kijaro contains a merger of my wishlist and those mentioned by Stephen in his blog. Features implemented are:

  • Array Index access syntax for Lists and Maps
    myList[0] instead of myList.get(0) and
    myMap[“key”] instead of myMap.get(“key”)

  • Concise syntax to initialize and fill Collections
    new HashMap<Integer, String>(5) [1: “one”, 2:”two”, 3:”three”, ]
    instead of
    HashMap<Integer, String> m1 = new HashMap<Integer, String>(5);
    m1.put(1, “one”);
    m1.put(2, “two”);
    m1.put(3, “three”);

There was a catch while implementing the second feature as the expression needed to be converted to another expression and not a series of statements. Hence I needed to make use of a utility method to execute the set of statements to populate a Collection or a Map.


It was really exciting for me to see a .java file with the above syntax compiled into a .class file. Below is an example of such a file and the corresponding jdk4 compliant code it was converted to during the compilation process.

public class TestConcise {
public static void main(String[] args) {
boolean b = new java.util.LinkedList<String>()
["a", "b", "c" ].add("d");
param(new java.util.ArrayList<String>() {
public boolean add(String e) {
return super.add(e);
}
} ["a", "b", "c" ]
);
java.util.Map<Integer, String> m1 = ret();
m1[2] = "two";

java.util.LinkedList<String> l1 = new
java.util.LinkedList<String>()["a", "b", "c" ];
m1[3] = l1[2];
l1[0] = m1[0];
l1[1] = "one";

}

private static void param(
final java.util.Collection<? extends Object> coll){
System.out.println(coll);
}

private static java.util.Map<Integer, String> ret() {
return new java.util.HashMap<Integer, String>() {}
[1:"a", 2:"b", ];
}
}

was converted to



class TestConcise$1 extends java.util.ArrayList {
TestConcise$1() {
super();
}

public boolean add(String e) {
return super.add(e);
}

/*synthetic*/ public boolean add(Object x0) {
return this.add((String)x0);
}
},

class TestConcise$2 extends java.util.HashMap {
TestConcise$2() {
super();
}
},

public class TestConcise {
public TestConcise() {
super();
}

public static void main(String[] args) {
boolean b = java.util.CollectionUtil.fillCollection(
new java.util.LinkedList(),
new String[]{"a", "b", "c"}).add("d");
param(java.util.CollectionUtil.fillCollection(
new TestConcise$1(), new String[]{"a", "b", "c"}));
java.util.Map m1 = ret();
m1.put(Integer.valueOf(2), "two");

java.util.LinkedList l1 =
java.util.CollectionUtil.fillCollection(
new java.util.LinkedList(),
new String[]{"a", "b", "c"});
m1.put(Integer.valueOf(3), l1.get(2));
l1.set(0, m1.get(Integer.valueOf(0)));
l1.set(1, "one");

}

private static void param(
final java.util.Collection coll) {
System.out.println(coll);
}

private static java.util.Map ret() {
return java.util.CollectionUtil.fillMap(
new TestConcise$2(),
new Integer[]{Integer.valueOf(1), Integer.valueOf(2)},
new String[]{"a", "b"});
}
}

--

113 comments :