View Javadoc

1   /*
2    * @(#)AbstractProperty.java April 15, 2004 Copyright (c) 2002-2005-2004 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.introspection;
8   
9   import java.lang.reflect.Array;
10  import java.util.Collection;
11  
12  import nl.tudelft.simulation.logger.Logger;
13  
14  /***
15   * A default Property implementation that provides a standard way to handle
16   * composite values.
17   * <p>
18   * (c) copyright 2002-2005-2004 <a href="http://www.simulation.tudelft.nl">Delft
19   * University of Technology </a>, the Netherlands. <br>
20   * See for project information <a
21   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
22   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
23   * General Public License (LGPL) </a>, no warranty.
24   * 
25   * @author <a
26   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
27   *         Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
28   *         Jacobs </a>
29   * @version 1.1 Apr 15, 2004
30   * @since 1.5
31   */
32  public abstract class AbstractProperty implements Property
33  {
34      /***
35       * Basic 'setValue' implementation. It is checked whether this property
36       * contains a composite value. If so, the composite value of this property
37       * is updated. Composite values are expected to be supplied as a {see
38       * java.util.Collection}. If needed, array conversion takes place. If the
39       * property is not composite, the value-setting is delegated to the
40       * 'setRegularValue' method.
41       * 
42       * @see nl.tudelft.simulation.introspection.Property#setValue(java.lang.Object)
43       */
44      @SuppressWarnings("unchecked")
45      public void setValue(final Object value)
46      {
47          if (!this.isCollection())
48          {
49              this.setRegularValue(value);
50              return;
51          }
52          if (!(value instanceof Collection))
53          {
54              throw new IllegalArgumentException(this
55                      + "assign Collection values to composite properties");
56          }
57          if (this.getType().isArray())
58          {
59              Object[] array = (Object[]) Array.newInstance(getType()
60                      .getComponentType(), 0);
61              this.setRegularValue(((Collection< ? >) value).toArray(array));
62          } else
63          {
64              synchronized (this.getInstance())
65              {
66                  Collection<Object> oldValues = (Collection<Object>) getValue();
67                  try
68                  {
69                      oldValues.clear();
70                      oldValues.addAll((Collection<Object>) value);
71                  } catch (UnsupportedOperationException e)
72                  {
73                      Logger.warning(this, "setValue", "could not empty "
74                              + oldValues + "setValue method canceled");
75                  }
76              }
77          }
78  
79      }
80  
81      /***
82       * Method used to set a regular (i.e. not-composite) property value.
83       * 
84       * @param value the new value
85       */
86      protected abstract void setRegularValue(final Object value);
87  
88      /***
89       * Returns true when the contained value is either an array, or an instance
90       * of {see java.util.Collection}, i.e. is a property with composite value.
91       * 
92       * @see nl.tudelft.simulation.introspection.Property#isCollection()
93       */
94      public boolean isCollection()
95      {
96          if (getType().isArray() || Collection.class.isAssignableFrom(getType()))
97          {
98              return true;
99          }
100         return false;
101     }
102 
103     /***
104      * @see nl.tudelft.simulation.introspection.Property#getComponentType()
105      */
106     public Class getComponentType()
107     {
108         if (!isCollection())
109         {
110             return null;
111         }
112         if (getType().isArray())
113         {
114             return getType().getComponentType();
115         }
116         Collection value = (Collection) getValue();
117         if (value == null || value.size() == 0)
118         {
119             return null;
120         }
121         return value.toArray()[0].getClass();
122     }
123 }