Trivial commit which can not be fundamentally different. In Geotk:
valueType and keyType fields are private and documented.put method is synchronized, documented, checks for write access,
delegates the type check to a ensureValidType private method, and
formats error message using resource bundles. The Geotk relevant code is:@Override
public V put(final K key, final V value)
throws IllegalArgumentException, UnsupportedOperationException
{
ensureValidType(key, keyType);
ensureValidType(value, valueType);
synchronized (getLock()) {
checkWritePermission();
return super.put(key, value);
}
}
Command line:
svn cat -r7112 https://svn.osgeo.org/geotools/trunk/modules/library/metadata/src/main/java/org/geotools/util/CheckedHashMap.java
| Revision 7112 |
|---|
package org.geotools.util;
import java.util.HashMap;
public class MapOf extends HashMap {
private Class valueType;
private Class keyType;
public MapOf( Class keyType, Class valueType ){
this.keyType = keyType;
this.valueType = valueType;
}
public Object put( Object key, Object value ) {
if( key != null && !keyType.isInstance( key ) ){
throw new IllegalArgumentException( "Key limited to type "+keyType.getName() );
}
if( value != null && !valueType.isInstance( value ) ){
throw new IllegalArgumentException( "Value limited type "+valueType.getName() );
}
return super.put( key, value );
}
} |