1f5dc654aa67be683478a18c0572de235860157e
[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 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 java.time.Instant;
27 import java.util.Date;
28 import javax.persistence.Column;
29 import javax.persistence.Embeddable;
30 import javax.persistence.Temporal;
31 import javax.persistence.TemporalType;
32 import lombok.Data;
33 import lombok.EqualsAndHashCode;
34 import lombok.NonNull;
35 import org.onap.policy.common.parameters.annotations.Pattern;
36 import org.onap.policy.common.utils.validation.Assertions;
37
38 @Embeddable
39 @Data
40 @EqualsAndHashCode(callSuper = false)
41 public class PfTimestampKey extends PfKeyImpl {
42     private static final long serialVersionUID = -8410208962541783805L;
43
44     private static final String TIMESTAMP_TOKEN = "timeStamp";
45
46     @Column(name = NAME_TOKEN, length = 120)
47     @Pattern(regexp = NAME_REGEXP)
48     private String name;
49
50     @Column(name = VERSION_TOKEN, length = 20)
51     @Pattern(regexp = VERSION_REGEXP)
52     private String version;
53
54     @Column(name = TIMESTAMP_TOKEN, precision = 3)
55     @Temporal(TemporalType.TIMESTAMP)
56     @NonNull
57     private Date timeStamp;
58
59
60     /**
61      * The default constructor creates a null concept key.
62      */
63     public PfTimestampKey() {
64         this(NULL_KEY_NAME, NULL_KEY_VERSION, Instant.EPOCH);
65     }
66
67     /**
68      * Copy constructor.
69      *
70      * @param copyConcept the concept to copy from
71      */
72     public PfTimestampKey(@NonNull final PfTimestampKey copyConcept) {
73         super(copyConcept);
74         this.timeStamp = copyConcept.getTimeStamp();
75     }
76
77     /**
78      * Constructor to create a key with the specified name and version.
79      *
80      * @param name the key name
81      * @param version the key version
82      * @param instant the time stamp of key
83      */
84     public PfTimestampKey(@NonNull final String name, @NonNull final String version,
85             @NonNull final Instant instant) {
86         super(name, version);
87         this.timeStamp = Date.from(instant);
88     }
89
90     /**
91      * Constructor to create a key using the key and version from the specified key ID.
92      *
93      * @param id the key ID in a format that respects the KEY_ID_REGEXP
94      */
95     public PfTimestampKey(final String id) {
96         super(id.substring(0, id.lastIndexOf(':')));
97         this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
98     }
99
100     @Override
101     public String getId() {
102         return getName() + ':' + getVersion() + ':' + getTimeStamp().getTime();
103     }
104
105     /**
106      * Get a null key.
107      *
108      * @return a null key
109      */
110     public static final PfTimestampKey getNullKey() {
111         return new PfTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, Instant.EPOCH);
112     }
113
114     public Instant getInstant() {
115         return timeStamp.toInstant();
116     }
117
118     public void setInstant(final Instant instant) {
119         setTimeStamp(Date.from(instant));
120     }
121
122     @Override
123     public boolean isNewerThan(@NonNull PfKey otherKey) {
124         Assertions.instanceOf(otherKey, PfTimestampKey.class);
125
126         final PfTimestampKey otherConceptKey = (PfTimestampKey) otherKey;
127
128         if (this.equals(otherConceptKey)) {
129             return false;
130         }
131
132         if (!timeStamp.equals(otherConceptKey.timeStamp)) {
133             return timeStamp.after(otherConceptKey.timeStamp);
134         }
135
136         return super.isNewerThan(otherKey);
137     }
138
139     @Override
140     public boolean isNullKey() {
141         return super.isNullKey() && getTimeStamp().getTime() == 0;
142     }
143
144     @Override
145     public int compareTo(@NonNull final PfConcept otherObj) {
146         int result = super.compareTo(otherObj);
147         if (0 == result) {
148             final PfTimestampKey other = (PfTimestampKey) otherObj;
149             return timeStamp.compareTo(other.timeStamp);
150         }
151         return result;
152     }
153
154     @Override
155     public void setName(@NonNull String name) {
156         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
157     }
158
159     @Override
160     public void setVersion(@NonNull String version) {
161         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
162     }
163
164 }