package nl.tudelft.simulation.introspection; import java.lang.reflect.Array; import java.util.Collection; /** * A default Property implementation that provides a standard way to handle composite values. *
* copyright (c) 2002-2018 Delft University of Technology.
* BSD-style license. See DSOL License.
* @author Peter Jacobs.
* @author Alexander Verbraeck.
* @author Niels Lang.
* @since 1.5
*/
public abstract class AbstractProperty implements Property
{
/**
* Basic 'setValue' implementation. It is checked whether this property contains a composite value. If so, the
* composite value of this property is updated. Composite values are expected to be supplied as a {see
* java.util.Collection}. If needed, array conversion takes place. If the property is not composite, the
* value-setting is delegated to the 'setRegularValue' method.
* @see nl.tudelft.simulation.introspection.Property#setValue(java.lang.Object)
*/
@SuppressWarnings("unchecked")
@Override
public void setValue(final Object value)
{
if (!this.isCollection())
{
this.setRegularValue(value);
return;
}
if (!(value instanceof Collection))
{
throw new IllegalArgumentException(this + " - assign Collection values to composite properties");
}
if (this.getType().isArray())
{
Object[] array = (Object[]) Array.newInstance(getType().getComponentType(), 0);
this.setRegularValue(((Collection>) value).toArray(array));
}
else
{
synchronized (this.getInstance())
{
Collection