Add unit tests for policy type and policy filters
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfObjectFilter.java
index a377c24..10ce4ea 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -45,11 +46,11 @@ public interface PfObjectFilter<T extends Comparable<T>> {
      * Check if a value matches a regular expression.
      *
      * @param value the incoming value to check
-     * @param regexp the regular expression to check against
+     * @param pattern the pattern to check against
      * @return match or not
      */
-    public default boolean filterOnRegexp(@NonNull final String value, @NonNull final String regexp) {
-        return value.matches(regexp);
+    public default boolean filterString(final String value, final String pattern) {
+        return value == null || pattern == null || value.equals(pattern);
     }
 
     /**
@@ -59,26 +60,31 @@ public interface PfObjectFilter<T extends Comparable<T>> {
      * @return the filtered list
      */
     public default List<T> latestVersionFilter(final List<T> originalList) {
-        List<T> filteredList = new ArrayList<>();
-        Collections.sort(filteredList);
+        if (originalList.size() <= 1) {
+            return originalList;
+        }
 
-        List<T> filteredOutList = new ArrayList<>();
+        List<T> filteredList = new ArrayList<>(originalList);
+        Collections.sort(filteredList);
 
-        for (int i = 1; i < filteredList.size(); i++) {
+        int icur = 0;
+        for (int j = 1; j < filteredList.size(); j++) {
             // Get the current and last element
-            T thisElement = filteredList.get(i);
-            T lastElement = filteredList.get(i - 1);
+            T curElement = filteredList.get(icur);
+            T lastElement = filteredList.get(j);
 
-            // The list is sorted so if the last element name is the same as the current element name, the last element
-            // should be removed
-            if (((PfNameVersion)thisElement).getName().equals(((PfNameVersion)lastElement).getName())) {
-                filteredOutList.add(lastElement);
+            /*
+             * The list is sorted so if the last element name is the same as the current element name, the current
+             * element should be removed.
+             */
+            if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) {
+                // have a new name - done comparing with the old "current"
+                ++icur;
             }
-        }
 
-        // We can now remove these elements
-        filteredList.removeAll(filteredOutList);
+            filteredList.set(icur, lastElement);
+        }
 
-        return filteredList;
+        return new ArrayList<>(filteredList.subList(0, icur + 1));
     }
 }