Add extra authorative TOSCA concepts
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfObjectFilter.java
index 35319b4..f7e29f1 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019-2020 Nordix Foundation.
  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -23,7 +23,11 @@ package org.onap.policy.models.base;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.regex.Pattern;
 
 /**
  * Interface for filtering a list of concepts.
@@ -31,7 +35,7 @@ import java.util.List;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 @FunctionalInterface
-public interface PfObjectFilter<T extends Comparable<T>> {
+public interface PfObjectFilter<T> {
     /**
      * Filter an incoming list, removing items that do not match the filter.
      *
@@ -41,29 +45,90 @@ public interface PfObjectFilter<T extends Comparable<T>> {
     public List<T> filter(final List<T> originalList);
 
     /**
-     * Check if a value matches a regular expression.
+     * Check if a value exactly equals some text.
      *
      * @param value the incoming value to check
-     * @param pattern the pattern to check against
+     * @param text the desired text to check against
      * @return match or not
      */
-    public default boolean filterString(final String value, final String pattern) {
-        return value == null || pattern == null || value.equals(pattern);
+    public default boolean filterString(final String value, final String text) {
+        return value == null || text == null || value.equals(text);
+    }
+
+    /**
+     * Gets a predicate used to filter an item in a list by exactly matching an extracted value
+     * with some text.
+     *
+     * @param text the desired text to check against, or {@code null} if to accept everything
+     * @param extractor function to extract the value, to be matched, from a list item
+     * @return a predicate to match a value from a list item
+     */
+    public default Predicate<T> filterStringPred(final String text, Function<T, String> extractor) {
+        // if null text, then everything matches
+        if (text == null) {
+            return item -> true;
+        }
+
+        return item -> text.equals(extractor.apply(item));
+    }
+
+    /**
+     * Gets a predicate used to filter an item in a list by comparing the start of an
+     * extracted value with a prefix.
+     *
+     * @param prefix the desired prefix to check against, or {@code null} if to accept
+     *        everything
+     * @param extractor function to extract the value, to be matched, from a list item
+     * @return a predicate to match a prefix with a value from a list item
+     */
+    public default Predicate<T> filterPrefixPred(final String prefix, Function<T, String> extractor) {
+        // if null prefix, then everything matches
+        if (prefix == null) {
+            return item -> true;
+        }
+
+        return item -> {
+            String value = extractor.apply(item);
+            return (value != null && value.startsWith(prefix));
+        };
+    }
+
+    /**
+     * Gets a predicate used to filter an item in a list by matching an extracted value
+     * with a regular expression.
+     *
+     * @param pattern regular expression to match, or {@code null} if to accept everything
+     * @param extractor function to extract the value, to be matched, from a list item
+     * @return a predicate to match a value from a list item using a regular expression
+     */
+    public default Predicate<T> filterRegexpPred(final String pattern, Function<T, String> extractor) {
+        // if null pattern, then everything matches
+        if (pattern == null) {
+            return item -> true;
+        }
+
+        Pattern pat = Pattern.compile(pattern);
+
+        return item -> {
+            String value = extractor.apply(item);
+            return (value != null && pat.matcher(value).matches());
+        };
     }
 
     /**
      * Sort an incoming list and remove all but the latest version of each concept.
      *
      * @param originalList the incoming list
+     * @param versionComparator the comparator to use to order versions of the incoming object
      * @return the filtered list
      */
-    public default List<T> latestVersionFilter(final List<T> originalList) {
+    public default List<T> latestVersionFilter(final List<T> originalList, final Comparator<T> versionComparator) {
         if (originalList.size() <= 1) {
             return originalList;
         }
 
         List<T> filteredList = new ArrayList<>(originalList);
-        Collections.sort(filteredList);
+        Collections.sort(filteredList, versionComparator);
 
         int icur = 0;
         for (int j = 1; j < filteredList.size(); j++) {