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