Friday, November 8, 2013

syntax highlight template

syntax highlight

$(document).bind('click', function(e) {
            var $clicked = $(e.target);
            if (! $clicked.parents().hasClass("staticMenu")){
                $(".staticMenu dd ul").hide();
                $(".staticMenu dt a").removeClass("selected");
            }        });

Java generics

Java introduced generics, but then I realized that it does not have the equivalent of typedef as C/C++. So if you have something like

Map<String, Future<MyLongClass>> mp = new HashMap<String, Future<MyLongClass>>();

 
this is tedious and annoying, why do I have to type the same thing twice? In java 7, you can write this

Map<String, Future<MyLongClass>> mp = new HashMap<>();

For java 6 and earlier, here is one solution, which seems to be the approach of google guava library as well.

http://www.ibm.com/developerworks/library/j-jtp02216/

public class Util {
  static Map newHashMap() {
    return new HashMap();
  }
}

Here is a small test file
// UtilTest.java
import java.util.HashMap;
import java.util.Map;

public class UtilTest {
    public static void main(String[] args) {
        Pair<Integer, String> p1 = new Pair<>(1, "apple");
        Pair<Integer, String> p2 = new Pair<>(2, "pair");
        boolean same = Util.<Integer, String>compare(p1, p2);
        System.out.println(same);

        Map<String, String> hashMap = Util.newHashMap();
        hashMap.put("key", "value");
        for (Map.Entry<String, String> entry : hashMap.entrySet()) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
    }
}