// Make sure that items added to this
variables = TypedMap.decorate( new HashMap(), String.class, Number.class );
// Add two String objects
variables.put( "maxThreads", new Integer(200) );
variables.put( "minThreads", new Integer(20) );
variables.put( "lightSpeed", new Double( 2.99792458e8 ) );
// Try to add a String value
try {
variables.put( "server", "test.oreilly.com" );
} catch( IllegalArgumentException iae ) {
System.out.println( "Adding an String value Failed as expected" );
}
// Try to add an Integer key
try {
variables.put( new Integer(30), "test.oreilly.com" );
} catch( IllegalArgumentException iae ) {
System.out.println( "Adding an Integer key Failed as expected" );
}
// Now we can safely cast without the possibility of a ClassCastException
Number reading = (Number) variables.get("lightSpeed");