98b67819e474d3b4316dfdcb40e2ef200030d731
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfConceptKey.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import javax.persistence.Column;
25 import javax.persistence.Embeddable;
26 import lombok.EqualsAndHashCode;
27 import lombok.Getter;
28 import lombok.NonNull;
29 import lombok.ToString;
30 import org.onap.policy.common.parameters.annotations.Pattern;
31 import org.onap.policy.common.utils.validation.Assertions;
32
33 /**
34  * An concept key uniquely identifies every first order entity in the system. Every first order concept in the system
35  * must have an {@link PfConceptKey} to identify it. Concepts that are wholly contained in another concept are
36  * identified using a {@link PfReferenceKey} key.
37  *
38  * <p>Key validation checks that the name and version fields match the NAME_REGEXP and VERSION_REGEXP
39  * regular expressions respectively.
40  */
41 @Embeddable
42 @Getter
43 @EqualsAndHashCode(callSuper = false)
44 @ToString
45 public class PfConceptKey extends PfKeyImpl {
46     private static final long serialVersionUID = 8932717618579392561L;
47
48     @Column(name = NAME_TOKEN, length = 120)
49     @Pattern(regexp = NAME_REGEXP)
50     private String name;
51
52     @Column(name = VERSION_TOKEN, length = 20)
53     @Pattern(regexp = VERSION_REGEXP)
54     private String version;
55
56     /**
57      * The default constructor creates a null concept key.
58      */
59     public PfConceptKey() {
60         this(NULL_KEY_NAME, NULL_KEY_VERSION);
61     }
62
63     /**
64      * Copy constructor.
65      *
66      * @param copyConcept the concept to copy from
67      */
68     public PfConceptKey(final PfConceptKey copyConcept) {
69         super(copyConcept);
70     }
71
72     /**
73      * Constructor to create a key with the specified name and version.
74      *
75      * @param name the key name
76      * @param version the key version
77      */
78     public PfConceptKey(final String name, final String version) {
79         super(name, version);
80     }
81
82     /**
83      * Constructor to create a key using the key and version from the specified key ID.
84      *
85      * @param id the key ID in a format that respects the KEY_ID_REGEXP
86      */
87     public PfConceptKey(final String id) {
88         super(id);
89     }
90
91     public void setName(@NonNull final String name) {
92         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
93     }
94
95     public void setVersion(@NonNull final String version) {
96         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
97     }
98
99     /**
100      * Get a null concept key.
101      *
102      * @return a null concept key
103      */
104     public static final PfConceptKey getNullKey() {
105         return new PfConceptKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
106     }
107 }