Add extra authorative TOSCA concepts
[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
60      * with some text.
61      *
62      * @param text the desired text to check against, or {@code null} if to accept everything
63      * @param extractor function to extract the value, to be matched, from a list item
64      * @return a predicate to match a value from a list item
65      */
66     public default Predicate<T> filterStringPred(final String text, Function<T, String> extractor) {
67         // if null text, then everything matches
68         if (text == null) {
69             return item -> true;
70         }
71
72         return item -> text.equals(extractor.apply(item));
73     }
74
75     /**
76      * Gets a predicate used to filter an item in a list by comparing the start of an
77      * extracted value with a prefix.
78      *
79      * @param prefix the desired prefix to check against, or {@code null} if to accept
80      *        everything
81      * @param extractor function to extract the value, to be matched, from a list item
82      * @return a predicate to match a prefix with a value from a list item
83      */
84     public default Predicate<T> filterPrefixPred(final String prefix, Function<T, String> extractor) {
85         // if null prefix, then everything matches
86         if (prefix == null) {
87             return item -> true;
88         }
89
90         return item -> {
91             String value = extractor.apply(item);
92             return (value != null && value.startsWith(prefix));
93         };
94     }
95
96     /**
97      * Gets a predicate used to filter an item in a list by matching an extracted value
98      * with a regular expression.
99      *
100      * @param pattern regular expression to match, or {@code null} if to accept everything
101      * @param extractor function to extract the value, to be matched, from a list item
102      * @return a predicate to match a value from a list item using a regular expression
103      */
104     public default Predicate<T> filterRegexpPred(final String pattern, Function<T, String> extractor) {
105         // if null pattern, then everything matches
106         if (pattern == null) {
107             return item -> true;
108         }
109
110         Pattern pat = Pattern.compile(pattern);
111
112         return item -> {
113             String value = extractor.apply(item);
114             return (value != null && pat.matcher(value).matches());
115         };
116     }
117
118     /**
119      * Sort an incoming list and remove all but the latest version of each concept.
120      *
121      * @param originalList the incoming list
122      * @param versionComparator the comparator to use to order versions of the incoming object
123      * @return the filtered list
124      */
125     public default List<T> latestVersionFilter(final List<T> originalList, final Comparator<T> versionComparator) {
126         if (originalList.size() <= 1) {
127             return originalList;
128         }
129
130         List<T> filteredList = new ArrayList<>(originalList);
131         Collections.sort(filteredList, versionComparator);
132
133         int icur = 0;
134         for (int j = 1; j < filteredList.size(); j++) {
135             // Get the current and last element
136             T curElement = filteredList.get(icur);
137             T lastElement = filteredList.get(j);
138
139             /*
140              * The list is sorted so if the last element name is the same as the current element name, the current
141              * element should be removed.
142              */
143             if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
144                 // have a new name - done comparing with the old "current"
145                 ++icur;
146             }
147
148             filteredList.set(icur, lastElement);
149         }
150
151         return new ArrayList<>(filteredList.subList(0, icur + 1));
152     }
153 }