1 package org.xvsm.selectors;
2
3 import java.util.Properties;
4
5 import org.xvsm.core.AtomicEntry;
6 import org.xvsm.internal.exceptions.FatalException;
7
8 /***
9 * @author Christian Schreiber, Michael Proestler
10 * @param <T>
11 * the type of the key value.
12 *
13 */
14 public class GenericKeySelector<T> extends Selector {
15 /***
16 *
17 */
18 private static final long serialVersionUID = 2641355619106596334L;
19
20 /***
21 * the type of the keyValue.
22 */
23 protected Class valueType;
24
25 /***
26 * The value of the key.
27 */
28 protected Object keyValue;
29
30 /***
31 * The name of the key.
32 */
33 protected String keyName;
34
35 /***
36 * Default constructor.
37 */
38 public GenericKeySelector() {
39 }
40
41 /***
42 * Creates a new KeySelector.
43 *
44 * @param keyName
45 * the name of the key.
46 * @param keyValue
47 * the value of the key (must not be <code>null</code>).
48 */
49 public GenericKeySelector(String keyName, T keyValue) {
50 this.setCount(1);
51 if (keyValue == null) {
52 throw new IllegalArgumentException(
53 "The keyValue paramater must not be null");
54 }
55 if (keyName == null) {
56 throw new IllegalArgumentException(
57 "The keyName paramater must not be null");
58 }
59 this.keyName = keyName;
60 this.keyValue = keyValue;
61 this.valueType = keyValue.getClass();
62 }
63
64 /***
65 * Get the keyName of this KeySelector.
66 *
67 * @return the keyName
68 */
69 public String getKeyName() {
70 return keyName;
71 }
72
73 /***
74 * Get the keyValue of this KeySelector.
75 *
76 * @return the keyValue
77 */
78 public T getKeyValue() {
79 return (T) keyValue;
80 }
81
82 /***
83 * Get the valueType of this KeySelector.
84 *
85 * @return the valueType
86 */
87 public Class getValueType() {
88 return valueType;
89 }
90
91 /***
92 * Sets the properties of this KeySelector.<br>
93 * The properties are:<br>
94 * <code>KeyName</code>: The name of the Key (a String value)<br>
95 * <code>KeyType</code>: The type of the Key<br>
96 * <code>KeyValue</code>: The value of the Key<br>
97 *
98 * @return the properties.
99 */
100 @Override
101 public Properties getProperties() {
102 Properties p = new Properties();
103 p.put("Name", new AtomicEntry<String>(this.keyName));
104 p.put("Type", new AtomicEntry<String>(this.valueType.getSimpleName()));
105 p.put("Value", new AtomicEntry<Object>(this.keyValue.toString(),
106 this.valueType, (Selector) null));
107 return p;
108 }
109
110 /***
111 * {@inheritDoc}
112 */
113 @Override
114 public void setProperties(Properties properties) {
115 String name = (String) ((AtomicEntry<?>) properties.get("Name"))
116 .getValue();
117 String type = (String) ((AtomicEntry<?>) properties.get("Type"))
118 .getValue();
119 Object value = (Object) ((AtomicEntry<?>) properties.get("Value"))
120 .getValue();
121 if (name == null || type == null || value == null) {
122 throw new IllegalArgumentException(
123 "The properties KeyName, KeyType and KeyValue are requred");
124 }
125 this.keyName = name;
126 try {
127 this.valueType = Class.forName("java.lang." + type);
128 } catch (ClassNotFoundException e) {
129 throw new FatalException(e);
130 }
131 if (this.valueType.equals(Integer.class)) {
132
133
134 this.keyValue = (Integer) value;
135 } else if (this.valueType.equals(Boolean.class)) {
136 this.keyValue = (Boolean) value;
137 } else if (this.valueType.equals(Character.class)) {
138 this.keyValue = (Character) value;
139 } else if (this.valueType.equals(Byte.class)) {
140 this.keyValue = (Byte) value;
141 } else if (this.valueType.equals(Short.class)) {
142 this.keyValue = (Short) value;
143 } else if (this.valueType.equals(Long.class)) {
144 this.keyValue = (Long) value;
145 } else if (this.valueType.equals(Float.class)) {
146 this.keyValue = (Float) value;
147 } else if (this.valueType.equals(Double.class)) {
148 this.keyValue = (Double) value;
149 } else if (this.valueType.equals(String.class)) {
150 this.keyValue = (String) value;
151 } else {
152
153 }
154 }
155
156 /***
157 *
158 * {@inheritDoc}.
159 */
160 @Override
161 public String toString() {
162 return "<KeySelector " + keyName + ", " + valueType + ", " + keyValue
163 + ">";
164 }
165
166 /***
167 *
168 * {@inheritDoc}.
169 */
170 @Override
171 public int hashCode() {
172 final int prime = 31;
173 int result = 1;
174 result = prime * result + ((keyName == null) ? 0 : keyName.hashCode());
175 result = prime * result
176 + ((keyValue == null) ? 0 : keyValue.hashCode());
177 result = prime * result
178 + ((valueType == null) ? 0 : valueType.hashCode());
179 return result;
180 }
181
182 /***
183 *
184 * {@inheritDoc}.
185 */
186 @Override
187 public boolean equals(Object obj) {
188 if (this == obj)
189 return true;
190 if (obj == null)
191 return false;
192 if (getClass() != obj.getClass())
193 return false;
194 final GenericKeySelector other = (GenericKeySelector) obj;
195 if (keyName == null) {
196 if (other.keyName != null)
197 return false;
198 } else if (!keyName.equals(other.keyName))
199 return false;
200 if (keyValue == null) {
201 if (other.keyValue != null)
202 return false;
203 } else if (!keyValue.equals(other.keyValue))
204 return false;
205 if (valueType == null) {
206 if (other.valueType != null)
207 return false;
208 } else if (!valueType.equals(other.valueType))
209 return false;
210 return true;
211 }
212 }