Example usage for org.apache.commons.collections.map TypedMap decorate

List of usage examples for org.apache.commons.collections.map TypedMap decorate

Introduction

In this page you can find the example usage for org.apache.commons.collections.map TypedMap decorate.

Prototype

public static Map decorate(Map map, Class keyType, Class valueType) 

Source Link

Document

Factory method to create a typed map.

Usage

From source file:com.discursive.jccook.collections.typed.TypedMapExample.java

public void start() {
    // 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 {/*w w  w.j a va  2 s . c  om*/
        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");

}