0f9d6ca50ab4f7ca0fad2ff0136486f4df0c59d4
[policy/apex-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.utilities.comparison;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.TreeMap;
27
28 /**
29  * This class holds the result of a difference check between two keyed maps. Four results are returned in the class. The {@code leftOnly} result is the entries
30  * that appear only in the left map. the {@code rightOnly} result is the entries that appear only in the right map. The {@code differentValues} result are the
31  * entries that have the same key but different values in the maps being compared. The {@code identicalValues} result are the entries with identical keys and
32  * values in both maps being compared.
33  *
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  * @param <K> the generic type
36  * @param <V> the generic type
37  */
38 public class KeyedMapDifference<K, V> {
39         private static final String KEY = "key=";
40         private static final String VALUE = ",value=";
41
42         // Three maps to hold the comparison result
43         private Map<K, V> leftOnly = new TreeMap<>();
44         private Map<K, V> rightOnly = new TreeMap<>();
45         private Map<K, V> identicalValues = new TreeMap<>();
46         private Map<K, List<V>> differentValues = new TreeMap<>();
47
48         /**
49          * Gets the entries that were found only in the left map.
50          *
51          * @return the entries only in the left map
52          */
53         public Map<K, V> getLeftOnly() {
54                 return leftOnly;
55         }
56
57         /**
58          * Gets the entries that were found only in the right map.
59          *
60          * @return the entries only in the right map
61          */
62         public Map<K, V> getRightOnly() {
63                 return rightOnly;
64         }
65
66         /**
67          * Gets the entries that were identical (keys and values the same) in both maps.
68          *
69          * @return the identical entries
70          */
71         public Map<K, V> getIdenticalValues() {
72                 return identicalValues;
73         }
74
75         /**
76          * Gets the entries that had the same key but different values in both maps.
77          *
78          * @return the entries that were different. There are two values in the list of values for each entry. The first value is the value that was in the left map
79          *         and the second value is the value that was in the right map.
80          */
81         public Map<K, List<V>> getDifferentValues() {
82                 return differentValues;
83         }
84
85         /**
86          * Return a string representation of the differences.
87          *
88          * @param diffsOnly if set, then a blank string is returned if the maps are equal
89          * @param keysOnly if set, then a terse string that prints only the keys is returned, otherwise both keys and values are printed
90          * @return the string
91          */
92         public String asString(final boolean diffsOnly, final boolean keysOnly) {
93                 StringBuilder builder = new StringBuilder();
94
95                 if (leftOnly.isEmpty()) {
96                         if (!diffsOnly) {
97                                 builder.append("*** all left keys in right\n");
98                         }
99                 }
100                 else {
101                         builder.append(getInOneSideOnlyAsString(leftOnly, "left",  keysOnly));
102                 }
103
104                 if (leftOnly.isEmpty()) {
105                         if (!diffsOnly) {
106                                 builder.append("*** all right keys in left\n");
107                         }
108                 }
109                 else {
110                         builder.append(getInOneSideOnlyAsString(rightOnly, "right",  keysOnly));
111                 }
112
113                 if (differentValues.isEmpty()) {
114                         if (!diffsOnly) {
115                                 builder.append("*** all values in left and right are identical\n");
116                         }
117                 }
118                 else {
119                         builder.append(getDifferencesAsString(keysOnly));
120                 }
121
122                 if (!diffsOnly) {
123                         builder.append(getIdenticalsAsString(keysOnly));
124                 }
125
126                 return builder.toString();
127         }
128
129         /**
130          * Output the entries in a map with entries that are in one side only as a string
131          * @param sideMap the map for the side being checked
132          * @param sideMapString the string that represents the map in output strings
133          * @param keysOnly if true, just add key information and not entries
134          * @return the entries as a string
135          */
136         private Object getInOneSideOnlyAsString(final Map<K, V> sideMap, final String sideMapString, final boolean keysOnly) {
137                 StringBuilder builder = new StringBuilder();
138
139                 builder.append("*** list of keys on " + sideMapString + " only\n");
140                 for (Entry<K, V> leftEntry : sideMap.entrySet()) {
141                         builder.append(KEY);
142                         builder.append(leftEntry.getKey());
143                         if (!keysOnly) {
144                                 builder.append(VALUE);
145                                 builder.append(leftEntry.getValue());
146                         }
147                         builder.append('\n');
148                 }
149
150                 return builder.toString();
151         }
152
153         /**
154          * Output the differences between two the maps as a string
155          * @param keysOnly if true, just add key information and not entries
156          * @return the differences as a string
157          */
158         private String getDifferencesAsString(final boolean keysOnly) {
159                 StringBuilder builder = new StringBuilder();
160
161                 builder.append("*** list of differing entries between left and right\n");
162                 for (Entry<K, List<V>> differentEntry : differentValues.entrySet()) {
163                         builder.append(KEY);
164                         builder.append(differentEntry.getKey());
165                         if (!keysOnly) {
166                                 builder.append(",values={");
167                                 boolean first = true;
168                                 for (V differentEntryValue : differentEntry.getValue()) {
169                                         builder.append(differentEntryValue);
170                                         if (first) {
171                                                 first = false;
172                                         }
173                                         else {
174                                                 builder.append(',');
175                                         }
176                                 }
177                                 builder.append("}");
178                         }
179                         builder.append('\n');
180                 }
181
182                 return builder.toString();
183         }
184
185         /**
186          * Output the identical entries in the maps as a string
187          * @param keysOnly if true, just add key information and not entries
188          * @return the identical entries as a string
189          */
190         private String getIdenticalsAsString(final boolean keysOnly) {
191                 StringBuilder builder = new StringBuilder();
192
193                 builder.append("*** list of identical entries in left and right\n");
194                 for (Entry<K, V> identicalEntry : identicalValues.entrySet()) {
195                         builder.append(KEY);
196                         builder.append(identicalEntry.getKey());
197                         if (!keysOnly) {
198                                 builder.append(VALUE);
199                                 builder.append(identicalEntry.getValue());
200                         }
201                         builder.append('\n');
202                 }
203
204                 return builder.toString();
205         }
206 }