replace hashCode function with compareTo
[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 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 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
28 import java.util.Date;
29 import java.util.List;
30
31 import javax.persistence.EmbeddedId;
32 import javax.persistence.Entity;
33 import javax.persistence.Inheritance;
34 import javax.persistence.InheritanceType;
35 import javax.persistence.Table;
36
37 import lombok.Data;
38 import lombok.EqualsAndHashCode;
39 import lombok.NonNull;
40
41 import org.onap.policy.common.utils.validation.Assertions;
42 import org.onap.policy.models.base.PfConcept;
43 import org.onap.policy.models.base.PfKey;
44 import org.onap.policy.models.base.PfReferenceKey;
45 import org.onap.policy.models.base.PfUtils;
46 import org.onap.policy.models.base.PfValidationMessage;
47 import org.onap.policy.models.base.PfValidationResult;
48 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
49
50 /**
51  * Class to represent the TimeInterval in TOSCA definition.
52  *
53  * @author Chenfei Gao (cgao@research.att.com)
54  * @author Liam Fallon (liam.fallon@est.tech)
55  *
56  */
57 @Entity
58 @Table(name = "ToscaTimeInterval")
59 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
60 @Data
61 @EqualsAndHashCode(callSuper = false)
62 public class JpaToscaTimeInterval extends PfConcept {
63     private static final long serialVersionUID = 9151467029611969980L;
64
65     @EmbeddedId
66     private PfReferenceKey key;
67
68     @SerializedName("start_time")
69     private Date startTime;
70
71     @SerializedName("end_time")
72     private Date 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, new Date(0), new Date(0));
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 Date startTime,
96             @NonNull final Date endTime) {
97         this.key = key;
98         this.startTime = startTime;
99         this.endTime = 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     }
110
111     @Override
112     public List<PfKey> getKeys() {
113         return getKey().getKeys();
114     }
115
116     @Override
117     public void clean() {
118         key.clean();
119     }
120
121     @Override
122     public PfValidationResult validate(@NonNull final PfValidationResult resultIn) {
123         PfValidationResult result = resultIn;
124
125         if (key.isNullKey()) {
126             result.addValidationMessage(
127                     new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key is a null key"));
128         }
129
130         result = key.validate(result);
131
132         if (startTime == null || startTime.getTime() == 0) {
133             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
134                     "start time on time interval may not be null or zero"));
135         }
136
137         if (endTime == null || endTime.getTime() == 0) {
138             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
139                     "end time on time interval may not be null or zero"));
140         }
141
142         if (startTime != null && endTime != null && endTime.before(startTime)) {
143             result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID,
144                     "end time \"" + endTime.toString() + "\" on time interval may not be before start time \""
145                             + startTime.toString() + "\""));
146         }
147
148         return result;
149     }
150
151     @Override
152     public int compareTo(final PfConcept otherConcept) {
153         if (otherConcept == null) {
154             return -1;
155         }
156         if (this == otherConcept) {
157             return 0;
158         }
159         if (getClass() != otherConcept.getClass()) {
160             return getClass().getName().compareTo(otherConcept.getClass().getName());
161         }
162
163         final JpaToscaTimeInterval other = (JpaToscaTimeInterval) otherConcept;
164         if (!key.equals(other.key)) {
165             return key.compareTo(other.key);
166         }
167
168         int returnVal = PfUtils.compareObjects(startTime, other.startTime);
169         if (returnVal != 0) {
170             return returnVal;
171         }
172
173         return PfUtils.compareObjects(endTime, other.endTime);
174     }
175
176     @Override
177     public PfConcept copyTo(@NonNull final PfConcept target) {
178         final Object copyObject = target;
179         Assertions.instanceOf(copyObject, JpaToscaTimeInterval.class);
180
181         final JpaToscaTimeInterval copy = ((JpaToscaTimeInterval) copyObject);
182         copy.setKey(new PfReferenceKey(key));
183         copy.setStartTime(startTime);
184         copy.setEndTime(endTime);
185
186         return copy;
187     }
188 }