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;
29 * Interface for filtering a list of concepts.
31 * @author Liam Fallon (liam.fallon@est.tech)
34 public interface PfObjectFilter<T extends Comparable<T>> {
36 * Filter an incoming list, removing items that do not match the filter.
38 * @param originalList the original list
39 * @return the filtered list
41 public List<T> filter(final List<T> originalList);
44 * Check if a value matches a regular expression.
46 * @param value the incoming value to check
47 * @param pattern the pattern to check against
48 * @return match or not
50 public default boolean filterString(final String value, final String pattern) {
51 return value == null || pattern == null || value.equals(pattern);
55 * Sort an incoming list and remove all but the latest version of each concept.
57 * @param originalList the incoming list
58 * @return the filtered list
60 public default List<T> latestVersionFilter(final List<T> originalList) {
61 if (originalList.size() <= 1) {
65 List<T> filteredList = new ArrayList<>(originalList);
66 Collections.sort(filteredList);
69 for (int j = 1; j < filteredList.size(); j++) {
70 // Get the current and last element
71 T curElement = filteredList.get(icur);
72 T lastElement = filteredList.get(j);
75 * The list is sorted so if the last element name is the same as the current element name, the current
76 * element should be removed.
78 if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
79 // have a new name - done comparing with the old "current"
83 filteredList.set(icur, lastElement);
86 return new ArrayList<>(filteredList.subList(0, icur + 1));