2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.models.base;
25 import java.util.Date;
26 import javax.persistence.Column;
27 import javax.persistence.Embeddable;
28 import lombok.EqualsAndHashCode;
30 import lombok.NonNull;
31 import org.onap.policy.common.utils.validation.Assertions;
35 @EqualsAndHashCode(callSuper = false)
36 public class PfTimestampKey extends PfKeyImpl {
37 private static final long serialVersionUID = -8410208962541783805L;
39 private static final String TIMESTAMP_TOKEN = "timeStamp";
41 @Column(name = NAME_TOKEN, length = 120)
44 @Column(name = VERSION_TOKEN, length = 20)
45 private String version;
47 @Column(name = TIMESTAMP_TOKEN)
48 private Date timeStamp;
52 * The default constructor creates a null concept key.
54 public PfTimestampKey() {
55 this(NULL_KEY_NAME, NULL_KEY_VERSION, new Date(0));
61 * @param copyConcept the concept to copy from
63 public PfTimestampKey(@NonNull final PfTimestampKey copyConcept) {
65 this.timeStamp = new Date(copyConcept.getTimeStamp().getTime());
69 * Constructor to create a key with the specified name and version.
71 * @param name the key name
72 * @param version the key version
73 * @param timeStamp the timestamp of key
75 public PfTimestampKey(@NonNull final String name, @NonNull final String version,
76 @NonNull final Date timeStamp) {
78 this.timeStamp = new Date(timeStamp.getTime());
82 * Constructor to create a key using the key and version from the specified key ID.
84 * @param id the key ID in a format that respects the KEY_ID_REGEXP
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)));
92 public String getId() {
93 return getName() + ':' + getVersion() + ':' + getTimeStamp().getTime();
101 public static final PfTimestampKey getNullKey() {
102 return new PfTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, new Date(0));
106 public String toString() {
107 return "PfTimestampKey(name=" + getName() + ", version=" + getVersion() + ", timestamp="
108 + getTimeStamp().getTime() + ")";
112 public boolean isNewerThan(@NonNull PfKey otherKey) {
113 Assertions.instanceOf(otherKey, PfTimestampKey.class);
115 final PfTimestampKey otherConceptKey = (PfTimestampKey) otherKey;
117 if (this.equals(otherConceptKey)) {
121 if (!timeStamp.equals(otherConceptKey.timeStamp)) {
122 return timeStamp.after(otherConceptKey.timeStamp);
125 return super.isNewerThan(otherKey);
128 public void setTimeStamp(@NonNull final Date timeStamp) {
129 this.timeStamp = new Date(timeStamp.getTime());
133 public boolean isNullKey() {
134 return super.isNullKey() && getTimeStamp().getTime() == 0;
138 public int compareTo(@NonNull final PfConcept otherObj) {
139 int result = super.compareTo(otherObj);
141 final PfTimestampKey other = (PfTimestampKey) otherObj;
142 return timeStamp.compareTo(other.timeStamp);
148 public void setName(@NonNull String name) {
149 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
153 public void setVersion(@NonNull String version) {
154 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());