X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-base%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fbase%2FPfObjectFilter.java;h=f4e457192901e4e46914100f654250f34bec7faf;hb=309c49634bf9fcb53458d3e5f07684cb7bfbdd6f;hp=a7d8401f0a660eb4537d29f309ee6cc2398b0a38;hpb=c7ebd4c91bdbf00faed2b9935b818c781803edd1;p=policy%2Fmodels.git 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 a7d8401f0..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 @@ -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. @@ -23,8 +24,9 @@ package org.onap.policy.models.base; import java.util.ArrayList; import java.util.Collections; import java.util.List; - -import lombok.NonNull; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.regex.Pattern; /** * Interface for filtering a list of concepts. @@ -42,14 +44,74 @@ public interface PfObjectFilter> { public List filter(final List 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 regexp the regular expression to check against + * @param text the desired text 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 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 filterStringPred(final String text, Function 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 filterPrefixPred(final String prefix, Function 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 filterRegexpPred(final String pattern, Function 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()); + }; } /** @@ -73,10 +135,10 @@ public interface PfObjectFilter> { T lastElement = filteredList.get(j); /* - * The list is sorted so if the last element name is the same as the current - * element name, the current element should be removed. + * 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())) { + if (!((PfNameVersion) curElement).getName().equals(((PfNameVersion) lastElement).getName())) { // have a new name - done comparing with the old "current" ++icur; }