package de.ugoe.cs.quest.data; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import de.ugoe.cs.quest.SequenceInstanceOf; import de.ugoe.cs.quest.usageprofiles.IStochasticProcess; /** *

* This data structure can be used by the commands to store any {@link Object}. * The data is stored in a key-value map, with strings as keys. *

*

* This class is implemented as a singleton, as more than one data container * does not serves no purpose. *

* * @author Steffen Herbold * @version 1.0 */ public class GlobalDataContainer implements Serializable { /** *

* Id for object serialization. *

*/ private static final long serialVersionUID = 1L; /** *

* Instance of the {@link GlobalDataContainer} (implemented as singleton). *

*/ transient private static GlobalDataContainer theInstance = null; /** *

* Internal storage of the data. *

*/ private Map dataObjects; /** *

* Returns the instance of the container. If it does not yet exist, the data * container is created. *

* * @return instance of the container */ public static GlobalDataContainer getInstance() { if (theInstance == null) { theInstance = new GlobalDataContainer(); } return theInstance; } /** *

* Manual serialization of the object. Necessary to guarantee the singleton * property. *

* * @param s * output stream for the serialization * @throws IOException * thrown if there is problem writing to the output stream */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeObject(dataObjects); } /** *

* Manual de-serialization of the object. Necessary to guarantee the * singleton property. * * @param s * input stream for the de-serialization * @throws IOException * thrown if there is problem reading from the input stream * @throws ClassNotFoundException * thrown if there is a problem reading from the input stream */ @SuppressWarnings("unchecked") private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); if (theInstance == null) { theInstance = new GlobalDataContainer(); } theInstance.dataObjects = (Map) s.readObject(); } /** *

* Manual de-serialization to guarantee the singleton property. *

* * @return instance of the container */ private Object readResolve() { return theInstance; } /** *

* Constructor. Creates a new GlobalDataContainer. Private to guarantee the * singleton property. *

*/ private GlobalDataContainer() { dataObjects = new HashMap(); } /** *

* Adds data to the container. *

* * @param key * key that identifies the data * @param data * data that is stored * @return true, if an old entry was overwritten; false otherwise */ public boolean addData(String key, Object data) { Object previousEntry = dataObjects.put(key, data); return previousEntry != null; } /** *

* Removes data from the container. *

* * @param key * key of the data to be removed * @return true, if the object was removed; false if it was not present */ public boolean removeData(String key) { Object previousEntry = dataObjects.remove(key); return previousEntry != null; } /** *

* Returns the data associated with a key or {@code null} if no data is * stored for the key. *

* * @param key * key whose data is returned * @return data associated with the key; {@code null} if no data is * available */ public Object getData(String key) { return dataObjects.get(key); } /** *

* Resets the data container, i.e., deletes all its contents. *

*/ public void reset() { dataObjects = new HashMap(); } /** *

* Returns all keys of collections of sequences contained in the storage. *

* * @return keys of all collections of sequences contained in the storage */ public Collection getAllSequencesNames() { Collection allSequencesNames = new LinkedList(); for (Entry entry : dataObjects.entrySet()) { if( SequenceInstanceOf.isCollectionOfSequences(entry.getValue())) { allSequencesNames.add(entry.getKey()); } } return allSequencesNames; } /** *

* Returns the keys of all {@link IStochasticProcess}s contained in the * storage. *

* * @return keys of all {@link IStochasticProcess}s contained in the storage */ public Collection getAllModelNames() { Collection modelNames = new LinkedList(); for (Entry entry : dataObjects.entrySet()) { if (entry.getValue() instanceof IStochasticProcess) { modelNames.add(entry.getKey()); } } return modelNames; } /** *

* Returns the keys of all objects contained in the storage. *

* * @return keys of all objects in the storage */ public Collection getAllKeys() { return dataObjects.keySet(); } }