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;
28 import lombok.NonNull;
31 * Interface for filtering a list of concepts.
33 * @author Liam Fallon (liam.fallon@est.tech)
36 public interface PfObjectFilter<T extends Comparable<T>> {
38 * Filter an incoming list, removing items that do not match the filter.
40 * @param originalList the original list
41 * @return the filtered list
43 public List<T> filter(final List<T> originalList);
46 * Check if a value matches a regular expression.
48 * @param value the incoming value to check
49 * @param pattern the pattern to check against
50 * @return match or not
52 public default boolean filterString(@NonNull final String value, final String pattern) {
53 return pattern == null || value.equals(pattern);
57 * Sort an incoming list and remove all but the latest version of each concept.
59 * @param originalList the incoming list
60 * @return the filtered list
62 public default List<T> latestVersionFilter(final List<T> originalList) {
63 if (originalList.size() <= 1) {
67 List<T> filteredList = new ArrayList<>(originalList);
68 Collections.sort(filteredList);
71 for (int j = 1; j < filteredList.size(); j++) {
72 // Get the current and last element
73 T curElement = filteredList.get(icur);
74 T lastElement = filteredList.get(j);
77 * The list is sorted so if the last element name is the same as the current element name, the current
78 * element should be removed.
80 if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
81 // have a new name - done comparing with the old "current"
85 filteredList.set(icur, lastElement);
88 return new ArrayList<>(filteredList.subList(0, icur + 1));