publicclassCollectionFactory{staticbooleanuseTrove=true;/** * Return a hashmap based on the properties */publicstaticMapgetHashMap(){if(useTrove)returnnewTHashMap();elsereturnnewHashMap();}/** * Return a hashset based on the properties */publicstaticSetgetHashSet(){if(useTrove)returnnewTHashSet();elsereturnnewHashSet();}/** * Return a linkedlist based on the properties */publicstaticListgetLinkedList(){if(useTrove)returnnewTLinkedList();elsereturnnewLinkedList();}}
publicstaticvoidmain(String[]args){char[]foo=newchar[]{'a','b','c'};char[]bar=newchar[]{'a','b','c'};TCustomHashMap<char[],String>ch=newTCustomHashMap<char[],String>(newCharArrayStrategy());ch.put(foo,"John");ch.put(bar,"Tom");}classCharArrayStrategyimplementsHashingStrategy<char[]>{publicintcomputeHashCode(char[]c){// use the shift-add-xor class of string hashing functions// cf. Ramakrishna and Zobel, "Performance in Practice// of String Hashing Functions"inth=31;// seed chosen at randomfor(inti=0;i<c.length;i++){// could skip invariantsh=h^((h<<5)+(h>>2)+c[i]);// L=5, R=2 works well for// ASCII input}returnh;}publicbooleanequals(char[]c1,char[]c2){if(c1.length!=c2.length){// could drop this check for fixed-length// keysreturnfalse;}for(inti=0,len=c1.length;i<len;i++){// could skip// invariantsif(c1[i]!=c2[i]){returnfalse;}}returntrue;}}