Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2004-2008 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.binding.collection;
 
 import java.util.Map;
 
A simple, generic decorator for getting attributes out of a map. May be instantiated directly or used as a base class as a convenience.

Author(s):
Keith Donald
 
 public class MapAccessor implements MapAdaptable {

The target map.
 
 	private Map map;

Creates a new attribute map accessor.

Parameters:
map the map
 
 	public MapAccessor(Map map) {
 		Assert.notNull(map"The map to decorate is required");
 		this. = map;
 	}
 
 	// implementing MapAdaptable
 
 	public Map asMap() {
 		return ;
 	}

Returns a value in the map, returning null if the attribute is not present.

Parameters:
key the key
Returns:
the value
 
 	public Object get(Object key) {
 		return .get(key);
 	}

Returns a value in the map, returning the defaultValue if no value was found.

Parameters:
key the key
defaultValue the default
Returns:
the attribute value
 
 	public Object get(Object keyObject defaultValue) {
 		if (!.containsKey(key)) {
 			return defaultValue;
 		}
 		return .get(key);
 	}

Returns a value in the map, asserting it is of the required type if present and returning null if not found.

Parameters:
key the key
requiredType the required type
Returns:
the value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not of the required type
 
 	public Object get(Object keyClass requiredTypethrows IllegalArgumentException {
 		return get(keyrequiredTypenull);
 	}

Returns a value in the map of the specified type, returning the defaultValue if no value is found.

Parameters:
key the key
requiredType the required type
defaultValue the default
Returns:
the attribute value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not of the required type
 
 	public Object get(Object keyClass requiredTypeObject defaultValue) {
 		if (!.containsKey(key)) {
 			return defaultValue;
 		}
 		return assertKeyValueOfType(keyrequiredType);
 	}

Returns a value in the map, throwing an exception if the attribute is not present and of the correct type.

Parameters:
key the key
Returns:
the value
		return .get(key);
	}

Returns an value in the map, asserting it is present and of the required type.

Parameters:
key the key
requiredType the required type
Returns:
the value
	public Object getRequired(Object keyClass requiredTypethrows IllegalArgumentException {
		return assertKeyValueOfType(keyrequiredType);
	}

Returns a string value in the map, returning null if no value was found.

Parameters:
key the key
Returns:
the string value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a string
		return getString(keynull);
	}

Returns a string value in the map, returning the defaultValue if no value was found.

Parameters:
key the key
defaultValue the default
Returns:
the string value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a string
	public String getString(Object keyString defaultValuethrows IllegalArgumentException {
		if (!.containsKey(key)) {
			return defaultValue;
		}
		return (StringassertKeyValueOfType(keyString.class);
	}

Returns a string value in the map, throwing an exception if the attribute is not present and of the correct type.

Parameters:
key the key
Returns:
the string value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a string
		return (StringassertKeyValueOfType(keyString.class);
	}

Returns a collection value in the map, returning null if no value was found.

Parameters:
key the key
Returns:
the collection value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a collection
		if (!.containsKey(key)) {
			return null;
		}
	}

Returns a collection value in the map, asserting it is of the required type if present and returning null if not found.

Parameters:
key the key
Returns:
the collection value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a collection
	public Collection getCollection(Object keyClass requiredTypethrows IllegalArgumentException {
		if (!.containsKey(key)) {
			return null;
		}
		assertAssignableTo(Collection.classrequiredType);
		return (CollectionassertKeyValueOfType(keyrequiredType);
	}

Returns a collection value in the map, throwing an exception if not found.

Parameters:
key the key
Returns:
the collection value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a collection
	}

Returns a collection value in the map, asserting it is of the required type if present and throwing an exception if not found.

Parameters:
key the key
Returns:
the collection value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a collection of the required type
	public Collection getRequiredCollection(Object keyClass requiredTypethrows IllegalArgumentException {
		assertAssignableTo(Collection.classrequiredType);
		return (CollectionassertKeyValueOfType(keyrequiredType);
	}

Returns a array value in the map, asserting it is of the required type if present and returning null if not found.

Parameters:
key the key
Returns:
the array value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not an array of the required type
	public Object[] getArray(Object keyClass requiredTypethrows IllegalArgumentException {
		assertAssignableTo(Object[].classrequiredType);
		if (!.containsKey(key)) {
			return null;
		}
		return (Object[]) assertKeyValueOfType(keyrequiredType);
	}

Returns an array value in the map, asserting it is of the required type if present and throwing an exception if not found.

Parameters:
key the key
Returns:
the array value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a array of the required type
	public Object[] getRequiredArray(Object keyClass requiredTypethrows IllegalArgumentException {
		assertAssignableTo(Object[].classrequiredType);
		return (Object[]) assertKeyValueOfType(keyrequiredType);
	}

Returns a number value in the map that is of the specified type, returning null if no value was found.

Parameters:
key the key
requiredType the required number type
Returns:
the numbervalue
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a number of the required type
	public Number getNumber(Object keyClass requiredTypethrows IllegalArgumentException {
		return getNumber(keyrequiredTypenull);
	}

Returns a number attribute value in the map of the specified type, returning the defaultValue if no value was found.

Parameters:
key the attribute name
defaultValue the default
Returns:
the number value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a number of the required type
	public Number getNumber(Object keyClass requiredTypeNumber defaultValuethrows IllegalArgumentException {
		if (!.containsKey(key)) {
			return defaultValue;
		}
		assertAssignableTo(Number.classrequiredType);
		return (NumberassertKeyValueOfType(keyrequiredType);
	}

Returns a number value in the map, throwing an exception if the attribute is not present and of the correct type.

Parameters:
key the key
Returns:
the number value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a number of the required type
	public Number getRequiredNumber(Object keyClass requiredTypethrows IllegalArgumentException {
		return (NumberassertKeyValueOfType(keyrequiredType);
	}

Returns an integer value in the map, returning null if no value was found.

Parameters:
key the key
Returns:
the integer value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not an integer
		return getInteger(keynull);
	}

Returns an integer value in the map, returning the defaultValue if no value was found.

Parameters:
key the key
defaultValue the default
Returns:
the integer value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not an integer
	public Integer getInteger(Object keyInteger defaultValuethrows IllegalArgumentException {
		return (IntegergetNumber(keyInteger.classdefaultValue);
	}

Returns an integer value in the map, throwing an exception if the value is not present and of the correct type.

Parameters:
key the attribute name
Returns:
the integer attribute value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not an integer
		return (IntegergetRequiredNumber(keyInteger.class);
	}

Returns a long value in the map, returning null if no value was found.

Parameters:
key the key
Returns:
the long value
Throws:
java.lang.IllegalArgumentException if the key is present but not a long
	public Long getLong(Object keythrows IllegalArgumentException {
		return getLong(keynull);
	}

Returns a long value in the map, returning the defaultValue if no value was found.

Parameters:
key the key
defaultValue the default
Returns:
the long attribute value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a long
	public Long getLong(Object keyLong defaultValuethrows IllegalArgumentException {
		return (LonggetNumber(keyLong.classdefaultValue);
	}

Returns a long value in the map, throwing an exception if the value is not present and of the correct type.

Parameters:
key the key
Returns:
the long attribute value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a long
		return (LonggetRequiredNumber(keyLong.class);
	}

Returns a boolean value in the map, returning null if no value was found.

Parameters:
key the key
Returns:
the boolean value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a boolean
		return getBoolean(keynull);
	}

Returns a boolean value in the map, returning the defaultValue if no value was found.

Parameters:
key the key
defaultValue the default
Returns:
the boolean value
Throws:
java.lang.IllegalArgumentException if the key is present but the value is not a boolean
	public Boolean getBoolean(Object keyBoolean defaultValuethrows IllegalArgumentException {
		if (!.containsKey(key)) {
			return defaultValue;
		}
		return (BooleanassertKeyValueOfType(keyBoolean.class);
	}

Returns a boolean value in the map, throwing an exception if the value is not present and of the correct type.

Parameters:
key the attribute
Returns:
the boolean value
Throws:
java.lang.IllegalArgumentException if the key is not present or present but the value is not a boolean
		return (BooleanassertKeyValueOfType(keyBoolean.class);
	}

Asserts that the attribute is present in the attribute map.

Parameters:
key the key
Throws:
java.lang.IllegalArgumentException if the key is not present
	public void assertContainsKey(Object keythrows IllegalArgumentException {
		if (!.containsKey(key)) {
			throw new IllegalArgumentException("Required attribute '" + key
"' is not present in map; attributes present are [" + asMap() + "]");
		}
	}

Indicates if the attribute is present in the attribute map and of the required type.

Parameters:
key the attribute name
Returns:
true if present and of the required type, false if not present.
	public boolean containsKey(Object keyClass requiredTypethrows IllegalArgumentException {
		if (.containsKey(key)) {
			assertKeyValueOfType(keyrequiredType);
			return true;
else {
			return false;
		}
	}

Assert that value of the map key, if non-null, is of the required type.

Parameters:
key the attribute name
requiredType the required attribute value type
Returns:
the attribute value
	public Object assertKeyValueOfType(Object keyClass requiredType) {
		return assertKeyValueInstanceOf(key.get(key), requiredType);
	}

Assert that the key value, if non null, is an instance of the required type.

Parameters:
key the key
value the value
requiredType the required type
Returns:
the value
	public Object assertKeyValueInstanceOf(Object keyObject valueClass requiredType) {
		Assert.notNull(requiredType"The required type to assert is required");
		if (value != null && !requiredType.isInstance(value)) {
			throw new IllegalArgumentException("Map key '" + key + "' has value [" + value
"] that is not of expected type [" + requiredType + "], instead it is of type ["
value.getClass().getName() + "]");
		}
		return value;
	}
	private void assertAssignableTo(Class clazzClass requiredType) {
		Assert.isTrue(clazz.isAssignableFrom(requiredType), "The provided required type must be assignable to ["
clazz + "]");
	}
New to GrepCode? Check out our FAQ X