2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.models.base;
26 import java.util.Date;
27 import javax.persistence.Column;
28 import javax.persistence.Embeddable;
29 import lombok.EqualsAndHashCode;
31 import lombok.NonNull;
32 import org.onap.policy.common.parameters.annotations.Pattern;
33 import org.onap.policy.common.utils.validation.Assertions;
37 @EqualsAndHashCode(callSuper = false)
38 public class PfTimestampKey extends PfKeyImpl {
39 private static final long serialVersionUID = -8410208962541783805L;
41 private static final String TIMESTAMP_TOKEN = "timeStamp";
43 @Column(name = NAME_TOKEN, length = 120)
44 @Pattern(regexp = NAME_REGEXP)
47 @Column(name = VERSION_TOKEN, length = 20)
48 @Pattern(regexp = VERSION_REGEXP)
49 private String version;
51 @Column(name = TIMESTAMP_TOKEN)
52 private Date timeStamp;
56 * The default constructor creates a null concept key.
58 public PfTimestampKey() {
59 this(NULL_KEY_NAME, NULL_KEY_VERSION, new Date(0));
65 * @param copyConcept the concept to copy from
67 public PfTimestampKey(@NonNull final PfTimestampKey copyConcept) {
69 long millis = copyConcept.getTimeStamp().getTime();
70 this.timeStamp = new Date(millis);
74 * Constructor to create a key with the specified name and version.
76 * @param name the key name
77 * @param version the key version
78 * @param timeStamp the timestamp of key
80 public PfTimestampKey(@NonNull final String name, @NonNull final String version,
81 @NonNull final Date timeStamp) {
83 this.timeStamp = new Date(timeStamp.getTime());
87 * Constructor to create a key using the key and version from the specified key ID.
89 * @param id the key ID in a format that respects the KEY_ID_REGEXP
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)));
97 public String getId() {
98 return getName() + ':' + getVersion() + ':' + getTimeStamp().getTime();
106 public static final PfTimestampKey getNullKey() {
107 return new PfTimestampKey(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, new Date(0));
111 public String toString() {
112 return "PfTimestampKey(name=" + getName() + ", version=" + getVersion() + ", timestamp="
113 + getTimeStamp().getTime() + ")";
117 public boolean isNewerThan(@NonNull PfKey otherKey) {
118 Assertions.instanceOf(otherKey, PfTimestampKey.class);
120 final PfTimestampKey otherConceptKey = (PfTimestampKey) otherKey;
122 if (this.equals(otherConceptKey)) {
126 if (!timeStamp.equals(otherConceptKey.timeStamp)) {
127 return timeStamp.after(otherConceptKey.timeStamp);
130 return super.isNewerThan(otherKey);
133 public void setTimeStamp(@NonNull final Date timeStamp) {
134 this.timeStamp = new Date(timeStamp.getTime());
138 public boolean isNullKey() {
139 return super.isNullKey() && getTimeStamp().getTime() == 0;
143 public int compareTo(@NonNull final PfConcept otherObj) {
144 int result = super.compareTo(otherObj);
146 final PfTimestampKey other = (PfTimestampKey) otherObj;
147 return timeStamp.compareTo(other.timeStamp);
153 public void setName(@NonNull String name) {
154 this.name = Assertions.validateStringParameter(NAME_TOKEN, name, getNameRegEx());
158 public void setVersion(@NonNull String version) {
159 this.version = Assertions.validateStringParameter(VERSION_TOKEN, version, getVersionRegEx());