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
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.List;
27 import java.util.function.Function;
28 import java.util.function.Predicate;
29 import java.util.regex.Pattern;
32 * Interface for filtering a list of concepts.
34 * @author Liam Fallon (liam.fallon@est.tech)
37 public interface PfObjectFilter<T extends Comparable<T>> {
39 * Filter an incoming list, removing items that do not match the filter.
41 * @param originalList the original list
42 * @return the filtered list
44 public List<T> filter(final List<T> originalList);
47 * Check if a value exactly equals some text.
49 * @param value the incoming value to check
50 * @param text the desired text to check against
51 * @return match or not
53 public default boolean filterString(final String value, final String text) {
54 return value == null || text == null || value.equals(text);
58 * Gets a predicate used to filter an item in a list by exactly matching an extracted value
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
65 public default Predicate<T> filterStringPred(final String text, Function<T, String> extractor) {
66 // if null text, then everything matches
71 return item -> text.equals(extractor.apply(item));
75 * Gets a predicate used to filter an item in a list by comparing the start of an
76 * extracted value with a prefix.
78 * @param prefix the desired prefix to check against, or {@code null} if to accept
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
83 public default Predicate<T> filterPrefixPred(final String prefix, Function<T, String> extractor) {
84 // if null prefix, then everything matches
90 String value = extractor.apply(item);
91 return (value != null && value.startsWith(prefix));
96 * Gets a predicate used to filter an item in a list by matching an extracted value
97 * with a regular expression.
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
103 public default Predicate<T> filterRegexpPred(final String pattern, Function<T, String> extractor) {
104 // if null pattern, then everything matches
105 if (pattern == null) {
109 Pattern pat = Pattern.compile(pattern);
112 String value = extractor.apply(item);
113 return (value != null && pat.matcher(value).matches());
118 * Sort an incoming list and remove all but the latest version of each concept.
120 * @param originalList the incoming list
121 * @return the filtered list
123 public default List<T> latestVersionFilter(final List<T> originalList) {
124 if (originalList.size() <= 1) {
128 List<T> filteredList = new ArrayList<>(originalList);
129 Collections.sort(filteredList);
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);
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.
141 if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
142 // have a new name - done comparing with the old "current"
146 filteredList.set(icur, lastElement);
149 return new ArrayList<>(filteredList.subList(0, icur + 1));