[434] | 1 | package de.ugoe.cs.quest.ui;
|
---|
[1] | 2 |
|
---|
[88] | 3 | import java.io.IOException;
|
---|
| 4 | import java.io.ObjectInputStream;
|
---|
| 5 | import java.io.ObjectOutputStream;
|
---|
| 6 | import java.io.Serializable;
|
---|
[187] | 7 | import java.util.Collection;
|
---|
[1] | 8 | import java.util.HashMap;
|
---|
[187] | 9 | import java.util.LinkedList;
|
---|
[1] | 10 | import java.util.Map;
|
---|
[187] | 11 | import java.util.Map.Entry;
|
---|
[1] | 12 |
|
---|
[432] | 13 | import de.ugoe.cs.quest.SequenceInstanceOf;
|
---|
[433] | 14 | import de.ugoe.cs.quest.usageprofiles.IStochasticProcess;
|
---|
[187] | 15 |
|
---|
[171] | 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 | */
|
---|
[88] | 29 | public class GlobalDataContainer implements Serializable {
|
---|
[171] | 30 |
|
---|
[88] | 31 | /**
|
---|
[171] | 32 | * <p>
|
---|
[88] | 33 | * Id for object serialization.
|
---|
[171] | 34 | * </p>
|
---|
[88] | 35 | */
|
---|
| 36 | private static final long serialVersionUID = 1L;
|
---|
| 37 |
|
---|
[171] | 38 | /**
|
---|
| 39 | * <p>
|
---|
| 40 | * Instance of the {@link GlobalDataContainer} (implemented as singleton).
|
---|
| 41 | * </p>
|
---|
| 42 | */
|
---|
[88] | 43 | transient private static GlobalDataContainer theInstance = null;
|
---|
[171] | 44 |
|
---|
| 45 | /**
|
---|
| 46 | * <p>
|
---|
| 47 | * Internal storage of the data.
|
---|
| 48 | * </p>
|
---|
| 49 | */
|
---|
[1] | 50 | private Map<String, Object> dataObjects;
|
---|
[171] | 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 | */
|
---|
[1] | 60 | public static GlobalDataContainer getInstance() {
|
---|
[171] | 61 | if (theInstance == null) {
|
---|
[1] | 62 | theInstance = new GlobalDataContainer();
|
---|
| 63 | }
|
---|
| 64 | return theInstance;
|
---|
| 65 | }
|
---|
[171] | 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 | */
|
---|
[88] | 78 | private void writeObject(ObjectOutputStream s) throws IOException {
|
---|
| 79 | s.defaultWriteObject();
|
---|
| 80 | s.writeObject(dataObjects);
|
---|
| 81 | }
|
---|
[171] | 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 | */
|
---|
[88] | 95 | @SuppressWarnings("unchecked")
|
---|
[171] | 96 | private void readObject(ObjectInputStream s) throws IOException,
|
---|
| 97 | ClassNotFoundException {
|
---|
[88] | 98 | s.defaultReadObject();
|
---|
[171] | 99 | if (theInstance == null) {
|
---|
[88] | 100 | theInstance = new GlobalDataContainer();
|
---|
| 101 | }
|
---|
| 102 | theInstance.dataObjects = (Map<String, Object>) s.readObject();
|
---|
| 103 | }
|
---|
[171] | 104 |
|
---|
| 105 | /**
|
---|
| 106 | * <p>
|
---|
| 107 | * Manual de-serialization to guarantee the singleton property.
|
---|
| 108 | * </p>
|
---|
| 109 | *
|
---|
| 110 | * @return instance of the container
|
---|
| 111 | */
|
---|
[88] | 112 | private Object readResolve() {
|
---|
| 113 | return theInstance;
|
---|
| 114 | }
|
---|
[171] | 115 |
|
---|
| 116 | /**
|
---|
| 117 | * <p>
|
---|
| 118 | * Constructor. Creates a new GlobalDataContainer. Private to guarantee the
|
---|
| 119 | * singleton property.
|
---|
| 120 | * </p>
|
---|
| 121 | */
|
---|
[1] | 122 | private GlobalDataContainer() {
|
---|
| 123 | dataObjects = new HashMap<String, Object>();
|
---|
| 124 | }
|
---|
[171] | 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 | */
|
---|
[1] | 137 | public boolean addData(String key, Object data) {
|
---|
| 138 | Object previousEntry = dataObjects.put(key, data);
|
---|
[171] | 139 | return previousEntry != null;
|
---|
[1] | 140 | }
|
---|
[171] | 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 | */
|
---|
[1] | 151 | public boolean removeData(String key) {
|
---|
| 152 | Object previousEntry = dataObjects.remove(key);
|
---|
[171] | 153 | return previousEntry != null;
|
---|
[1] | 154 | }
|
---|
[171] | 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 | */
|
---|
[1] | 167 | public Object getData(String key) {
|
---|
| 168 | return dataObjects.get(key);
|
---|
| 169 | }
|
---|
[171] | 170 |
|
---|
| 171 | /**
|
---|
| 172 | * <p>
|
---|
| 173 | * Resets the data container, i.e., deletes all its contents.
|
---|
| 174 | * </p>
|
---|
| 175 | */
|
---|
[1] | 176 | public void reset() {
|
---|
| 177 | dataObjects = new HashMap<String, Object>();
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[187] | 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()) {
|
---|
[209] | 190 | if( SequenceInstanceOf.isCollectionOfSequences(entry.getValue())) {
|
---|
| 191 | allSequencesNames.add(entry.getKey());
|
---|
[187] | 192 | }
|
---|
| 193 | }
|
---|
| 194 | return allSequencesNames;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /**
|
---|
| 198 | * <p>
|
---|
| 199 | * Returns the keys of all {@link IStochasticProcess}s contained in the
|
---|
[191] | 200 | * storage.
|
---|
[187] | 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 |
|
---|
[191] | 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 |
|
---|
[1] | 226 | }
|
---|