Change the header to SO
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / RollbackData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.bpmn.core;
22
23 import java.io.Serializable;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 /**
28  * An object that stores data for rollbacks.  Data is organized by type.  A
29  * type is simply a string identifier.  Multiple types of data may be stored
30  * in the same object for separate rollback operations.
31  */
32 public class RollbackData implements Serializable {
33         private static final long serialVersionUID = 1L;
34
35         private Map<String, Map<String, Serializable>> dictionary =
36                 new HashMap<String, Map<String, Serializable>>();
37         
38         /**
39          * Returns true if the specified type is stored in this object.
40          * @param type the data type
41          */
42         public boolean hasType(String type) {
43                 return dictionary.containsKey(type);
44         }
45
46         /**
47          * Stores a single item.
48          * @param type the data type
49          * @param key the key
50          * @param value the value
51          */
52         public void put(String type, String key, String value) {
53                 Map<String, Serializable> mapForType = dictionary.get(type);
54
55                 if (mapForType == null) {
56                         mapForType = new HashMap<String, Serializable>();
57                         dictionary.put(type, mapForType);
58                 }
59
60                 mapForType.put(key, value);
61         }
62
63         /**
64          * Gets a single item.
65          * @param type the data type
66          * @param key the key
67          * @return the item or null if there is no item for the specified type and key
68          */
69         public Serializable get(String type, String key) {
70                 Map<String, Serializable> mapForType = dictionary.get(type);
71
72                 if (mapForType == null) {
73                         return null;
74                 }
75
76                 return mapForType.get(key);
77         }
78
79         /**
80          * Gets a map containing all items associated with the specified data type.
81          * @param type the data type
82          * @return a map, or null if there are no items associated with the specified
83          *         data type
84          */
85         public Map<String, Serializable> get(String type) {
86                 return dictionary.get(type);
87         }
88
89         /**
90          * Returns a string representation of this object.
91          */
92         public String toString() {
93                 StringBuilder out = new StringBuilder();
94                 out.append(getClass().getSimpleName());
95                 out.append('[');
96                 boolean hasOne = false;
97                 for (String type : dictionary.keySet()) {
98                         if (hasOne) {
99                                 out.append(',');
100                         }
101                         out.append(type);
102                         out.append(dictionary.get(type));
103                         hasOne = true;
104                 }
105                 out.append(']');
106                 return out.toString();
107         }
108 }