2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import lombok.NonNull;
30 * Interface for filtering a list of concepts.
32 * @author Liam Fallon (liam.fallon@est.tech)
35 public interface PfObjectFilter<T extends Comparable<T>> {
37 * Filter an incoming list, removing items that do not match the filter.
39 * @param originalList the original list
40 * @return the filtered list
42 public List<T> filter(final List<T> originalList);
45 * Check if a value matches a regular expression.
47 * @param value the incoming value to check
48 * @param regexp the regular expression to check against
49 * @return match or not
51 public default boolean filterOnRegexp(@NonNull final String value, @NonNull final String regexp) {
52 return value.matches(regexp);
56 * Sort an incoming list and remove all but the latest version of each concept.
58 * @param originalList the incoming list
59 * @return the filtered list
61 public default List<T> latestVersionFilter(final List<T> originalList) {
62 List<T> filteredList = new ArrayList<>();
63 Collections.sort(filteredList);
65 List<T> filteredOutList = new ArrayList<>();
67 for (int i = 1; i < filteredList.size(); i++) {
68 // Get the current and last element
69 T thisElement = filteredList.get(i);
70 T lastElement = filteredList.get(i - 1);
72 // The list is sorted so if the last element name is the same as the current element name, the last element
74 if (((PfNameVersion)thisElement).getName().equals(((PfNameVersion)lastElement).getName())) {
75 filteredOutList.add(lastElement);
79 // We can now remove these elements
80 filteredList.removeAll(filteredOutList);