2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.utilities.comparison;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.TreeMap;
31 * This class holds the result of a difference check between two keyed maps. Four results are returned in the class. The
32 * {@code leftOnly} result is the entries that appear only in the left map. the {@code rightOnly} result is the entries
33 * that appear only in the right map. The {@code differentValues} result are the entries that have the same key but
34 * different values in the maps being compared. The {@code identicalValues} result are the entries with identical keys
35 * and values in both maps being compared.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
38 * @param <K> the generic type
39 * @param <V> the generic type
42 public class KeyedMapDifference<K, V> {
43 private static final String KEY = "key=";
44 private static final String VALUE = ",value=";
46 // Three maps to hold the comparison result
47 private Map<K, V> leftOnly = new TreeMap<>();
48 private Map<K, V> rightOnly = new TreeMap<>();
49 private Map<K, V> identicalValues = new TreeMap<>();
50 private Map<K, List<V>> differentValues = new TreeMap<>();
53 * Return a string representation of the differences.
55 * @param diffsOnly if set, then a blank string is returned if the maps are equal
56 * @param keysOnly if set, then a terse string that prints only the keys is returned, otherwise both keys and values
60 public String asString(final boolean diffsOnly, final boolean keysOnly) {
61 var builder = new StringBuilder();
63 if (leftOnly.isEmpty()) {
65 builder.append("*** all left keys in right\n");
68 builder.append(getInOneSideOnlyAsString(leftOnly, "left", keysOnly));
71 if (rightOnly.isEmpty()) {
73 builder.append("*** all right keys in left\n");
76 builder.append(getInOneSideOnlyAsString(rightOnly, "right", keysOnly));
79 if (differentValues.isEmpty()) {
81 builder.append("*** all values in left and right are identical\n");
84 builder.append(getDifferencesAsString(keysOnly));
88 builder.append(getIdenticalsAsString(keysOnly));
91 return builder.toString();
95 * Output the entries in a map with entries that are in one side only as a string.
97 * @param sideMap the map for the side being checked
98 * @param sideMapString the string that represents the map in output strings
99 * @param keysOnly if true, just add key information and not entries
100 * @return the entries as a string
102 private Object getInOneSideOnlyAsString(final Map<K, V> sideMap, final String sideMapString,
103 final boolean keysOnly) {
104 var builder = new StringBuilder();
106 builder.append("*** list of keys on " + sideMapString + " only\n");
107 for (Entry<K, V> leftEntry : sideMap.entrySet()) {
109 builder.append(leftEntry.getKey());
111 builder.append(VALUE);
112 builder.append(leftEntry.getValue());
114 builder.append('\n');
117 return builder.toString();
121 * Output the differences between two the maps as a string.
123 * @param keysOnly if true, just add key information and not entries
124 * @return the differences as a string
126 private String getDifferencesAsString(final boolean keysOnly) {
127 var builder = new StringBuilder();
129 builder.append("*** list of differing entries between left and right\n");
130 for (Entry<K, List<V>> differentEntry : differentValues.entrySet()) {
132 builder.append(differentEntry.getKey());
134 builder.append(",values={");
136 for (V differentEntryValue : differentEntry.getValue()) {
137 builder.append(differentEntryValue);
146 builder.append('\n');
149 return builder.toString();
153 * Output the identical entries in the maps as a string.
155 * @param keysOnly if true, just add key information and not entries
156 * @return the identical entries as a string
158 private String getIdenticalsAsString(final boolean keysOnly) {
159 var builder = new StringBuilder();
161 builder.append("*** list of identical entries in left and right\n");
162 for (Entry<K, V> identicalEntry : identicalValues.entrySet()) {
164 builder.append(identicalEntry.getKey());
166 builder.append(VALUE);
167 builder.append(identicalEntry.getValue());
169 builder.append('\n');
172 return builder.toString();