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
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.models.base;
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;
33 * Interface for filtering a list of concepts.
35 * @author Liam Fallon (liam.fallon@est.tech)
38 public interface PfObjectFilter<T> {
40 * Filter an incoming list, removing items that do not match the filter.
42 * @param originalList the original list
43 * @return the filtered list
45 public List<T> filter(final List<T> originalList);
48 * Check if a value exactly equals some text.
50 * @param value the incoming value to check
51 * @param text the desired text to check against
52 * @return match or not
54 public default boolean filterString(final String value, final String text) {
55 return value == null || text == null || value.equals(text);
59 * Gets a predicate used to filter an item in a list by exactly matching an extracted value
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
66 public default Predicate<T> filterStringPred(final String text, Function<T, String> extractor) {
67 // if null text, then everything matches
72 return item -> text.equals(extractor.apply(item));
76 * Gets a predicate used to filter an item in a list by comparing the start of an
77 * extracted value with a prefix.
79 * @param prefix the desired prefix to check against, or {@code null} if to accept
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
84 public default Predicate<T> filterPrefixPred(final String prefix, Function<T, String> extractor) {
85 // if null prefix, then everything matches
91 String value = extractor.apply(item);
92 return (value != null && value.startsWith(prefix));
97 * Gets a predicate used to filter an item in a list by matching an extracted value
98 * with a regular expression.
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
104 public default Predicate<T> filterRegexpPred(final String pattern, Function<T, String> extractor) {
105 // if null pattern, then everything matches
106 if (pattern == null) {
110 Pattern pat = Pattern.compile(pattern);
113 String value = extractor.apply(item);
114 return (value != null && pat.matcher(value).matches());
119 * Sort an incoming list and remove all but the latest version of each concept.
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
125 public default List<T> latestVersionFilter(final List<T> originalList, final Comparator<T> versionComparator) {
126 if (originalList.size() <= 1) {
130 List<T> filteredList = new ArrayList<>(originalList);
131 Collections.sort(filteredList, versionComparator);
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);
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.
143 if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
144 // have a new name - done comparing with the old "current"
148 filteredList.set(icur, lastElement);
151 return new ArrayList<>(filteredList.subList(0, icur + 1));