source: trunk/java-utils/src/main/java/de/ugoe/cs/util/console/GlobalDataContainer.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 5.0 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util.console;
16
17import java.io.IOException;
18import java.io.ObjectInputStream;
19import java.io.ObjectOutputStream;
20import java.io.Serializable;
21import java.util.Collection;
22import java.util.HashMap;
23import java.util.Map;
24
25/**
26 * <p>
27 * This data structure can be used by the commands to store any {@link Object}.
28 * The data is stored in a key-value map, with strings as keys.
29 * </p>
30 * <p>
31 * This class is implemented as a singleton, as more than one data container
32 * does not serves no purpose.
33 * </p>
34 *
35 * @author Steffen Herbold
36 * @version 1.0
37 */
38public class GlobalDataContainer implements Serializable {
39
40        /**
41         * <p>
42         * Id for object serialization.
43         * </p>
44         */
45        private static final long serialVersionUID = 1L;
46
47        /**
48         * <p>
49         * Instance of the {@link GlobalDataContainer} (implemented as singleton).
50         * </p>
51         */
52        transient private static GlobalDataContainer theInstance = null;
53
54        /**
55         * <p>
56         * Internal storage of the data.
57         * </p>
58         */
59        private Map<String, Object> dataObjects;
60
61        /**
62         * <p>
63         * Returns the instance of the container. If it does not yet exist, the data
64         * container is created.
65         * </p>
66         *
67         * @return instance of the container
68         */
69        public static GlobalDataContainer getInstance() {
70                if (theInstance == null) {
71                        theInstance = new GlobalDataContainer();
72                }
73                return theInstance;
74        }
75
76        /**
77         * <p>
78         * Manual serialization of the object. Necessary to guarantee the singleton
79         * property.
80         * </p>
81         *
82         * @param s
83         *            output stream for the serialization
84         * @throws IOException
85         *             thrown if there is problem writing to the output stream
86         */
87        private void writeObject(ObjectOutputStream s) throws IOException {
88                s.defaultWriteObject();
89                s.writeObject(dataObjects);
90        }
91
92        /**
93         * <p>
94         * Manual de-serialization of the object. Necessary to guarantee the
95         * singleton property.
96         *
97         * @param s
98         *            input stream for the de-serialization
99         * @throws IOException
100         *             thrown if there is problem reading from the input stream
101         * @throws ClassNotFoundException
102         *             thrown if there is a problem reading from the input stream
103         */
104        @SuppressWarnings("unchecked")
105        private void readObject(ObjectInputStream s) throws IOException,
106                        ClassNotFoundException {
107                s.defaultReadObject();
108                if (theInstance == null) {
109                        theInstance = new GlobalDataContainer();
110                }
111                theInstance.dataObjects = (Map<String, Object>) s.readObject();
112        }
113
114        /**
115         * <p>
116         * Manual de-serialization to guarantee the singleton property.
117         * </p>
118         *
119         * @return instance of the container
120         */
121        private Object readResolve() {
122                return theInstance;
123        }
124
125        /**
126         * <p>
127         * Constructor. Creates a new GlobalDataContainer. Private to guarantee the
128         * singleton property.
129         * </p>
130         */
131        private GlobalDataContainer() {
132                dataObjects = new HashMap<String, Object>();
133        }
134
135        /**
136         * <p>
137         * Adds data to the container.
138         * </p>
139         *
140         * @param key
141         *            key that identifies the data
142         * @param data
143         *            data that is stored
144         * @return true, if an old entry was overwritten; false otherwise
145         */
146        public boolean addData(String key, Object data) {
147                Object previousEntry = dataObjects.put(key, data);
148                return previousEntry != null;
149        }
150
151        /**
152         * <p>
153         * Removes data from the container.
154         * </p>
155         *
156         * @param key
157         *            key of the data to be removed
158         * @return true, if the object was removed; false if it was not present
159         */
160        public boolean removeData(String key) {
161                Object previousEntry = dataObjects.remove(key);
162                return previousEntry != null;
163        }
164
165        /**
166         * <p>
167         * Returns the data associated with a key or {@code null} if no data is
168         * stored for the key.
169         * </p>
170         *
171         * @param key
172         *            key whose data is returned
173         * @return data associated with the key; {@code null} if no data is
174         *         available
175         */
176        public Object getData(String key) {
177                return dataObjects.get(key);
178        }
179
180        /**
181         * <p>
182         * Resets the data container, i.e., deletes all its contents.
183         * </p>
184         */
185        public void reset() {
186                dataObjects = new HashMap<String, Object>();
187        }
188
189        /**
190         * <p>
191         * Returns the keys of all objects contained in the storage.
192         * </p>
193         *
194         * @return keys of all objects in the storage
195         */
196        public Collection<String> getAllKeys() {
197                return dataObjects.keySet();
198        }
199
200}
Note: See TracBrowser for help on using the repository browser.