/*-
* ============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.
* 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);
}
/**
* @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));
}
}