Changeset 171 for trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/data
- Timestamp:
- 09/09/11 06:23:36 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/data/GlobalDataContainer.java
r88 r171 8 8 import java.util.Map; 9 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 */ 10 23 public class GlobalDataContainer implements Serializable { 11 24 12 25 /** 26 * <p> 13 27 * Id for object serialization. 28 * </p> 14 29 */ 15 30 private static final long serialVersionUID = 1L; 16 31 32 /** 33 * <p> 34 * Instance of the {@link GlobalDataContainer} (implemented as singleton). 35 * </p> 36 */ 17 37 transient private static GlobalDataContainer theInstance = null; 18 38 39 /** 40 * <p> 41 * Internal storage of the data. 42 * </p> 43 */ 19 44 private Map<String, Object> dataObjects; 20 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 */ 21 54 public static GlobalDataContainer getInstance() { 22 if ( theInstance==null) {55 if (theInstance == null) { 23 56 theInstance = new GlobalDataContainer(); 24 57 } 25 58 return theInstance; 26 59 } 27 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 */ 28 72 private void writeObject(ObjectOutputStream s) throws IOException { 29 73 s.defaultWriteObject(); 30 74 s.writeObject(dataObjects); 31 75 } 32 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 */ 33 89 @SuppressWarnings("unchecked") 34 private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { 90 private void readObject(ObjectInputStream s) throws IOException, 91 ClassNotFoundException { 35 92 s.defaultReadObject(); 36 if ( theInstance==null) {93 if (theInstance == null) { 37 94 theInstance = new GlobalDataContainer(); 38 95 } 39 96 theInstance.dataObjects = (Map<String, Object>) s.readObject(); 40 97 } 41 98 99 /** 100 * <p> 101 * Manual de-serialization to guarantee the singleton property. 102 * </p> 103 * 104 * @return instance of the container 105 */ 42 106 private Object readResolve() { 43 107 return theInstance; 44 108 } 45 109 110 /** 111 * <p> 112 * Constructor. Creates a new GlobalDataContainer. Private to guarantee the 113 * singleton property. 114 * </p> 115 */ 46 116 private GlobalDataContainer() { 47 117 dataObjects = new HashMap<String, Object>(); 48 118 } 49 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 */ 50 131 public boolean addData(String key, Object data) { 51 132 Object previousEntry = dataObjects.put(key, data); 52 return previousEntry !=null;133 return previousEntry != null; 53 134 } 54 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 */ 55 145 public boolean removeData(String key) { 56 146 Object previousEntry = dataObjects.remove(key); 57 return previousEntry ==null;147 return previousEntry != null; 58 148 } 59 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 */ 60 161 public Object getData(String key) { 61 162 return dataObjects.get(key); 62 163 } 63 164 165 /** 166 * <p> 167 * Resets the data container, i.e., deletes all its contents. 168 * </p> 169 */ 64 170 public void reset() { 65 171 dataObjects = new HashMap<String, Object>();
Note: See TracChangeset
for help on using the changeset viewer.