source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/data/GlobalDataContainer.java @ 137

Last change on this file since 137 was 88, checked in by sherbold, 14 years ago

+ added commands save and load that saves, respectively loads the internal data

File size: 1.6 KB
Line 
1package de.ugoe.cs.eventbench.data;
2
3import java.io.IOException;
4import java.io.ObjectInputStream;
5import java.io.ObjectOutputStream;
6import java.io.Serializable;
7import java.util.HashMap;
8import java.util.Map;
9
10public 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}
Note: See TracBrowser for help on using the repository browser.