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.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | public class GlobalDataContainer implements Serializable {
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Id for object serialization.
|
---|
14 | */
|
---|
15 | private static final long serialVersionUID = 1L;
|
---|
16 |
|
---|
17 | transient private static GlobalDataContainer theInstance = null;
|
---|
18 |
|
---|
19 | private Map<String, Object> dataObjects;
|
---|
20 |
|
---|
21 | public static GlobalDataContainer getInstance() {
|
---|
22 | if( theInstance==null ) {
|
---|
23 | theInstance = new GlobalDataContainer();
|
---|
24 | }
|
---|
25 | return theInstance;
|
---|
26 | }
|
---|
27 |
|
---|
28 | private void writeObject(ObjectOutputStream s) throws IOException {
|
---|
29 | s.defaultWriteObject();
|
---|
30 | s.writeObject(dataObjects);
|
---|
31 | }
|
---|
32 |
|
---|
33 | @SuppressWarnings("unchecked")
|
---|
34 | private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
|
---|
35 | s.defaultReadObject();
|
---|
36 | if( theInstance==null ) {
|
---|
37 | theInstance = new GlobalDataContainer();
|
---|
38 | }
|
---|
39 | theInstance.dataObjects = (Map<String, Object>) s.readObject();
|
---|
40 | }
|
---|
41 |
|
---|
42 | private Object readResolve() {
|
---|
43 | return theInstance;
|
---|
44 | }
|
---|
45 |
|
---|
46 | private GlobalDataContainer() {
|
---|
47 | dataObjects = new HashMap<String, Object>();
|
---|
48 | }
|
---|
49 |
|
---|
50 | public boolean addData(String key, Object data) {
|
---|
51 | Object previousEntry = dataObjects.put(key, data);
|
---|
52 | return previousEntry!=null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public boolean removeData(String key) {
|
---|
56 | Object previousEntry = dataObjects.remove(key);
|
---|
57 | return previousEntry==null;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public Object getData(String key) {
|
---|
61 | return dataObjects.get(key);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void reset() {
|
---|
65 | dataObjects = new HashMap<String, Object>();
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|