source: trunk/quest-ui-core/src/main/java/de/ugoe/cs/quest/ui/GlobalDataContainer.java @ 658

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