991b4e2d7ea5ccd257e26d45960609c4d19aa253
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
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.hibernate;
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 @Entity
39 @Table(name = "ReferenceKeyTestEntity")
40 public class ReferenceKeyTestEntity extends AxConcept {
41     private static final long serialVersionUID = -2962570563281067894L;
42
43     @EmbeddedId()
44     @XmlElement(name = "key", required = true)
45     @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
46     protected AxReferenceKey key;
47
48     private double doubleValue;
49
50     public ReferenceKeyTestEntity() {
51         this.key = new AxReferenceKey();
52         this.doubleValue = 0;
53     }
54
55     public ReferenceKeyTestEntity(final Double doubleValue) {
56         this.key = new AxReferenceKey();
57         this.doubleValue = doubleValue;
58     }
59
60     public ReferenceKeyTestEntity(final AxReferenceKey key, final Double doubleValue) {
61         this.key = key;
62         this.doubleValue = doubleValue;
63     }
64
65     public AxReferenceKey getKey() {
66         return key;
67     }
68
69     public List<AxKey> getKeys() {
70         return Arrays.asList((AxKey) getKey());
71     }
72
73     public void setKey(final AxReferenceKey key) {
74         this.key = key;
75     }
76
77     public boolean checkSetKey() {
78         return (this.key != null);
79     }
80
81     public double getDoubleValue() {
82         return doubleValue;
83     }
84
85     public void setDoubleValue(final double doubleValue) {
86         this.doubleValue = doubleValue;
87     }
88
89     @Override
90     public AxValidationResult validate(final AxValidationResult result) {
91         return key.validate(result);
92     }
93
94     @Override
95     public void clean() {
96         key.clean();
97     }
98
99     @Override
100     public String toString() {
101         return "ReferenceKeyTestEntity [key=" + key + ", doubleValue=" + doubleValue + "]";
102     }
103
104     @Override
105     public AxConcept copyTo(final AxConcept target) {
106         final Object copyObject = ((target == null) ? new ReferenceKeyTestEntity() : target);
107         if (copyObject instanceof ReferenceKeyTestEntity) {
108             final ReferenceKeyTestEntity copy = ((ReferenceKeyTestEntity) copyObject);
109             if (this.checkSetKey()) {
110                 copy.setKey(new AxReferenceKey(key));
111             } else {
112                 copy.key = null;
113             }
114             copy.doubleValue = doubleValue;
115             return copy;
116         } else {
117             return null;
118         }
119     }
120
121     @Override
122     public int hashCode() {
123         final int prime = 31;
124         int result = 1;
125         result = prime * result + ((key == null) ? 0 : key.hashCode());
126         return result;
127     }
128
129     @Override
130     public boolean equals(final Object obj) {
131         if (obj == null)
132             return false;
133         if (this == obj)
134             return true;
135         if (getClass() != obj.getClass())
136             return false;
137         final ReferenceKeyTestEntity other = (ReferenceKeyTestEntity) obj;
138         if (key == null) {
139             if (other.key != null)
140                 return false;
141         } else if (!key.equals(other.key))
142             return false;
143         if (doubleValue != other.doubleValue)
144             return false;
145         return true;
146     }
147
148     @Override
149     public int compareTo(final AxConcept otherObj) {
150         if (otherObj == null)
151             return -1;
152         if (this == otherObj)
153             return 0;
154         final ReferenceKeyTestEntity other = (ReferenceKeyTestEntity) otherObj;
155         if (key == null) {
156             if (other.key != null)
157                 return 1;
158         } else if (!key.equals(other.key))
159             return key.compareTo(other.key);
160         if (doubleValue != other.doubleValue)
161             return new Double(doubleValue).compareTo(other.doubleValue);
162
163         return 0;
164     }
165 }