// Copyright 2012 Georg-August-Universität Göttingen, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package de.ugoe.cs.util.console; 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.Map; /** *

* 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 the keys of all objects contained in the storage. *

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