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