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