Refactor timestamp property in policy models to use Instant
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfReferenceTimestampKey.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import java.sql.Timestamp;
24 import java.time.Instant;
25 import javax.persistence.Column;
26 import javax.persistence.Embeddable;
27 import lombok.Data;
28 import lombok.EqualsAndHashCode;
29 import lombok.NonNull;
30 import lombok.ToString;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32 import org.onap.policy.common.utils.validation.Assertions;
33
34 /**
35  * This class is an extension of PfReferenceKey. It has similar behaviour as of PfReferenceKey with an
36  * additional option to have timestamp as a parameter.
37  *
38  */
39 @Embeddable
40 @Data
41 @EqualsAndHashCode(callSuper = true)
42 @ToString(callSuper = true)
43 public class PfReferenceTimestampKey extends PfReferenceKey {
44     private static final long serialVersionUID = 1130918285832617215L;
45
46     private static final String TIMESTAMP_TOKEN = "timeStamp";
47
48     @Column(name = TIMESTAMP_TOKEN)
49     @NotNull
50     private Timestamp timeStamp;
51
52
53     /**
54      * The default constructor creates a null reference timestamp key.
55      */
56     public PfReferenceTimestampKey() {
57         super();
58         this.timeStamp = new Timestamp(0);
59     }
60
61     /**
62      * The Copy Constructor creates a key by copying another key.
63      *
64      * @param referenceKey
65      *        the reference key to copy from
66      */
67     public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceKey) {
68         super(referenceKey);
69         this.timeStamp = referenceKey.getTimeStamp();
70     }
71
72     /**
73      * Constructor to create a null reference key for the specified parent concept key.
74      *
75      * @param pfConceptKey
76      *        the parent concept key of this reference key
77      */
78     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) {
79         super(pfConceptKey);
80         this.timeStamp = new Timestamp(0);
81     }
82
83     /**
84      * Constructor to create a reference timestamp key for the given parent concept key with the given local name.
85      *
86      * @param pfConceptKey
87      *        the parent concept key of this reference key
88      * @param localName
89      *        the local name of this reference key
90      * @param instant
91      *        the time stamp for this reference key
92      */
93     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) {
94         super(pfConceptKey, localName);
95         this.timeStamp = Timestamp.from(instant);
96     }
97
98     /**
99      * Constructor to create a reference timestamp key for the given parent reference key with the given local name.
100      *
101      * @param parentReferenceKey
102      *        the parent reference key of this reference key
103      * @param localName
104      *        the local name of this reference key
105      * @param instant
106      *        the time stamp for this reference key
107      */
108     public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName,
109                                    final Instant instant) {
110         super(parentReferenceKey, localName);
111         this.timeStamp = Timestamp.from(instant);
112     }
113
114     /**
115      * Constructor to create a reference timestamp key for the given parent reference key (specified by the parent
116      * reference key's concept key and local name) with the given local name.
117      *
118      * @param pfConceptKey
119      *        the concept key of the parent reference key of this reference key
120      * @param parentLocalName
121      *        the local name of the parent reference key of this reference key
122      * @param localName
123      *        the local name of this reference key
124      * @param instant
125      *        the time stamp for this reference key
126      */
127     public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName,
128                                    final String localName, final Instant instant) {
129         super(pfConceptKey, parentLocalName, localName);
130         this.timeStamp = Timestamp.from(instant);
131     }
132
133     /**
134      * Constructor to create a reference timestamp key for the given parent concept key (specified by the
135      * parent concept key's name and version) with the given local name.
136      *
137      * @param parentKeyName
138      *        the name of the parent concept key of this reference key
139      * @param parentKeyVersion
140      *        the version of the parent concept key of this reference key
141      * @param localName
142      *        the local name of this reference key
143      * @param instant
144      *        the time stamp for this reference key
145      */
146     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName,
147                                    final Instant instant) {
148         super(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName);
149         this.timeStamp = Timestamp.from(instant);
150     }
151
152     /**
153      * Constructor to create a reference timestamp key for the given parent key (specified by the parent key's name,
154      * version and local name) with the given local name.
155      *
156      * @param parentKeyName
157      *        the parent key name of this reference key
158      * @param parentKeyVersion
159      *        the parent key version of this reference key
160      * @param parentLocalName
161      *        the parent local name of this reference key
162      * @param localName
163      *        the local name of this reference key
164      * @param instant
165      *        the instant for this reference key
166      */
167     public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion,
168                                    final String parentLocalName, final String localName, final Instant instant) {
169         super(parentKeyName, parentKeyVersion, parentLocalName, localName);
170         this.timeStamp = Timestamp.from(instant);
171     }
172
173
174     /**
175      * Constructor to create a key using the key and version from the specified key ID.
176      *
177      * @param id the key ID in a format that respects the KEY_ID_REGEXP
178      */
179     public PfReferenceTimestampKey(final String id) {
180         super(id.substring(0, id.lastIndexOf(':')));
181         this.timeStamp = new Timestamp(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
182     }
183
184
185     /**
186      * Get a null reference timestamp key.
187      *
188      * @return a null reference key
189      */
190     public static PfReferenceTimestampKey getNullKey() {
191         return new PfReferenceTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, PfKey.NULL_KEY_NAME,
192             PfKey.NULL_KEY_NAME, Instant.EPOCH);
193     }
194
195     public Instant getInstant() {
196         return timeStamp.toInstant();
197     }
198
199     public void setInstant(final Instant instant) {
200         setTimeStamp(Timestamp.from(instant));
201     }
202
203     @Override
204     public PfReferenceTimestampKey getKey() {
205         return this;
206     }
207
208     @Override
209     public String getId() {
210         return super.getId() + ':' + getTimeStamp().getTime();
211     }
212
213     @Override
214     public boolean isNullKey() {
215         return super.isNullKey() && getTimeStamp().getTime() == 0;
216     }
217
218     @Override
219     public boolean isNewerThan(@NonNull PfKey otherKey) {
220         Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class);
221         final PfReferenceTimestampKey otherConceptKey = (PfReferenceTimestampKey) otherKey;
222
223         if (this.equals(otherConceptKey)) {
224             return false;
225         }
226         if (!getTimeStamp().equals(otherConceptKey.timeStamp)) {
227             return timeStamp.after(otherConceptKey.timeStamp);
228         }
229         return super.isNewerThan(otherKey);
230     }
231 }