Java 17 Upgrade
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfTimestampKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2021, 2023 Nordix Foundation.
6  * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.models.base;
25
26 import jakarta.persistence.Column;
27 import jakarta.persistence.Embeddable;
28 import jakarta.persistence.Temporal;
29 import jakarta.persistence.TemporalType;
30 import java.io.Serial;
31 import java.time.Instant;
32 import java.util.Date;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.NonNull;
36 import org.onap.policy.common.parameters.annotations.Pattern;
37 import org.onap.policy.common.utils.validation.Assertions;
38
39 @Embeddable
40 @Data
41 @EqualsAndHashCode(callSuper = false)
42 public class PfTimestampKey extends PfKeyImpl {
43     @Serial
44     private static final long serialVersionUID = -8410208962541783805L;
45
46     private static final String TIMESTAMP_TOKEN = "timeStamp";
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     @Column(name = TIMESTAMP_TOKEN, precision = 3)
57     @Temporal(TemporalType.TIMESTAMP)
58     @NonNull
59     private Date timeStamp;
60
61
62     /**
63      * The default constructor creates a null concept key.
64      */
65     public PfTimestampKey() {
66         this(NULL_KEY_NAME, NULL_KEY_VERSION, Instant.EPOCH);
67     }
68
69     /**
70      * Copy constructor.
71      *
72      * @param copyConcept the concept to copy from
73      */
74     public PfTimestampKey(@NonNull final PfTimestampKey copyConcept) {
75         super(copyConcept);
76         this.timeStamp = copyConcept.getTimeStamp();
77     }
78
79     /**
80      * Constructor to create a key with the specified name and version.
81      *
82      * @param name    the key name
83      * @param version the key version
84      * @param instant the time stamp of key
85      */
86     public PfTimestampKey(@NonNull final String name, @NonNull final String version,
87                           @NonNull final Instant instant) {
88         super(name, version);
89         this.timeStamp = Date.from(instant);
90     }
91
92     /**
93      * Constructor to create a key using the key and version from the specified key ID.
94      *
95      * @param id the key ID in a format that respects the KEY_ID_REGEXP
96      */
97     public PfTimestampKey(final String id) {
98         super(id.substring(0, id.lastIndexOf(':')));
99         this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
100     }
101
102     @Override
103     public String getId() {
104         return getName() + ':' + getVersion() + ':' + getTimeStamp().getTime();
105     }
106
107     /**
108      * Get a null key.
109      *
110      * @return a null key
111      */
112     public static PfTimestampKey getNullKey() {
113         return new PfTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, Instant.EPOCH);
114     }
115
116     public Instant getInstant() {
117         return timeStamp.toInstant();
118     }
119
120     public void setInstant(final Instant instant) {
121         setTimeStamp(Date.from(instant));
122     }
123
124     @Override
125     public boolean isNewerThan(@NonNull PfKey otherKey) {
126         Assertions.instanceOf(otherKey, PfTimestampKey.class);
127
128         final PfTimestampKey otherConceptKey = (PfTimestampKey) otherKey;
129
130         if (this.equals(otherConceptKey)) {
131             return false;
132         }
133
134         if (!timeStamp.equals(otherConceptKey.timeStamp)) {
135             return timeStamp.after(otherConceptKey.timeStamp);
136         }
137
138         return super.isNewerThan(otherKey);
139     }
140
141     @Override
142     public boolean isNullKey() {
143         return super.isNullKey() && getTimeStamp().getTime() == 0;
144     }
145
146     @Override
147     public int compareTo(@NonNull final PfConcept otherObj) {
148         int result = super.compareTo(otherObj);
149         if (0 == result) {
150             final PfTimestampKey other = (PfTimestampKey) otherObj;
151             return timeStamp.compareTo(other.timeStamp);
152         }
153         return result;
154     }
155
156     @Override
157     public void setName(@NonNull String name) {
158         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
159     }
160
161     @Override
162     public void setVersion(@NonNull String version) {
163         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
164     }
165
166 }