View Javadoc

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