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