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