ff2bf9354f1c48cdf3fb1f5762522c45f14b8d20
[policy/apex-pdp.git] / model / basic-model / src / test / java / org / onap / policy / apex / model / basicmodel / concepts / DummyEntity.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.basicmodel.concepts;
23
24 import java.util.Arrays;
25 import java.util.List;
26
27 import javax.persistence.EmbeddedId;
28 import javax.persistence.Entity;
29 import javax.persistence.Table;
30 import javax.xml.bind.annotation.XmlElement;
31 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
32
33 import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter;
34
35 @Entity
36 @Table(name = "TestEntity")
37
38 public class DummyEntity extends AxConcept {
39     private static final long serialVersionUID = -2962570563281067894L;
40
41     @EmbeddedId()
42     @XmlElement(name = "key", required = true)
43     @XmlJavaTypeAdapter(AxReferenceKeyAdapter.class)
44     protected AxReferenceKey key;
45
46     private double doubleValue;
47
48     public DummyEntity() {
49         this.key = new AxReferenceKey();
50         this.doubleValue = 0;
51     }
52
53     public DummyEntity(Double doubleValue) {
54         this.key = new AxReferenceKey();
55         this.doubleValue = doubleValue;
56     }
57
58     public DummyEntity(AxReferenceKey key, Double doubleValue) {
59         this.key = key;
60         this.doubleValue = doubleValue;
61     }
62
63     @Override
64     public AxReferenceKey getKey() {
65         return key;
66     }
67
68     @Override
69     public List<AxKey> getKeys() {
70         return Arrays.asList((AxKey) getKey());
71     }
72
73     public void setKey(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(double doubleValue) {
86         this.doubleValue = doubleValue;
87     }
88
89     @Override
90     public AxValidationResult validate(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         final StringBuilder builder = new StringBuilder();
102         builder.append("doubleValue=");
103         builder.append(doubleValue);
104         return builder.toString();
105     }
106
107     @Override
108     public AxConcept copyTo(AxConcept target) {
109         final Object copyObject = ((target == null) ? new DummyEntity() : target);
110         if (copyObject instanceof DummyEntity) {
111             final DummyEntity copy = ((DummyEntity) copyObject);
112             if (this.checkSetKey()) {
113                 copy.setKey(new AxReferenceKey(key));
114             } else {
115                 copy.key = null;
116             }
117             copy.doubleValue = doubleValue;
118             return copy;
119         } else {
120             return null;
121         }
122     }
123
124     @Override
125     public int hashCode() {
126         final int prime = 31;
127         int result = 1;
128         result = prime * result + ((key == null) ? 0 : key.hashCode());
129         return result;
130     }
131
132     @Override
133     public boolean equals(Object obj) {
134         if (obj == null) {
135             return false;
136         }
137         if (this == obj) {
138             return true;
139         }
140         if (getClass() != obj.getClass()) {
141             return false;
142         }
143         DummyEntity other = (DummyEntity) obj;
144         if (key == null) {
145             if (other.key != null) {
146                 return false;
147             }
148         } else if (!key.equals(other.key)) {
149             return false;
150         }
151         if (doubleValue != other.doubleValue) {
152             return false;
153         }
154         return true;
155     }
156
157     @Override
158     public int compareTo(AxConcept otherObj) {
159         if (otherObj == null) {
160             return -1;
161         }
162         if (this == otherObj) {
163             return 0;
164         }
165         DummyEntity other = (DummyEntity) otherObj;
166         if (key == null) {
167             if (other.key != null) {
168                 return 1;
169             }
170         } else if (!key.equals(other.key)) {
171             return key.compareTo(other.key);
172         }
173         if (doubleValue != other.doubleValue) {
174             return Double.valueOf(doubleValue).compareTo(other.doubleValue);
175         }
176
177         return 0;
178     }
179 }