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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.concepts;
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;
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.
33 * <p>Validation checks that each key is valid.
36 public class AxKeyUse extends AxKey {
37 private static final long serialVersionUID = 2007147220109881705L;
39 private AxKey usedKey;
42 * The Default Constructor creates this concept with a null key.
45 this(new AxArtifactKey());
51 * @param copyConcept the concept to copy from
53 public AxKeyUse(final AxKeyUse copyConcept) {
58 * This constructor creates an instance of this class, and holds a reference to a used key.
60 * @param usedKey a used key
62 public AxKeyUse(final AxKey usedKey) {
63 Assertions.argumentNotNull(usedKey, "usedKey may not be null");
64 this.usedKey = usedKey;
71 public AxKey getKey() {
79 public List<AxKey> getKeys() {
80 return usedKey.getKeys();
87 public String getId() {
88 return usedKey.getId();
96 public void setKey(final AxKey key) {
97 Assertions.argumentNotNull(key, "usedKey may not be null");
105 public AxKey.Compatibility getCompatibility(final AxKey otherKey) {
106 return usedKey.getCompatibility(otherKey);
113 public boolean isCompatible(final AxKey otherKey) {
114 return usedKey.isCompatible(otherKey);
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"));
126 return usedKey.validate(result);
133 public void clean() {
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);
148 return builder.toString();
155 public AxConcept copyTo(final AxConcept target) {
156 Assertions.argumentNotNull(target, "target may not be null");
158 final Object copyObject = target;
159 Assertions.instanceOf(copyObject, AxKeyUse.class);
161 final AxKeyUse copy = ((AxKeyUse) copyObject);
163 copy.usedKey = usedKey.getClass().getDeclaredConstructor().newInstance();
164 } catch (final Exception e) {
165 throw new ApexRuntimeException("error copying concept key: " + e.getMessage(), e);
167 usedKey.copyTo(copy.usedKey);
176 public int hashCode() {
177 final int prime = 31;
179 result = prime * result + usedKey.hashCode();
187 public boolean equals(final Object obj) {
189 throw new IllegalArgumentException("comparison object may not be null");
195 if (getClass() != obj.getClass()) {
199 final AxKeyUse other = (AxKeyUse) obj;
200 return usedKey.equals(other.usedKey);
207 public int compareTo(final AxConcept otherObj) {
208 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
210 if (this == otherObj) {
213 if (getClass() != otherObj.getClass()) {
214 return this.hashCode() - otherObj.hashCode();
217 final AxKeyUse other = (AxKeyUse) otherObj;
219 return usedKey.compareTo(other.usedKey);