/*
 * This code is copyright (c) 2005 David Owen.  All rights not explicitly
 * granted by the author are reserved.
 *
 * Created on Aug 2, 2005.
 */
package ws.fugue88.access;

import java.util.Map;

/**
 * @author dsowen
 */
public class MapAccessor implements Accessor {

	public MapAccessor(final Map map, final Object key, final Class type)
	{
		map.getClass();
		type.getClass();

		_map = map;
		_key = key;
		_type = type;
	}

	public Class getType()
	{
		return _type;
	}

	public void clearValue()
	{
		_map.remove(_key);
	}

	public Object getValue()
	{
		return check(_map.get(_key));
	}

	public void setValue(final Object value)
	{
		_map.put(_key, check(value));
	}

	private Object check(final Object obj)
	{
		if(obj != null && !_type.isInstance(obj))
				throw new ClassCastException();
		return obj;
	}

	private final Map _map;
	private final Object _key;
	private final Class _type;
}