View Javadoc

1   package org.xvsm.core;
2   
3   import java.security.InvalidParameterException;
4   
5   import org.xvsm.selectors.Selector;
6   
7   /***
8    * An AtomicEntry is an {@link Entry} without an inner structure.<br>
9    * With the embedded version of the core you can use any object. The remote
10   * version will only support: <br>
11   * TODO: add the supported coordination types.<br>
12   * 
13   * @author Christian Schreiber, Michael Proestler
14   * 
15   * @param <T>
16   *            The object type which will be stored in this Entry.
17   */
18  public class AtomicEntry<T> extends Entry {
19  	/***
20  	 * Generated serial version UID.
21  	 */
22  	private static final long serialVersionUID = -1226744563053474366L;
23  
24  	/***
25  	 * The value stored in this entry.
26  	 */
27  	private T value;
28  
29  	/***
30  	 * The class of the generic value.<br>
31  	 * This is only needed when reading with LindaSelector
32  	 */
33  	private Class<T> valueClass;
34  
35  	/***
36  	 * Private Default Constructor. Setting the Typ to AtomicEntry.
37  	 * 
38  	 */
39  	private AtomicEntry() {
40  		super();
41  		this.setEntryType(Entry.EntryTypes.ATOMICENTRY);
42  	}
43  
44  	/***
45  	 * Creates a new AtomicEntry instance.
46  	 * 
47  	 * @param value
48  	 *            the object which shall be stored.
49  	 */
50  	@SuppressWarnings("unchecked")
51  	public AtomicEntry(T value) {
52  		this();
53  		if (value == null) {
54  			throw new InvalidParameterException(
55  					"Value must not be null."
56  							+ " Use public AtomicEntry(T value, Class<T> valueClass) to create"
57  							+ " an AtomicEnty with null as value.");
58  		}
59  		this.setEntryType(Entry.EntryTypes.ATOMICENTRY);
60  		this.setValue(value);
61  		this.valueClass = (Class<T>) value.getClass();
62  
63  	}
64  
65  	/***
66  	 * Creates a new AtomicEntry instance.
67  	 * 
68  	 * @param valueClass
69  	 *            the Class of the value. This is only needed, when using the
70  	 *            LindaSelector.
71  	 */
72  	public AtomicEntry(Class<T> valueClass) {
73  		this();
74  		this.valueClass = valueClass;
75  	}
76  
77  	/***
78  	 * Creates a new AtomicEntry instance.
79  	 * 
80  	 * @param value
81  	 *            the object which shall be stored.
82  	 * @param valueClass
83  	 *            the Class of the value. This is only needed, when using the
84  	 *            LindaSelector.
85  	 */
86  	public AtomicEntry(T value, Class<T> valueClass) {
87  		this(value);
88  		if (valueClass == null) {
89  			throw new IllegalArgumentException(
90  					"valueClass is not allowed to be null.");
91  		}
92  		this.valueClass = valueClass;
93  
94  	}
95  
96  	/***
97  	 * Creates a new AtomicEntry instance.
98  	 * 
99  	 * @param value
100 	 *            the object which shall be stored.
101 	 * @param valueClass
102 	 *            the Class of the value. This is only needed, when using the
103 	 *            LindaSelector.
104 	 * @param selectors
105 	 *            the {Selector}s which shall be used for writing.
106 	 */
107 	public AtomicEntry(T value, Class<T> valueClass, Selector... selectors) {
108 		this();
109 		this.setEntryType(Entry.EntryTypes.ATOMICENTRY);
110 		this.setValue(value);
111 		this.setSelectors(java.util.Arrays.asList(selectors));
112 		this.valueClass = valueClass;
113 	}
114 
115 	/***
116 	 * Get the value of this entry.
117 	 * 
118 	 * @return the value
119 	 */
120 	public T getValue() {
121 		return value;
122 	}
123 
124 	/***
125 	 * Set the value of this entry.
126 	 * 
127 	 * @param value
128 	 *            the value to set
129 	 */
130 	public void setValue(T value) {
131 		this.value = value;
132 	}
133 
134 	/***
135 	 * Get the valueClass.
136 	 * 
137 	 * @return the value Class
138 	 */
139 	public Class<T> getValueClass() {
140 		return valueClass;
141 	}
142 
143 	/***
144 	 * Set the Value Class.
145 	 * 
146 	 * @param c
147 	 *            the new ValueClass.
148 	 */
149 	public void setValueClass(Class<T> c) {
150 		this.valueClass = c;
151 	}
152 
153 	/***
154 	 * 
155 	 * {@inheritDoc}.
156 	 */
157 	@Override
158 	public String toString() {
159 		return "<ID: " + this.getId() + " Value:" + this.valueClass + ":["
160 				+ (this.value == null ? null : this.value) + "], "
161 				+ super.toString() + ">";
162 	}
163 
164 	/***
165 	 * {@inheritDoc}.
166 	 */
167 	@Override
168 	public int hashCode() {
169 		final int prime = 31;
170 		int result = super.hashCode();
171 		result = prime * result + ((value == null) ? 0 : value.hashCode());
172 		return result;
173 	}
174 
175 	/***
176 	 * {@inheritDoc}.
177 	 */
178 	@Override
179 	public boolean equals(Object o) {
180 		// super just checks if the ids are equal ... if
181 		// this is the case we have equal entries.
182 		if (super.equals(o)) {
183 			return true;
184 		}
185 
186 		if (o instanceof AtomicEntry) {
187 			AtomicEntry<?> entry = (AtomicEntry<?>) o;
188 			return this.getEntryType().equals(entry.getEntryType())
189 					&& this.valueClass.equals(entry.getValueClass())
190 					&& (this.value == entry.getValue())
191 					|| (this.value != null && (this.value.equals(entry
192 							.getValue())));
193 		}
194 		return false;
195 	}
196 
197 	/***
198 	 * {@inheritDoc}.
199 	 */
200 	@Override
201 	public AtomicEntry<T> getFlatCopy() {
202 		AtomicEntry<T> e = new AtomicEntry<T>(this.value);
203 		return e;
204 	}
205 }