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