ba857223efa200671b5c68bb5dcbed22e7a0546d
[policy/apex-pdp.git] / plugins / plugins-persistence / plugins-persistence-jpa / plugins-persistence-jpa-eclipselink / src / test / java / org / onap / policy / apex / plugins / persistence / jpa / eclipselink / entities / ReferenceKeyTestEntity.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.persistence.jpa.eclipselink.entities;
22
23 import java.util.Arrays;
24 import java.util.List;
25
26 import javax.persistence.EmbeddedId;
27 import javax.persistence.Entity;
28 import javax.persistence.Table;
29 import javax.xml.bind.annotation.XmlElement;
30 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
31
32 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
36 import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter;
37
38 /**
39  * The Class ReferenceKeyTestEntity provides a reference key test concept.
40  */
41 @Entity
42 @Table(name = "ReferenceKeyTestEntity")
43 public class ReferenceKeyTestEntity extends AxConcept {
44     private static final long serialVersionUID = -2962570563281067895L;
45
46     @EmbeddedId()
47     @XmlElement(name = "key", required = true)
48     @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
49     protected AxReferenceKey key;
50
51     private double doubleValue;
52
53     /**
54      * Instantiates a new reference key test entity.
55      */
56     public ReferenceKeyTestEntity() {
57         this.key = new AxReferenceKey();
58         this.doubleValue = 0;
59     }
60
61     /**
62      * Instantiates a new reference key test entity.
63      *
64      * @param doubleValue the double value
65      */
66     public ReferenceKeyTestEntity(final Double doubleValue) {
67         this.key = new AxReferenceKey();
68         this.doubleValue = doubleValue;
69     }
70
71     /**
72      * Instantiates a new reference key test entity.
73      *
74      * @param key the key
75      * @param doubleValue the double value
76      */
77     public ReferenceKeyTestEntity(final AxReferenceKey key, final Double doubleValue) {
78         this.key = key;
79         this.doubleValue = doubleValue;
80     }
81
82     /**
83      * {@inheritDoc}.
84      */
85     @Override
86     public AxReferenceKey getKey() {
87         return key;
88     }
89
90     /**
91      * {@inheritDoc}.
92      */
93     @Override
94     public List<AxKey> getKeys() {
95         return Arrays.asList((AxKey) getKey());
96     }
97
98     /**
99      * Sets the key.
100      *
101      * @param key the new key
102      */
103     public void setKey(final AxReferenceKey key) {
104         this.key = key;
105     }
106
107     /**
108      * Check set key.
109      *
110      * @return true, if successful
111      */
112     public boolean checkSetKey() {
113         return (this.key != null);
114     }
115
116     /**
117      * Gets the double value.
118      *
119      * @return the double value
120      */
121     public double getDoubleValue() {
122         return doubleValue;
123     }
124
125     /**
126      * Sets the double value.
127      *
128      * @param doubleValue the new double value
129      */
130     public void setDoubleValue(final double doubleValue) {
131         this.doubleValue = doubleValue;
132     }
133
134     /**
135      * {@inheritDoc}.
136      */
137     @Override
138     public AxValidationResult validate(final AxValidationResult result) {
139         return key.validate(result);
140     }
141
142     /**
143      * {@inheritDoc}.
144      */
145     @Override
146     public void clean() {
147         key.clean();
148     }
149
150     /**
151      * {@inheritDoc}.
152      */
153     @Override
154     public String toString() {
155         return "ReferenceKeyTestEntity [key=" + key + ", doubleValue=" + doubleValue + "]";
156     }
157
158     /**
159      * {@inheritDoc}.
160      */
161     @Override
162     public AxConcept copyTo(final AxConcept target) {
163         final Object copyObject = ((target == null) ? new ReferenceKeyTestEntity() : target);
164         if (copyObject instanceof ReferenceKeyTestEntity) {
165             final ReferenceKeyTestEntity copy = ((ReferenceKeyTestEntity) copyObject);
166             if (this.checkSetKey()) {
167                 copy.setKey(new AxReferenceKey(key));
168             } else {
169                 copy.key = null;
170             }
171             copy.doubleValue = doubleValue;
172             return copy;
173         } else {
174             return null;
175         }
176     }
177
178     /**
179      * {@inheritDoc}.
180      */
181     @Override
182     public int hashCode() {
183         final int prime = 31;
184         int result = 1;
185         result = prime * result + ((key == null) ? 0 : key.hashCode());
186         return result;
187     }
188
189     /**
190      * {@inheritDoc}.
191      */
192     @Override
193     public boolean equals(final Object obj) {
194         if (obj == null) {
195             return false;
196         }
197         if (this == obj) {
198             return true;
199         }
200         if (getClass() != obj.getClass()) {
201             return false;
202         }
203         final ReferenceKeyTestEntity other = (ReferenceKeyTestEntity) obj;
204         if (key == null) {
205             if (other.key != null) {
206                 return false;
207             }
208         } else if (!key.equals(other.key)) {
209             return false;
210         }
211         return (Double.compare(doubleValue, other.doubleValue) == 0);
212     }
213
214     /**
215      * {@inheritDoc}.
216      */
217     @Override
218     public int compareTo(final AxConcept otherObj) {
219         if (otherObj == null) {
220             return -1;
221         }
222         if (this == otherObj) {
223             return 0;
224         }
225         if (getClass() != otherObj.getClass()) {
226             return -1;
227         }
228         final ReferenceKeyTestEntity other = (ReferenceKeyTestEntity) otherObj;
229         if (key == null) {
230             if (other.key != null) {
231                 return 1;
232             }
233         } else if (!key.equals(other.key)) {
234             return key.compareTo(other.key);
235         }
236         return Double.compare(doubleValue, other.doubleValue);
237     }
238 }