diff options
Diffstat (limited to 'models-base/src/main/java/org')
3 files changed, 126 insertions, 40 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java b/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java index 47238fc40..7bdced77b 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfNameVersion.java @@ -20,6 +20,9 @@ package org.onap.policy.models.base; +import org.apache.commons.lang3.ObjectUtils; +import org.onap.policy.common.utils.validation.Version; + /** * An interface that forces a POJO to have getName() and getVersion() methods. * @@ -33,4 +36,43 @@ public interface PfNameVersion { public String getVersion(); public void setVersion(final String version); + + /** + * COmpare two name version implementation objects. + * + * @param left the left name/version implementation + * @param right the right name/version implementation + * @return the comparison resilt + */ + public default int compareNameVersion(final PfNameVersion left, final PfNameVersion right) { + if (left == null && right == null) { + return 0; + } + + if (left == null) { + return 1; + } + + if (right == null) { + return -1; + } + + int result = ObjectUtils.compare(left.getName(), right.getName()); + if (result != 0) { + return result; + } + + if (left.getVersion() == null && right.getVersion() == null) { + return 0; + } + + if (left.getVersion() == null) { + return 1; + } + + if (right.getVersion() == null) { + return -1; + } + return new Version(left.getVersion()).compareTo(new Version(right.getVersion())); + } } diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java deleted file mode 100644 index f1481bf3c..000000000 --- a/models-base/src/main/java/org/onap/policy/models/base/PfObjectFiler.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.base; - -import java.util.List; - -/** - * Interface for filtering a list of concepts. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -@FunctionalInterface -public interface PfObjectFiler<T> { - /** - * Filter an incoming list, removing items that do not match the filter. - * - * @param originalList the original list - * @return the filtered list - */ - public List<T> filter(final List<T> originalList); - -} 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 new file mode 100644 index 000000000..a377c24be --- /dev/null +++ b/models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java @@ -0,0 +1,84 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.base; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import lombok.NonNull; + +/** + * Interface for filtering a list of concepts. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +@FunctionalInterface +public interface PfObjectFilter<T extends Comparable<T>> { + /** + * Filter an incoming list, removing items that do not match the filter. + * + * @param originalList the original list + * @return the filtered list + */ + public List<T> filter(final List<T> originalList); + + /** + * Check if a value matches a regular expression. + * + * @param value the incoming value to check + * @param regexp the regular expression to check against + * @return match or not + */ + public default boolean filterOnRegexp(@NonNull final String value, @NonNull final String regexp) { + return value.matches(regexp); + } + + /** + * Sort an incoming list and remove all but the latest version of each concept. + * + * @param originalList the incoming list + * @return the filtered list + */ + public default List<T> latestVersionFilter(final List<T> originalList) { + List<T> filteredList = new ArrayList<>(); + Collections.sort(filteredList); + + List<T> filteredOutList = new ArrayList<>(); + + for (int i = 1; i < filteredList.size(); i++) { + // Get the current and last element + T thisElement = filteredList.get(i); + T lastElement = filteredList.get(i - 1); + + // 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); + } + } + + // We can now remove these elements + filteredList.removeAll(filteredOutList); + + return filteredList; + } +} |