/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
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.
*
public String getVersion();
public void setVersion(final String version);
+
+ /**
+ * Get the defined name for a concept, return null if no name is defined.
+ *
+ * @return the defined name
+ */
+ public default String getDefinedName() {
+ return getName();
+ }
+
+ /**
+ * Get the defined version for a concept, return null if no version is defined.
+ *
+ * @return the defined version
+ */
+ public default String getDefinedVersion() {
+ return getVersion();
+ }
+
+ /**
+ * 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()));
+ }
}