Add unit tests for models-base
[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         List<T> filteredList = new ArrayList<>(originalList);
63         Collections.sort(filteredList);
64
65         List<T> filteredOutList = new ArrayList<>();
66
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);
71
72             // The list is sorted so if the last element name is the same as the current element name, the last element
73             // should be removed
74             if (((PfNameVersion)thisElement).getName().equals(((PfNameVersion)lastElement).getName())) {
75                 filteredOutList.add(lastElement);
76             }
77         }
78
79         // We can now remove these elements
80         filteredList.removeAll(filteredOutList);
81
82         return filteredList;
83     }
84 }