Add JpaPdpStatistics entity
[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  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.base;
24
25 import java.util.Date;
26 import javax.persistence.Column;
27 import javax.persistence.Embeddable;
28 import lombok.EqualsAndHashCode;
29 import lombok.Getter;
30 import lombok.NonNull;
31 import org.onap.policy.common.utils.validation.Assertions;
32
33 @Embeddable
34 @Getter
35 @EqualsAndHashCode(callSuper = false)
36 public class PfTimestampKey extends PfKeyImpl {
37     private static final long serialVersionUID = -8410208962541783805L;
38
39     private static final String TIMESTAMP_TOKEN = "timeStamp";
40
41     @Column(name = NAME_TOKEN, length = 120)
42     private String name;
43
44     @Column(name = VERSION_TOKEN, length = 20)
45     private String version;
46
47     @Column(name = TIMESTAMP_TOKEN)
48     private Date timeStamp;
49
50
51     /**
52      * The default constructor creates a null concept key.
53      */
54     public PfTimestampKey() {
55         this(NULL_KEY_NAME, NULL_KEY_VERSION, new Date(0));
56     }
57
58     /**
59      * Copy constructor.
60      *
61      * @param copyConcept the concept to copy from
62      */
63     public PfTimestampKey(@NonNull final PfTimestampKey copyConcept) {
64         super(copyConcept);
65         this.timeStamp = new Date(copyConcept.getTimeStamp().getTime());
66     }
67
68     /**
69      * Constructor to create a key with the specified name and version.
70      *
71      * @param name the key name
72      * @param version the key version
73      * @param timeStamp the timestamp of key
74      */
75     public PfTimestampKey(@NonNull final String name, @NonNull final String version,
76             @NonNull final Date timeStamp) {
77         super(name, version);
78         this.timeStamp = new Date(timeStamp.getTime());
79     }
80
81     /**
82      * Constructor to create a key using the key and version from the specified key ID.
83      *
84      * @param id the key ID in a format that respects the KEY_ID_REGEXP
85      */
86     public PfTimestampKey(final String id) {
87         super(id.substring(0, id.lastIndexOf(':')));
88         this.timeStamp = new Date(Long.parseLong(id.substring(id.lastIndexOf(':') + 1)));
89     }
90
91     @Override
92     public String getId() {
93         return getName() + ':' + getVersion() + ':' + getTimeStamp().getTime();
94     }
95
96     /**
97      * Get a null key.
98      *
99      * @return a null key
100      */
101     public static final PfTimestampKey getNullKey() {
102         return new PfTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, new Date(0));
103     }
104
105     @Override
106     public String toString() {
107         return "PfTimestampKey(name=" + getName() + ", version=" + getVersion() + ", timestamp="
108                 + getTimeStamp().getTime() + ")";
109     }
110
111     @Override
112     public boolean isNewerThan(@NonNull PfKey otherKey) {
113         Assertions.instanceOf(otherKey, PfTimestampKey.class);
114
115         final PfTimestampKey otherConceptKey = (PfTimestampKey) otherKey;
116
117         if (this.equals(otherConceptKey)) {
118             return false;
119         }
120
121         if (!timeStamp.equals(otherConceptKey.timeStamp)) {
122             return timeStamp.after(otherConceptKey.timeStamp);
123         }
124
125         return super.isNewerThan(otherKey);
126     }
127
128     public void setTimeStamp(@NonNull final Date timeStamp) {
129         this.timeStamp = new Date(timeStamp.getTime());
130     }
131
132     @Override
133     public boolean isNullKey() {
134         return super.isNullKey() && getTimeStamp().getTime() == 0;
135     }
136
137     @Override
138     public int compareTo(@NonNull final PfConcept otherObj) {
139         int result = super.compareTo(otherObj);
140         if (0 == result) {
141             final PfTimestampKey other = (PfTimestampKey) otherObj;
142             return timeStamp.compareTo(other.timeStamp);
143         }
144         return result;
145     }
146
147     @Override
148     public void setName(@NonNull String name) {
149         this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
150     }
151
152     @Override
153     public void setVersion(@NonNull String version) {
154         this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());
155     }
156 }