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