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

import java.util.Collection;

/**
 * Represents the containment property of a given collection and object.
 * 
 * @author dsowen
 */
public class ContainmentAccessor implements Accessor {

	public ContainmentAccessor(final Collection coll, final Object obj)
	{
		coll.getClass();

		_coll = coll;
		_obj = obj;
	}

	/**
	 * @return <code>Boolean.class</code>
	 */
	public Class getType()
	{
		return Boolean.class;
	}

	/**
	 * Equivalent to <code>setValue(Boolean.FALSE);</code>.
	 */
	public void clearValue()
	{
		setValue(Boolean.FALSE);
	}

	/**
	 * @return <code>true</code> iff the collection contains the object
	 */
	public Object getValue()
	{
		return Boolean.valueOf(_coll.contains(_obj));
	}

	/**
	 * If <var>value </var> is <code>true</code> or <code>false</code>,
	 * ensures that the collection does or doesn't contain, respectively, the
	 * object.
	 */
	public void setValue(final Object value)
	{
		if(((Boolean)value).booleanValue()) {
			if(!_coll.contains(_obj)) _coll.add(_obj);
		} else {
			while(_coll.contains(_obj))
				_coll.remove(_obj);
		}
	}

	private final Collection _coll;
	private final Object _obj;
}