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