Guaranteed linear-time latestVersion()
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfObjectFilter.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26
27 import lombok.NonNull;
28
29 /**
30  * Interface for filtering a list of concepts.
31  *
32  * @author Liam Fallon (liam.fallon@est.tech)
33  */
34 @FunctionalInterface
35 public interface PfObjectFilter<T extends Comparable<T>> {
36     /**
37      * Filter an incoming list, removing items that do not match the filter.
38      *
39      * @param originalList the original list
40      * @return the filtered list
41      */
42     public List<T> filter(final List<T> originalList);
43
44     /**
45      * Check if a value matches a regular expression.
46      *
47      * @param value the incoming value to check
48      * @param regexp the regular expression to check against
49      * @return match or not
50      */
51     public default boolean filterOnRegexp(@NonNull final String value, @NonNull final String regexp) {
52         return value.matches(regexp);
53     }
54
55     /**
56      * Sort an incoming list and remove all but the latest version of each concept.
57      *
58      * @param originalList the incoming list
59      * @return the filtered list
60      */
61     public default List<T> latestVersionFilter(final List<T> originalList) {
62         if (originalList.size() <= 1) {
63             return originalList;
64         }
65
66         List<T> filteredList = new ArrayList<>(originalList);
67         Collections.sort(filteredList);
68
69         int icur = 0;
70         for (int j = 1; j < filteredList.size(); j++) {
71             // Get the current and last element
72             T curElement = filteredList.get(icur);
73             T lastElement = filteredList.get(j);
74
75             /*
76              * The list is sorted so if the last element name is the same as the current
77              * element name, the current element should be removed.
78              */
79             if (!((PfNameVersion)curElement).getName().equals(((PfNameVersion)lastElement).getName())) {
80                 // have a new name - done comparing with the old "current"
81                 ++icur;
82             }
83
84             filteredList.set(icur, lastElement);
85         }
86
87         return new ArrayList<>(filteredList.subList(0, icur + 1));
88     }
89 }