X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-base%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fbase%2FPfReferenceKey.java;h=246f8f069c4f1265e24e38d702c11d4972706692;hb=938005505883cf7a636a8840e20e3dc8a0ad9176;hp=2e9f48ba5b8bb12f366e3b89edd25cde7d4ad9b7;hpb=4c28d2cdbf03be9dfe51caa05d45ba341b4c94cd;p=policy%2Fmodels.git diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java index 2e9f48ba5..246f8f069 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceKey.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019, 2021, 2023 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 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. @@ -20,17 +21,17 @@ package org.onap.policy.models.base; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import java.io.Serial; import java.util.ArrayList; import java.util.List; - -import javax.persistence.Column; -import javax.persistence.Embeddable; - import lombok.Data; import lombok.EqualsAndHashCode; - +import lombok.NonNull; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.common.parameters.annotations.Pattern; import org.onap.policy.common.utils.validation.Assertions; -import org.onap.policy.models.base.PfValidationResult.ValidationResult; /** * A reference key identifies entities in the system that are contained in other entities. Every contained concept in @@ -58,30 +59,43 @@ public class PfReferenceKey extends PfKey { private static final String PARENT_LOCAL_NAME = "parentLocalName"; private static final String LOCAL_NAME = "localName"; + @Serial private static final long serialVersionUID = 8932717618579392561L; - /** Regular expression to specify the structure of local names in reference keys. */ + /** + * Regular expression to specify the structure of local names in reference keys. + */ public static final String LOCAL_NAME_REGEXP = "[A-Za-z0-9\\-_\\.]+|^$"; - /** Regular expression to specify the structure of IDs in reference keys. */ + /** + * Regular expression to specify the structure of IDs in reference keys. + */ public static final String REFERENCE_KEY_ID_REGEXP = - "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+"; + "[A-Za-z0-9\\-_]+:[0-9].[0-9].[0-9]:[A-Za-z0-9\\-_]+:[A-Za-z0-9\\-_]+"; private static final int PARENT_NAME_FIELD = 0; private static final int PARENT_VERSION_FIELD = 1; private static final int PARENT_LOCAL_NAME_FIELD = 2; private static final int LOCAL_NAME_FIELD = 3; - @Column(name = PARENT_KEY_NAME) + @Column(name = PARENT_KEY_NAME, length = 120) + @NotNull + @Pattern(regexp = NAME_REGEXP) private String parentKeyName; - @Column(name = PARENT_KEY_VERSION) + @Column(name = PARENT_KEY_VERSION, length = 15) + @NotNull + @Pattern(regexp = VERSION_REGEXP) private String parentKeyVersion; - @Column(name = PARENT_LOCAL_NAME) + @Column(name = PARENT_LOCAL_NAME, length = 120) + @NotNull + @Pattern(regexp = LOCAL_NAME_REGEXP) private String parentLocalName; - @Column(name = LOCAL_NAME) + @Column(name = LOCAL_NAME, length = 120) + @NotNull + @Pattern(regexp = LOCAL_NAME_REGEXP) private String localName; /** @@ -94,19 +108,17 @@ public class PfReferenceKey extends PfKey { /** * The Copy Constructor creates a key by copying another key. * - * @param referenceKey - * the reference key to copy from + * @param referenceKey the reference key to copy from */ public PfReferenceKey(final PfReferenceKey referenceKey) { this(referenceKey.getParentKeyName(), referenceKey.getParentKeyVersion(), referenceKey.getParentLocalName(), - referenceKey.getLocalName()); + referenceKey.getLocalName()); } /** * Constructor to create a null reference key for the specified parent concept key. * - * @param pfConceptKey - * the parent concept key of this reference key + * @param pfConceptKey the parent concept key of this reference key */ public PfReferenceKey(final PfConceptKey pfConceptKey) { this(pfConceptKey.getName(), pfConceptKey.getVersion(), NULL_KEY_NAME, NULL_KEY_NAME); @@ -115,10 +127,8 @@ public class PfReferenceKey extends PfKey { /** * Constructor to create a reference key for the given parent concept key with the given local name. * - * @param pfConceptKey - * the parent concept key of this reference key - * @param localName - * the local name of this reference key + * @param pfConceptKey the parent concept key of this reference key + * @param localName the local name of this reference key */ public PfReferenceKey(final PfConceptKey pfConceptKey, final String localName) { this(pfConceptKey, NULL_KEY_NAME, localName); @@ -127,10 +137,8 @@ public class PfReferenceKey extends PfKey { /** * Constructor to create a reference key for the given parent reference key with the given local name. * - * @param parentReferenceKey - * the parent reference key of this reference key - * @param localName - * the local name of this reference key + * @param parentReferenceKey the parent reference key of this reference key + * @param localName the local name of this reference key */ public PfReferenceKey(final PfReferenceKey parentReferenceKey, final String localName) { this(parentReferenceKey.getParentConceptKey(), parentReferenceKey.getLocalName(), localName); @@ -140,12 +148,9 @@ public class PfReferenceKey extends PfKey { * Constructor to create a reference key for the given parent reference key (specified by the parent reference key's * concept key and local name) with the given local name. * - * @param pfConceptKey - * the concept key of the parent reference key of this reference key - * @param parentLocalName - * the local name of the parent reference key of this reference key - * @param localName - * the local name of this reference key + * @param pfConceptKey the concept key of the parent reference key of this reference key + * @param parentLocalName the local name of the parent reference key of this reference key + * @param localName the local name of this reference key */ public PfReferenceKey(final PfConceptKey pfConceptKey, final String parentLocalName, final String localName) { this(pfConceptKey.getName(), pfConceptKey.getVersion(), parentLocalName, localName); @@ -155,12 +160,9 @@ public class PfReferenceKey extends PfKey { * Constructor to create a reference key for the given parent concept key (specified by the parent concept key's * name and version) with the given local name. * - * @param parentKeyName - * the name of the parent concept key of this reference key - * @param parentKeyVersion - * the version of the parent concept key of this reference key - * @param localName - * the local name of this reference key + * @param parentKeyName the name of the parent concept key of this reference key + * @param parentKeyVersion the version of the parent concept key of this reference key + * @param localName the local name of this reference key */ public PfReferenceKey(final String parentKeyName, final String parentKeyVersion, final String localName) { this(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName); @@ -170,34 +172,29 @@ public class PfReferenceKey extends PfKey { * Constructor to create a reference key for the given parent key (specified by the parent key's name, version nad * local name) with the given local name. * - * @param parentKeyName - * the parent key name of this reference key - * @param parentKeyVersion - * the parent key version of this reference key - * @param parentLocalName - * the parent local name of this reference key - * @param localName - * the local name of this reference key + * @param parentKeyName the parent key name of this reference key + * @param parentKeyVersion the parent key version of this reference key + * @param parentLocalName the parent local name of this reference key + * @param localName the local name of this reference key */ public PfReferenceKey(final String parentKeyName, final String parentKeyVersion, final String parentLocalName, - final String localName) { + final String localName) { super(); this.parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, parentKeyName, NAME_REGEXP); this.parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, parentKeyVersion, - VERSION_REGEXP); + VERSION_REGEXP); this.parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, parentLocalName, - LOCAL_NAME_REGEXP); + LOCAL_NAME_REGEXP); this.localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP); } /** * Constructor to create a key from the specified key ID. * - * @param id - * the key ID in a format that respects the KEY_ID_REGEXP + * @param id the key ID in a format that respects the KEY_ID_REGEXP */ public PfReferenceKey(final String id) { - final String conditionedId = Assertions.validateStringParameter("id", id, REFERENCE_KEY_ID_REGEXP); + final var conditionedId = Assertions.validateStringParameter("id", id, REFERENCE_KEY_ID_REGEXP); // Split on colon, if the id passes the regular expression test above // it'll have just three colons separating the parent name, @@ -207,13 +204,13 @@ public class PfReferenceKey extends PfKey { // Initiate the new key parentKeyName = Assertions.validateStringParameter(PARENT_KEY_NAME, nameVersionNameArray[PARENT_NAME_FIELD], - NAME_REGEXP); + NAME_REGEXP); parentKeyVersion = Assertions.validateStringParameter(PARENT_KEY_VERSION, - nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP); + nameVersionNameArray[PARENT_VERSION_FIELD], VERSION_REGEXP); parentLocalName = Assertions.validateStringParameter(PARENT_LOCAL_NAME, - nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP); + nameVersionNameArray[PARENT_LOCAL_NAME_FIELD], LOCAL_NAME_REGEXP); localName = Assertions.validateStringParameter(LOCAL_NAME, nameVersionNameArray[LOCAL_NAME_FIELD], - LOCAL_NAME_REGEXP); + LOCAL_NAME_REGEXP); } /** @@ -223,7 +220,7 @@ public class PfReferenceKey extends PfKey { */ public static PfReferenceKey getNullKey() { return new PfReferenceKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME, - PfKey.NULL_KEY_NAME); + PfKey.NULL_KEY_NAME); } @Override @@ -245,7 +242,9 @@ public class PfReferenceKey extends PfKey { @Override public boolean isNullKey() { - return this.equals(PfReferenceKey.getNullKey()); + return (PfKey.NULL_KEY_NAME.equals(this.getParentKeyName()) && PfKey.NULL_KEY_VERSION + .equals(this.getParentKeyVersion()) && PfKey.NULL_KEY_NAME.equals(this.getParentLocalName()) + && PfKey.NULL_KEY_NAME.equals(this.getLocalName())); } /** @@ -269,8 +268,7 @@ public class PfReferenceKey extends PfKey { /** * Sets the parent concept key of this reference key. * - * @param parentKey - * the parent concept key of this reference key + * @param parentKey the parent concept key of this reference key */ public void setParentConceptKey(final PfConceptKey parentKey) { Assertions.argumentNotNull(parentKey, "parentKey may not be null"); @@ -283,8 +281,7 @@ public class PfReferenceKey extends PfKey { /** * Sets the parent reference key of this reference key. * - * @param parentKey - * the parent reference key of this reference key + * @param parentKey the parent reference key of this reference key */ public void setParentReferenceKey(final PfReferenceKey parentKey) { Assertions.argumentNotNull(parentKey, "parentKey may not be null"); @@ -295,56 +292,46 @@ public class PfReferenceKey extends PfKey { } @Override - public PfKey.Compatibility getCompatibility(final PfKey otherKey) { - if (!(otherKey instanceof PfReferenceKey)) { + public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) { + if (!(otherKey instanceof PfReferenceKey otherReferenceKey)) { return Compatibility.DIFFERENT; } - final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey; return this.getParentConceptKey().getCompatibility(otherReferenceKey.getParentConceptKey()); } @Override - public boolean isCompatible(final PfKey otherKey) { - if (!(otherKey instanceof PfReferenceKey)) { + public boolean isCompatible(@NonNull final PfKey otherKey) { + if (!(otherKey instanceof PfReferenceKey otherReferenceKey)) { return false; } - final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey; return this.getParentConceptKey().isCompatible(otherReferenceKey.getParentConceptKey()); } @Override - public PfValidationResult validate(final PfValidationResult result) { - final String parentNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(PARENT_KEY_NAME, - parentKeyName, NAME_REGEXP); - if (parentNameValidationErrorMessage != null) { - result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentKeyName invalid-" + parentNameValidationErrorMessage)); - } + public int getMajorVersion() { + return this.getParentConceptKey().getMajorVersion(); + } - final String parentKeyVersionValidationErrorMessage = Assertions - .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP); - if (parentKeyVersionValidationErrorMessage != null) { - result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage)); - } + @Override + public int getMinorVersion() { + return this.getParentConceptKey().getMinorVersion(); + } - final String parentLocalNameValidationErrorMessage = Assertions - .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP); - if (parentLocalNameValidationErrorMessage != null) { - result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "parentLocalName invalid-" + parentLocalNameValidationErrorMessage)); - } + @Override + public int getPatchVersion() { + return this.getParentConceptKey().getPatchVersion(); + } - final String localNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(LOCAL_NAME, - localName, LOCAL_NAME_REGEXP); - if (localNameValidationErrorMessage != null) { - result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID, - "localName invalid-" + localNameValidationErrorMessage)); - } - return result; + @Override + public boolean isNewerThan(@NonNull final PfKey otherKey) { + Assertions.instanceOf(otherKey, PfReferenceKey.class); + + final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey; + + return this.getParentConceptKey().isNewerThan(otherReferenceKey.getParentConceptKey()); } @Override @@ -356,30 +343,14 @@ public class PfReferenceKey extends PfKey { } @Override - public PfConcept copyTo(final PfConcept target) { - Assertions.argumentNotNull(target, "target may not be null"); - - final Object copyObject = target; - Assertions.instanceOf(copyObject, PfReferenceKey.class); - - final PfReferenceKey copy = ((PfReferenceKey) copyObject); - copy.setParentKeyName(parentKeyName); - copy.setParentKeyVersion(parentKeyVersion); - copy.setLocalName(localName); - copy.setParentLocalName(parentLocalName); - - return copy; - } - - @Override - public int compareTo(final PfConcept otherObj) { + public int compareTo(@NonNull final PfConcept otherObj) { Assertions.argumentNotNull(otherObj, "comparison object may not be null"); if (this == otherObj) { return 0; } if (getClass() != otherObj.getClass()) { - return this.hashCode() - otherObj.hashCode(); + return getClass().getName().compareTo(otherObj.getClass().getName()); } final PfReferenceKey other = (PfReferenceKey) otherObj;