1 | package de.ugoe.cs.eventbench.data;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.io.ObjectInputStream;
|
---|
5 | import java.io.ObjectOutputStream;
|
---|
6 | import java.io.Serializable;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.LinkedList;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Map.Entry;
|
---|
12 |
|
---|
13 | import de.ugoe.cs.eventbench.SequenceInstanceOf;
|
---|
14 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * <p>
|
---|
18 | * This data structure can be used by the commands to store any {@link Object}.
|
---|
19 | * The data is stored in a key-value map, with strings as keys.
|
---|
20 | * </p>
|
---|
21 | * <p>
|
---|
22 | * This class is implemented as a singleton, as more than one data container
|
---|
23 | * does not serves no purpose.
|
---|
24 | * </p>
|
---|
25 | *
|
---|
26 | * @author Steffen Herbold
|
---|
27 | * @version 1.0
|
---|
28 | */
|
---|
29 | public class GlobalDataContainer implements Serializable {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * <p>
|
---|
33 | * Id for object serialization.
|
---|
34 | * </p>
|
---|
35 | */
|
---|
36 | private static final long serialVersionUID = 1L;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Instance of the {@link GlobalDataContainer} (implemented as singleton).
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | transient private static GlobalDataContainer theInstance = null;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * Internal storage of the data.
|
---|
48 | * </p>
|
---|
49 | */
|
---|
50 | private Map<String, Object> dataObjects;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * <p>
|
---|
54 | * Returns the instance of the container. If it does not yet exist, the data
|
---|
55 | * container is created.
|
---|
56 | * </p>
|
---|
57 | *
|
---|
58 | * @return instance of the container
|
---|
59 | */
|
---|
60 | public static GlobalDataContainer getInstance() {
|
---|
61 | if (theInstance == null) {
|
---|
62 | theInstance = new GlobalDataContainer();
|
---|
63 | }
|
---|
64 | return theInstance;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * <p>
|
---|
69 | * Manual serialization of the object. Necessary to guarantee the singleton
|
---|
70 | * property.
|
---|
71 | * </p>
|
---|
72 | *
|
---|
73 | * @param s
|
---|
74 | * output stream for the serialization
|
---|
75 | * @throws IOException
|
---|
76 | * thrown if there is problem writing to the output stream
|
---|
77 | */
|
---|
78 | private void writeObject(ObjectOutputStream s) throws IOException {
|
---|
79 | s.defaultWriteObject();
|
---|
80 | s.writeObject(dataObjects);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * <p>
|
---|
85 | * Manual de-serialization of the object. Necessary to guarantee the
|
---|
86 | * singleton property.
|
---|
87 | *
|
---|
88 | * @param s
|
---|
89 | * input stream for the de-serialization
|
---|
90 | * @throws IOException
|
---|
91 | * thrown if there is problem reading from the input stream
|
---|
92 | * @throws ClassNotFoundException
|
---|
93 | * thrown if there is a problem reading from the input stream
|
---|
94 | */
|
---|
95 | @SuppressWarnings("unchecked")
|
---|
96 | private void readObject(ObjectInputStream s) throws IOException,
|
---|
97 | ClassNotFoundException {
|
---|
98 | s.defaultReadObject();
|
---|
99 | if (theInstance == null) {
|
---|
100 | theInstance = new GlobalDataContainer();
|
---|
101 | }
|
---|
102 | theInstance.dataObjects = (Map<String, Object>) s.readObject();
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * <p>
|
---|
107 | * Manual de-serialization to guarantee the singleton property.
|
---|
108 | * </p>
|
---|
109 | *
|
---|
110 | * @return instance of the container
|
---|
111 | */
|
---|
112 | private Object readResolve() {
|
---|
113 | return theInstance;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * <p>
|
---|
118 | * Constructor. Creates a new GlobalDataContainer. Private to guarantee the
|
---|
119 | * singleton property.
|
---|
120 | * </p>
|
---|
121 | */
|
---|
122 | private GlobalDataContainer() {
|
---|
123 | dataObjects = new HashMap<String, Object>();
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * <p>
|
---|
128 | * Adds data to the container.
|
---|
129 | * </p>
|
---|
130 | *
|
---|
131 | * @param key
|
---|
132 | * key that identifies the data
|
---|
133 | * @param data
|
---|
134 | * data that is stored
|
---|
135 | * @return true, if an old entry was overwritten; false otherwise
|
---|
136 | */
|
---|
137 | public boolean addData(String key, Object data) {
|
---|
138 | Object previousEntry = dataObjects.put(key, data);
|
---|
139 | return previousEntry != null;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * <p>
|
---|
144 | * Removes data from the container.
|
---|
145 | * </p>
|
---|
146 | *
|
---|
147 | * @param key
|
---|
148 | * key of the data to be removed
|
---|
149 | * @return true, if the object was removed; false if it was not present
|
---|
150 | */
|
---|
151 | public boolean removeData(String key) {
|
---|
152 | Object previousEntry = dataObjects.remove(key);
|
---|
153 | return previousEntry != null;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * <p>
|
---|
158 | * Returns the data associated with a key or {@code null} if no data is
|
---|
159 | * stored for the key.
|
---|
160 | * </p>
|
---|
161 | *
|
---|
162 | * @param key
|
---|
163 | * key whose data is returned
|
---|
164 | * @return data associated with the key; {@code null} if no data is
|
---|
165 | * available
|
---|
166 | */
|
---|
167 | public Object getData(String key) {
|
---|
168 | return dataObjects.get(key);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * <p>
|
---|
173 | * Resets the data container, i.e., deletes all its contents.
|
---|
174 | * </p>
|
---|
175 | */
|
---|
176 | public void reset() {
|
---|
177 | dataObjects = new HashMap<String, Object>();
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * <p>
|
---|
182 | * Returns all keys of collections of sequences contained in the storage.
|
---|
183 | * </p>
|
---|
184 | *
|
---|
185 | * @return keys of all collections of sequences contained in the storage
|
---|
186 | */
|
---|
187 | public Collection<String> getAllSequencesNames() {
|
---|
188 | Collection<String> allSequencesNames = new LinkedList<String>();
|
---|
189 | for (Entry<String, Object> entry : dataObjects.entrySet()) {
|
---|
190 | if( SequenceInstanceOf.isCollectionOfSequences(entry.getValue())) {
|
---|
191 | allSequencesNames.add(entry.getKey());
|
---|
192 | }
|
---|
193 | }
|
---|
194 | return allSequencesNames;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * <p>
|
---|
199 | * Returns the keys of all {@link IStochasticProcess}s contained in the
|
---|
200 | * storage.
|
---|
201 | * </p>
|
---|
202 | *
|
---|
203 | * @return keys of all {@link IStochasticProcess}s contained in the storage
|
---|
204 | */
|
---|
205 | public Collection<String> getAllModelNames() {
|
---|
206 | Collection<String> modelNames = new LinkedList<String>();
|
---|
207 | for (Entry<String, Object> entry : dataObjects.entrySet()) {
|
---|
208 | if (entry.getValue() instanceof IStochasticProcess) {
|
---|
209 | modelNames.add(entry.getKey());
|
---|
210 | }
|
---|
211 | }
|
---|
212 | return modelNames;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * <p>
|
---|
217 | * Returns the keys of all objects contained in the storage.
|
---|
218 | * </p>
|
---|
219 | *
|
---|
220 | * @return keys of all objects in the storage
|
---|
221 | */
|
---|
222 | public Collection<String> getAllKeys() {
|
---|
223 | return dataObjects.keySet();
|
---|
224 | }
|
---|
225 |
|
---|
226 | }
|
---|