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