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;
24 import java.util.AbstractMap.SimpleEntry;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.NavigableMap;
29 import lombok.AccessLevel;
30 import lombok.NoArgsConstructor;
33 * This class provides utility functions for tree maps. A function to find the nearest match in the tree map to an input
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class TreeMapUtils {
42 * Find the list of entries that matches a given word, for example "p" will match "put", "policy", and "push".
44 * @param <T> the generic type for the value of the tree map
45 * @param searchMap the map that the method operates on
46 * @param word the word to search for
47 * @return the list of entries in the {@code searchMap} that match the {@code word}
49 public static <T> List<Entry<String, T>> findMatchingEntries(final NavigableMap<String, T> searchMap,
51 final List<Entry<String, T>> foundNodes = new ArrayList<>();
53 // A straight match check
54 if (searchMap.containsKey(word)) {
55 foundNodes.add(new SimpleEntry<>(word, searchMap.get(word)));
59 // Set up the beginning point for our search for a list of near matches
60 String foundKeyword = searchMap.floorKey(word);
61 if (foundKeyword == null) {
62 foundKeyword = searchMap.firstKey();
64 foundKeyword = searchMap.higherKey(foundKeyword);
67 // Find all the nodes that start with the word we are searching for
68 while (foundKeyword != null) {
69 if (foundKeyword.startsWith(word)) {
70 foundNodes.add(new SimpleEntry<>(foundKeyword, searchMap.get(foundKeyword)));
71 foundKeyword = searchMap.higherKey(foundKeyword);