Merge "Adding Junit"
[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 import java.util.stream.Collectors;
27
28 /**
29  * An object that stores data for rollbacks.  Data is organized by type.  A
30  * type is simply a string identifier.  Multiple types of data may be stored
31  * in the same object for separate rollback operations.
32  */
33 public class RollbackData implements Serializable {
34
35     private static final long serialVersionUID = 1L;
36
37     private Map<String, Map<String, Serializable>> dictionary =
38             new HashMap<String, Map<String, Serializable>>();
39
40     /**
41      * Returns true if the specified type is stored in this object.
42      *
43      * @param type the data type
44      */
45     public boolean hasType(String type) {
46         return dictionary.containsKey(type);
47     }
48
49     /**
50      * Stores a single item.
51      *
52      * @param type the data type
53      * @param key the key
54      * @param value the value
55      */
56     public void put(String type, String key, String value) {
57         Map<String, Serializable> mapForType = dictionary.get(type);
58
59         if (mapForType == null) {
60             mapForType = new HashMap<String, Serializable>();
61             dictionary.put(type, mapForType);
62         }
63
64         mapForType.put(key, value);
65     }
66
67     /**
68      * Gets a single item.
69      *
70      * @param type the data type
71      * @param key the key
72      * @return the item or null if there is no item for the specified type and key
73      */
74     public Serializable get(String type, String key) {
75         Map<String, Serializable> mapForType = dictionary.get(type);
76
77         if (mapForType == null) {
78             return null;
79         }
80
81         return mapForType.get(key);
82     }
83
84     /**
85      * Gets a map containing all items associated with the specified data type.
86      *
87      * @param type the data type
88      * @return a map, or null if there are no items associated with the specified data type
89      */
90     public Map<String, Serializable> get(String type) {
91         return dictionary.get(type);
92     }
93
94     /**
95      * Returns a string representation of this object.
96      */
97     @Override
98     public String toString() {
99         return dictionary.entrySet().stream().map(entry -> entry.getKey() + entry.getValue())
100                 .collect(Collectors.joining(",", "[", "]"));
101     }
102 }