Add prefix matching for policy version
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfObjectFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.models.base;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.function.Function;
28 import java.util.function.Predicate;
29 import java.util.regex.Pattern;
30
31 /**
32  * Interface for filtering a list of concepts.
33  *
34  * @author Liam Fallon (liam.fallon@est.tech)
35  */
36 @FunctionalInterface
37 public interface PfObjectFilter<T extends Comparable<T>> {
38     /**
39      * Filter an incoming list, removing items that do not match the filter.
40      *
41      * @param originalList the original list
42      * @return the filtered list
43      */
44     public List<T> filter(final List<T> originalList);
45
46     /**
47      * Check if a value exactly equals some text.
48      *
49      * @param value the incoming value to check
50      * @param text the desired text to check against
51      * @return match or not
52      */
53     public default boolean filterString(final String value, final String text) {
54         return value == null || text == null || value.equals(text);
55     }
56
57     /**
58      * Gets a predicate used to filter an item in a list by exactly matching an extracted value
59      * with some text.
60      *
61      * @param text the desired text to check against, or {@code null} if to accept everything
62      * @param extractor function to extract the value, to be matched, from a list item
63      * @return a predicate to match a value from a list item
64      */
65     public default Predicate<T> filterStringPred(final String text, Function<T, String> extractor) {
66         // if null text, then everything matches
67         if (text == null) {
68             return item -> true;
69         }
70
71         return item -> text.equals(extractor.apply(item));
72     }
73
74     /**
75      * Gets a predicate used to filter an item in a list by comparing the start of an
76      * extracted value with a prefix.
77      *
78      * @param prefix the desired prefix to check against, or {@code null} if to accept
79      *        everything
80      * @param extractor function to extract the value, to be matched, from a list item
81      * @return a predicate to match a prefix with a value from a list item
82      */
83     public default Predicate<T> filterPrefixPred(final String prefix, Function<T, String> extractor) {
84         // if null prefix, then everything matches
85         if (prefix == null) {
86             return item -> true;
87         }
88
89         return item -> {
90             String value = extractor.apply(item);
91             return (value != null && value.startsWith(prefix));
92         };
93     }
94
95     /**
96      * Gets a predicate used to filter an item in a list by matching an extracted value
97      * with a regular expression.
98      *
99      * @param pattern regular expression to match, or {@code null} if to accept everything
100      * @param extractor function to extract the value, to be matched, from a list item
101      * @return a predicate to match a value from a list item using a regular expression
102      */
103     public default Predicate<T> filterRegexpPred(final String pattern, Function<T, String> extractor) {
104         // if null pattern, then everything matches
105         if (pattern == null) {
106             return item -> true;
107         }
108
109         Pattern pat = Pattern.compile(pattern);
110
111         return item -> {
112             String value = extractor.apply(item);
113             return (value != null && pat.matcher(value).matches());
114         };
115     }
116
117     /**
118      * Sort an incoming list and remove all but the latest version of each concept.
119      *
120      * @param originalList the incoming list
121      * @return the filtered list
122      */
123     public default List<T> latestVersionFilter(final List<T> originalList) {
124         if (originalList.size() <= 1) {
125             return originalList;
126         }
127
128         List<T> filteredList = new ArrayList<>(originalList);
129         Collections.sort(filteredList);
130
131         int icur = 0;
132         for (int j = 1; j < filteredList.size(); j++) {
133             // Get the current and last element
134             T curElement = filteredList.get(icur);
135             T lastElement = filteredList.get(j);
136
137             /*
138              * The list is sorted so if the last element name is the same as the current element name, the current
139              * element should be removed.
140              */
141             if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
142                 // have a new name - done comparing with the old "current"
143                 ++icur;
144             }
145
146             filteredList.set(icur, lastElement);
147         }
148
149         return new ArrayList<>(filteredList.subList(0, icur + 1));
150     }
151 }