//----------------------------------------------------------------------- /** * Factory method to create a defaulting map. * <p> * The value specified is returned when a missing key is found. * * @param map the map to decorate, must not be null * @param defaultValue the default value to return when the key is not found * @throws IllegalArgumentException if map is null */ public static java.util.Map <Object, Object> decorate(java.util.Map <Object, Object> map, Object defaultValue) { if (defaultValue is Transformer) { defaultValue = ConstantTransformer.getInstance(defaultValue); } return(new DefaultedMap(map, defaultValue)); }
//----------------------------------------------------------------------- /** * Constructs a new empty <code>DefaultedMap</code> that decorates * a <code>HashMap</code>. * <p> * The object passed in will be returned by the map whenever an * unknown key is requested. * * @param defaultValue the default value to return when the key is not found */ public DefaultedMap(Object defaultValue) : base(new java.util.HashMap <Object, Object>()) { if (defaultValue is Transformer) { defaultValue = ConstantTransformer.getInstance(defaultValue); } this.value = defaultValue; }
/** * Creates a Transformer that will return the same object each time the * transformer is used. * * @see org.apache.commons.collections.functors.ConstantTransformer * * @param constantToReturn the constant object to return each time in the transformer * @return the transformer. */ public static Transformer constantTransformer(Object constantToReturn) { return(ConstantTransformer.getInstance(constantToReturn)); }