8eed845ada9c1e566d5d29c9dad1ee48ac10aa57
[policy/apex-pdp.git] / model / basic-model / src / main / java / org / onap / policy / apex / model / basicmodel / concepts / AxKeyUse.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-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.List;
25
26 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
27 import org.onap.policy.common.utils.validation.Assertions;
28
29 /**
30  * This class records a usage of a key in the system. When the list of keys being used by a concept is built using the
31  * getKeys() method of the {@link AxConcept} class, an instance of this class is created for every key occurrence. The
32  * list of keys returned by the getKeys() method is a list of {@link AxKeyUse} objects.
33  *
34  * <p>Validation checks that each key is valid.
35  */
36
37 public class AxKeyUse extends AxKey {
38     private static final long serialVersionUID = 2007147220109881705L;
39
40     private AxKey usedKey;
41
42     /**
43      * The Default Constructor creates this concept with a null key.
44      */
45     public AxKeyUse() {
46         this(new AxArtifactKey());
47     }
48
49     /**
50      * Copy constructor.
51      *
52      * @param copyConcept the concept to copy from
53      */
54     public AxKeyUse(final AxKeyUse copyConcept) {
55         super(copyConcept);
56     }
57
58     /**
59      * This constructor creates an instance of this class, and holds a reference to a used key.
60      *
61      * @param usedKey a used key
62      */
63     public AxKeyUse(final AxKey usedKey) {
64         Assertions.argumentNotNull(usedKey, "usedKey may not be null");
65         this.usedKey = usedKey;
66     }
67
68     /**
69      * {@inheritDoc}.
70      */
71     @Override
72     public AxKey getKey() {
73         return usedKey;
74     }
75
76     /**
77      * {@inheritDoc}.
78      */
79     @Override
80     public List<AxKey> getKeys() {
81         return usedKey.getKeys();
82     }
83
84     /**
85      * {@inheritDoc}.
86      */
87     @Override
88     public String getId() {
89         return usedKey.getId();
90     }
91
92     /**
93      * Sets the key.
94      *
95      * @param key the key
96      */
97     public void setKey(final AxKey key) {
98         Assertions.argumentNotNull(key, "usedKey may not be null");
99         this.usedKey = key;
100     }
101
102     /**
103      * {@inheritDoc}.
104      */
105     @Override
106     public AxKey.Compatibility getCompatibility(final AxKey otherKey) {
107         return usedKey.getCompatibility(otherKey);
108     }
109
110     /**
111      * {@inheritDoc}.
112      */
113     @Override
114     public boolean isCompatible(final AxKey otherKey) {
115         return usedKey.isCompatible(otherKey);
116     }
117
118     /**
119      * {@inheritDoc}.
120      */
121     @Override
122     public AxValidationResult validate(final AxValidationResult result) {
123         if (usedKey.equals(AxArtifactKey.getNullKey())) {
124             result.addValidationMessage(new AxValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
125                     "usedKey is a null key"));
126         }
127         return usedKey.validate(result);
128     }
129
130     /**
131      * {@inheritDoc}.
132      */
133     @Override
134     public void clean() {
135         usedKey.clean();
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public String toString() {
143         final StringBuilder builder = new StringBuilder();
144         builder.append(this.getClass().getSimpleName());
145         builder.append(":(");
146         builder.append("usedKey=");
147         builder.append(usedKey);
148         builder.append(")");
149         return builder.toString();
150     }
151
152     /**
153      * {@inheritDoc}.
154      */
155     @Override
156     public AxConcept copyTo(final AxConcept target) {
157         Assertions.argumentNotNull(target, "target may not be null");
158
159         final Object copyObject = target;
160         Assertions.instanceOf(copyObject, AxKeyUse.class);
161
162         final AxKeyUse copy = ((AxKeyUse) copyObject);
163         try {
164             copy.usedKey = usedKey.getClass().getDeclaredConstructor().newInstance();
165         } catch (final Exception e) {
166             throw new ApexRuntimeException("error copying concept key: " + e.getMessage(), e);
167         }
168         usedKey.copyTo(copy.usedKey);
169
170         return copy;
171     }
172
173     /**
174      * {@inheritDoc}.
175      */
176     @Override
177     public int hashCode() {
178         final int prime = 31;
179         int result = 1;
180         result = prime * result + usedKey.hashCode();
181         return result;
182     }
183
184     /**
185      * {@inheritDoc}.
186      */
187     @Override
188     public boolean equals(final Object obj) {
189         if (obj == null) {
190             throw new IllegalArgumentException("comparison object may not be null");
191         }
192
193         if (this == obj) {
194             return true;
195         }
196         if (getClass() != obj.getClass()) {
197             return false;
198         }
199
200         final AxKeyUse other = (AxKeyUse) obj;
201         return usedKey.equals(other.usedKey);
202     }
203
204     /**
205      * {@inheritDoc}.
206      */
207     @Override
208     public int compareTo(final AxConcept otherObj) {
209         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
210
211         if (this == otherObj) {
212             return 0;
213         }
214         if (getClass() != otherObj.getClass()) {
215             return this.hashCode() - otherObj.hashCode();
216         }
217
218         final AxKeyUse other = (AxKeyUse) otherObj;
219
220         return usedKey.compareTo(other.usedKey);
221     }
222 }