Use lombok annotations in apex-pdp
[policy/apex-pdp.git] / model / utilities / src / main / java / org / onap / policy / apex / model / utilities / TreeMapUtils.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.utilities;
23
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;
31
32 /**
33  * This class provides utility functions for tree maps. A function to find the nearest match in the tree map to an input
34  * string is provided.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 @NoArgsConstructor(access = AccessLevel.PRIVATE)
39 public final class TreeMapUtils {
40
41     /**
42      * Find the list of entries that matches a given word, for example "p" will match "put", "policy", and "push".
43      *
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}
48      */
49     public static <T> List<Entry<String, T>> findMatchingEntries(final NavigableMap<String, T> searchMap,
50             final String word) {
51         final List<Entry<String, T>> foundNodes = new ArrayList<>();
52
53         // A straight match check
54         if (searchMap.containsKey(word)) {
55             foundNodes.add(new SimpleEntry<>(word, searchMap.get(word)));
56             return foundNodes;
57         }
58
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();
63         } else {
64             foundKeyword = searchMap.higherKey(foundKeyword);
65         }
66
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);
72             } else {
73                 break;
74             }
75         }
76         return foundNodes;
77     }
78 }