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