Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaTimeInterval.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Model
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
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.tosca.simple.concepts;
25
26 import com.google.gson.annotations.SerializedName;
27 import jakarta.persistence.EmbeddedId;
28 import jakarta.persistence.Entity;
29 import jakarta.persistence.Inheritance;
30 import jakarta.persistence.InheritanceType;
31 import jakarta.persistence.Table;
32 import java.io.Serial;
33 import java.sql.Timestamp;
34 import java.time.Instant;
35 import java.util.List;
36 import lombok.Data;
37 import lombok.EqualsAndHashCode;
38 import lombok.NonNull;
39 import org.onap.policy.common.parameters.BeanValidationResult;
40 import org.onap.policy.common.parameters.annotations.NotNull;
41 import org.onap.policy.models.base.PfConcept;
42 import org.onap.policy.models.base.PfKey;
43 import org.onap.policy.models.base.PfReferenceKey;
44 import org.onap.policy.models.base.PfUtils;
45 import org.onap.policy.models.base.validation.annotations.VerifyKey;
46
47 /**
48  * Class to represent the TimeInterval in TOSCA definition.
49  *
50  * @author Chenfei Gao (cgao@research.att.com)
51  * @author Liam Fallon (liam.fallon@est.tech)
52  *
53  */
54 @Entity
55 @Table(name = "ToscaTimeInterval")
56 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
57 @Data
58 @EqualsAndHashCode(callSuper = false)
59 public class JpaToscaTimeInterval extends PfConcept {
60     @Serial
61     private static final long serialVersionUID = 9151467029611969980L;
62
63     @EmbeddedId
64     @VerifyKey
65     @NotNull
66     private PfReferenceKey key;
67
68     @SerializedName("start_time")
69     private Timestamp startTime;
70
71     @SerializedName("end_time")
72     private Timestamp endTime;
73
74     /**
75      * The Default Constructor creates a {@link JpaToscaTimeInterval} object with a null key.
76      */
77     public JpaToscaTimeInterval() {
78         this(new PfReferenceKey());
79     }
80
81     /**
82      * The Key Constructor creates a {@link JpaToscaTimeInterval} object with the given concept key.
83      *
84      * @param key the key
85      */
86     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key) {
87         this(key, Instant.EPOCH, Instant.EPOCH);
88     }
89
90     /**
91      * The full constructor creates a {@link JpaToscaTimeInterval} object with all fields.
92      *
93      * @param key the key
94      */
95     public JpaToscaTimeInterval(@NonNull final PfReferenceKey key, @NonNull final Instant startTime,
96             @NonNull final Instant endTime) {
97         this.key = key;
98         this.startTime = Timestamp.from(startTime);
99         this.endTime = Timestamp.from(endTime);
100     }
101
102     /**
103      * Copy constructor.
104      *
105      * @param copyConcept the concept to copy from
106      */
107     public JpaToscaTimeInterval(final JpaToscaTimeInterval copyConcept) {
108         super(copyConcept);
109         this.key = new PfReferenceKey(copyConcept.key);
110         this.startTime = copyConcept.startTime;
111         this.endTime = copyConcept.endTime;
112     }
113
114     @Override
115     public List<PfKey> getKeys() {
116         return getKey().getKeys();
117     }
118
119     @Override
120     public void clean() {
121         key.clean();
122     }
123
124     @Override
125     public BeanValidationResult validate(@NonNull String fieldName) {
126         BeanValidationResult result = super.validate(fieldName);
127
128         if (startTime == null || startTime.getTime() == 0) {
129             addResult(result, "startTime", startTime, "is null or zero");
130         }
131
132         if (endTime == null || endTime.getTime() == 0) {
133             addResult(result, "endTime", endTime, "is null or zero");
134         }
135
136         if (startTime != null && endTime != null && endTime.before(startTime)) {
137             addResult(result, "endTime", endTime, "is before startTime");
138         }
139
140         return result;
141     }
142
143     @Override
144     public int compareTo(final PfConcept otherConcept) {
145         if (otherConcept == null) {
146             return -1;
147         }
148         if (this == otherConcept) {
149             return 0;
150         }
151         if (getClass() != otherConcept.getClass()) {
152             return getClass().getName().compareTo(otherConcept.getClass().getName());
153         }
154
155         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
156         int result = key.compareTo(other.key);
157         if (result != 0) {
158             return result;
159         }
160
161         result = PfUtils.compareObjects(startTime, other.startTime);
162         if (result != 0) {
163             return result;
164         }
165
166         return PfUtils.compareObjects(endTime, other.endTime);
167     }
168 }