Java

A few quick tips I have discovered with JAva Maps:

 

Use ConcurrentHAshMap rather than HashMap for multi-threaded applications

Use Collections.checkedMap(new Map(), key.class, value.class)  to ensure that you don't add in the wrong type of item.  The generics implementation currently allows you to add any old object.  The type safe wrapper will cause a classCastException to be thrown.

This wrapper class will also ensure that gets are checked too:

import java.util.Collection;

import java.util.Collections;

import java.util.Map;

import java.util.Set;

 

/**

* This wrapper class uses {@link Collections#checkedMap(Map, Class, Class)} to

* ensure puts are correct but also checks that gets are the correct class too

*

* @author Steve Leonard

*

* @param

* @param

*/

public class SafeMap implements Map {

private Map realMap;

private Class keyClass;private Class valueClass;

 

@Override

public String toString() {return realMap.toString();

}

 

/**

* This wrapper class uses {@link Collections#checkedMap(Map, Class, Class)}

* to ensure puts are correct but also checks that gets are the correct

* class too

*

* @param map

* @param keyClass

* @param valueClass

*/

@SuppressWarnings("unchecked")

public SafeMap(Map map, Class keyClass, Class valueClass) {

this.realMap = Collections.checkedMap(map, keyClass, valueClass);

this.keyClass = keyClass;

this.valueClass = valueClass;

}

 

@Override

public void clear() {this.realMap.clear();

}

 

/**

* this will throw a class cast exception if argument is not the correct

* class

*

* @throws ClassCastException

*/

@Override

public boolean containsKey(Object arg0) { if (!this.keyClass.isAssignableFrom(arg0.getClass())) {

throw new ClassCastException("Tried to fetch a "

+ arg0.getClass().getName() + " but key is "

+ keyClass.getClass().getName());

}

return this.realMap.containsKey(arg0);

}

 

/**

* this will throw a class cast exception if argument is not the correct

* class

*

* @throws ClassCastException

*/

@Override

public boolean containsValue(Object arg0) { if (!this.valueClass.isAssignableFrom(arg0.getClass())) {

throw new ClassCastException("Tried to fetch a "

+ arg0.getClass().getName() + " but key is "

+ valueClass.getName());

}

return this.realMap.containsValue(arg0);

}

 

@Override

public Set entrySet() {return this.realMap.entrySet();

}

 

/**

* this will throw a class cast exception if argument is not the correct

* class

*

* @throws ClassCastException

*/

@Override

public U get(Object arg0) { if (!this.keyClass.isAssignableFrom(arg0.getClass())) {

throw new ClassCastException("Tried to fetch a "

+ arg0.getClass().getName() + " but key is "

+ keyClass.getName());

}

return this.realMap.get(arg0);

}

 

@Override

public boolean isEmpty() {return this.realMap.isEmpty();

}

 

@Override

public Set keySet() {return this.realMap.keySet();

}

 

/**

* this will throw a class cast exception if argument is not the correct

* class

*

* @throws ClassCastException

*/

@Override

public U put(T arg0, U arg1) {return this.realMap.put(arg0, arg1);

}

 

/**

* this will throw a class cast exception if argument is not the correct

* class

*

* @throws ClassCastException

*/

@Override

public void putAll(Mapextends T, ? extends U> arg0) {this.realMap.putAll(arg0);

}

 

@Override

public U remove(Object arg0) {return this.remove(arg0);

}

 

@Override

public int size() {return this.realMap.size();

}

 

@Override

public Collection values() {return this.realMap.values();

}

}