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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.utilities.comparison;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.TreeMap;
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.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
35 * @param <K> the generic type
36 * @param <V> the generic type
38 public class KeyedMapDifference<K, V> {
39 private static final String KEY = "key=";
40 private static final String VALUE = ",value=";
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<>();
49 * Gets the entries that were found only in the left map.
51 * @return the entries only in the left map
53 public Map<K, V> getLeftOnly() {
58 * Gets the entries that were found only in the right map.
60 * @return the entries only in the right map
62 public Map<K, V> getRightOnly() {
67 * Gets the entries that were identical (keys and values the same) in both maps.
69 * @return the identical entries
71 public Map<K, V> getIdenticalValues() {
72 return identicalValues;
76 * Gets the entries that had the same key but different values in both maps.
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.
81 public Map<K, List<V>> getDifferentValues() {
82 return differentValues;
86 * Return a string representation of the differences.
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
92 public String asString(final boolean diffsOnly, final boolean keysOnly) {
93 StringBuilder builder = new StringBuilder();
95 if (leftOnly.isEmpty()) {
97 builder.append("*** all left keys in right\n");
101 builder.append(getInOneSideOnlyAsString(leftOnly, "left", keysOnly));
104 if (leftOnly.isEmpty()) {
106 builder.append("*** all right keys in left\n");
110 builder.append(getInOneSideOnlyAsString(rightOnly, "right", keysOnly));
113 if (differentValues.isEmpty()) {
115 builder.append("*** all values in left and right are identical\n");
119 builder.append(getDifferencesAsString(keysOnly));
123 builder.append(getIdenticalsAsString(keysOnly));
126 return builder.toString();
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
136 private Object getInOneSideOnlyAsString(final Map<K, V> sideMap, final String sideMapString, final boolean keysOnly) {
137 StringBuilder builder = new StringBuilder();
139 builder.append("*** list of keys on " + sideMapString + " only\n");
140 for (Entry<K, V> leftEntry : sideMap.entrySet()) {
142 builder.append(leftEntry.getKey());
144 builder.append(VALUE);
145 builder.append(leftEntry.getValue());
147 builder.append('\n');
150 return builder.toString();
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
158 private String getDifferencesAsString(final boolean keysOnly) {
159 StringBuilder builder = new StringBuilder();
161 builder.append("*** list of differing entries between left and right\n");
162 for (Entry<K, List<V>> differentEntry : differentValues.entrySet()) {
164 builder.append(differentEntry.getKey());
166 builder.append(",values={");
167 boolean first = true;
168 for (V differentEntryValue : differentEntry.getValue()) {
169 builder.append(differentEntryValue);
179 builder.append('\n');
182 return builder.toString();
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
190 private String getIdenticalsAsString(final boolean keysOnly) {
191 StringBuilder builder = new StringBuilder();
193 builder.append("*** list of identical entries in left and right\n");
194 for (Entry<K, V> identicalEntry : identicalValues.entrySet()) {
196 builder.append(identicalEntry.getKey());
198 builder.append(VALUE);
199 builder.append(identicalEntry.getValue());
201 builder.append('\n');
204 return builder.toString();