1 package nl.tudelft.simulation.introspection;
2
3 import java.lang.reflect.Array;
4 import java.util.Collection;
5 import java.util.Map;
6
7 import org.djutils.immutablecollections.ImmutableCollection;
8 import org.djutils.immutablecollections.ImmutableMap;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 public abstract class AbstractProperty implements Property
25 {
26
27
28
29
30
31
32
33 @SuppressWarnings("unchecked")
34 @Override
35 public void setValue(final Object value)
36 {
37 if (!this.getComposedType().isComposed())
38 {
39 this.setRegularValue(value);
40 return;
41 }
42 if (!(value instanceof Collection))
43 {
44 throw new IllegalArgumentException(this + " - tried to assign a singular value to composite properties");
45 }
46 if (this.getComposedType().isArray())
47 {
48 Object[] array = (Object[]) Array.newInstance(getType().getComponentType(), 0);
49 this.setRegularValue(((Collection<?>) value).toArray(array));
50 }
51 else if (this.getComposedType().isCollection())
52 {
53 synchronized (this.getInstance())
54 {
55 Collection<Object> oldValues = (Collection<Object>) getValue();
56 try
57 {
58 oldValues.clear();
59 oldValues.addAll((Collection<Object>) value);
60 }
61 catch (UnsupportedOperationException e)
62 {
63 throw new IllegalArgumentException(
64 this + " - setValue: could not empty " + oldValues + "setValue method canceled");
65 }
66 }
67 }
68 else
69 {
70 throw new IllegalArgumentException(this + " - tried to assign a value to a map or an immutable collection");
71 }
72
73 }
74
75
76
77
78
79 protected abstract void setRegularValue(final Object value);
80
81
82 @Override
83 public ComposedTypeEnum getComposedType()
84 {
85 if (getType().isArray())
86 {
87 return ComposedTypeEnum.ARRAY;
88 }
89 else if (Collection.class.isAssignableFrom(getType()))
90 {
91 return ComposedTypeEnum.COLLECTION;
92 }
93 else if (ImmutableCollection.class.isAssignableFrom(getType()))
94 {
95 return ComposedTypeEnum.IMMUTABLECOLLECTION;
96 }
97 else if (Map.class.isAssignableFrom(getType()))
98 {
99 return ComposedTypeEnum.MAP;
100 }
101 else if (ImmutableMap.class.isAssignableFrom(getType()))
102 {
103 return ComposedTypeEnum.IMMUTABLEMAP;
104 }
105 return ComposedTypeEnum.NONE;
106 }
107
108
109 @Override
110 public Class<?> getComponentType()
111 {
112 if (!this.getComposedType().isComposed())
113 {
114 return null;
115 }
116 if (getComposedType().isArray())
117 {
118 return getType().getComponentType();
119 }
120 if (getComposedType().isCollection())
121 {
122 Collection<?> value = (Collection<?>) getValue();
123 if (value == null || value.size() == 0)
124 {
125 return null;
126 }
127 return value.toArray()[0].getClass();
128 }
129 if (getComposedType().isImmutableCollection())
130 {
131 ImmutableCollection<?> value = (ImmutableCollection<?>) getValue();
132 if (value == null || value.size() == 0)
133 {
134 return null;
135 }
136 return value.toArray()[0].getClass();
137 }
138
139 return null;
140 }
141
142
143 @Override
144 public String toString()
145 {
146 return "Property [getName()=" + this.getName() + "]";
147 }
148 }