Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfSearchableKey.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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 jakarta.persistence.Column;
25 import jakarta.persistence.Embeddable;
26 import java.io.Serial;
27 import lombok.EqualsAndHashCode;
28 import lombok.Getter;
29 import lombok.NonNull;
30 import org.onap.policy.common.parameters.annotations.Pattern;
31 import org.onap.policy.common.utils.validation.Assertions;
32
33 /**
34  * A key that is used to search for other concept keys. The name field may contain a
35  * trailing ".*", to indicate wild-card matching.
36  */
37 @Embeddable
38 @Getter
39 @EqualsAndHashCode(callSuper = false)
40 public class PfSearchableKey extends PfKeyImpl {
41     @Serial
42     private static final long serialVersionUID = 8932717618579392561L;
43
44     /** Regular expression to specify the structure of key names. */
45     public static final String WILDCARD_NAME_REGEXP = "^[A-Za-z0-9\\-_\\.]+(?:\\.\\*)?$";
46
47     @Column(name = NAME_TOKEN, length = 120)
48     @Pattern(regexp = WILDCARD_NAME_REGEXP)
49     private String name;
50
51     @Column(name = VERSION_TOKEN, length = 20)
52     @Pattern(regexp = VERSION_REGEXP)
53     private String version;
54
55     /**
56      * The default constructor creates a null key.
57      */
58     public PfSearchableKey() {
59         this(NULL_KEY_NAME, NULL_KEY_VERSION);
60     }
61
62     /**
63      * Copy constructor.
64      *
65      * @param copyKey the key to copy from
66      */
67     public PfSearchableKey(final PfSearchableKey copyKey) {
68         super(copyKey);
69     }
70
71     /**
72      * Constructor to create a key with the specified name and version.
73      *
74      * @param name the key name
75      * @param version the key version
76      */
77     public PfSearchableKey(final String name, final String version) {
78         super(name, version);
79     }
80
81     /**
82      * Constructor to create a key using the key and version from the specified key ID.
83      *
84      * @param id the key ID in a format that respects the KEY_ID_REGEXP
85      */
86     public PfSearchableKey(final String id) {
87         super(id);
88     }
89
90     public void setName(@NonNull final String name) {
91         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
92     }
93
94     public void setVersion(@NonNull final String version) {
95         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
96     }
97
98     /**
99      * Get a null key.
100      *
101      * @return a null key
102      */
103     public static PfSearchableKey getNullKey() {
104         return new PfSearchableKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION);
105     }
106
107     @Override
108     protected String getNameRegEx() {
109         return WILDCARD_NAME_REGEXP;
110     }
111
112     @Override
113     public String toString() {
114         return "PfSearchableKey(name=" + getName() + ", version=" + getVersion() + ")";
115     }
116 }