aboutsummaryrefslogtreecommitdiffstats
path: root/models-base/src/main
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-04-13 09:05:13 -0400
committerJim Hahn <jrh3@att.com>2019-04-14 16:43:51 -0400
commitc8d0a3d8b7df3b09ce3405f1192754fdef7026d3 (patch)
tree27651921942e65b243f0d4110592b759d7afd2d8 /models-base/src/main
parentc38fcf1a16f0d6bfdf2d80efa64a94ae68d80f03 (diff)
Add prefix matching for policy version
Re-introduced regular expressions to match policy version so that the policy version found in the metadata, which is just the major number, can be matched with a policy's version, which is of the form, major.minor.patch. Simplified equals() test, as we already know "text!=null". Added filterPrefixPred() and modified policy filter to use it when matching versions, as it is simpler to use when matching a version prefix. Also added tests to PfObjectFilterTest to test each of the XxxPred() methods. Change-Id: I0734873f701a539e883fe25b8eecfdde60cc8b6d Issue-ID: POLICY-1542 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-base/src/main')
-rw-r--r--models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java71
1 files changed, 67 insertions, 4 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java
index 35319b4fd..f4e457192 100644
--- a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java
+++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java
@@ -24,6 +24,9 @@ package org.onap.policy.models.base;
import java.util.ArrayList;
import java.util.Collections;
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.
@@ -41,14 +44,74 @@ 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());
+ };
}
/**