Finish unit test on policy-models
[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 /**
29  * Interface for filtering a list of concepts.
30  *
31  * @author Liam Fallon (liam.fallon@est.tech)
32  */
33 @FunctionalInterface
34 public interface PfObjectFilter<T extends Comparable<T>> {
35     /**
36      * Filter an incoming list, removing items that do not match the filter.
37      *
38      * @param originalList the original list
39      * @return the filtered list
40      */
41     public List<T> filter(final List<T> originalList);
42
43     /**
44      * Check if a value matches a regular expression.
45      *
46      * @param value the incoming value to check
47      * @param pattern the pattern to check against
48      * @return match or not
49      */
50     public default boolean filterString(final String value, final String pattern) {
51         return value == null || pattern == null || value.equals(pattern);
52     }
53
54     /**
55      * Sort an incoming list and remove all but the latest version of each concept.
56      *
57      * @param originalList the incoming list
58      * @return the filtered list
59      */
60     public default List<T> latestVersionFilter(final List<T> originalList) {
61         if (originalList.size() <= 1) {
62             return originalList;
63         }
64
65         List<T> filteredList = new ArrayList<>(originalList);
66         Collections.sort(filteredList);
67
68         int icur = 0;
69         for (int j = 1; j < filteredList.size(); j++) {
70             // Get the current and last element
71             T curElement = filteredList.get(icur);
72             T lastElement = filteredList.get(j);
73
74             /*
75              * The list is sorted so if the last element name is the same as the current element name, the current
76              * element should be removed.
77              */
78             if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
79                 // have a new name - done comparing with the old "current"
80                 ++icur;
81             }
82
83             filteredList.set(icur, lastElement);
84         }
85
86         return new ArrayList<>(filteredList.subList(0, icur + 1));
87     }
88 }