FileDocCategorySizeDatePackage
TypedMapExample.javaAPI DocExample2052Wed May 18 09:39:00 BST 2005com.discursive.jccook.collections.typed

TypedMapExample

public class TypedMapExample extends Object

Fields Summary
private Map
variables
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		TypedMapExample example = new TypedMapExample();
		example.start();
	
public voidstart()

		// 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");