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;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
27 import org.onap.policy.common.utils.validation.Assertions;
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.
34 * <p>Validation checks that each key is valid.
37 public class AxKeyUse extends AxKey {
38 private static final long serialVersionUID = 2007147220109881705L;
40 private AxKey usedKey;
43 * The Default Constructor creates this concept with a null key.
46 this(new AxArtifactKey());
52 * @param copyConcept the concept to copy from
54 public AxKeyUse(final AxKeyUse copyConcept) {
59 * This constructor creates an instance of this class, and holds a reference to a used key.
61 * @param usedKey a used key
63 public AxKeyUse(final AxKey usedKey) {
64 Assertions.argumentNotNull(usedKey, "usedKey may not be null");
65 this.usedKey = usedKey;
72 public AxKey getKey() {
80 public List<AxKey> getKeys() {
81 return usedKey.getKeys();
88 public String getId() {
89 return usedKey.getId();
97 public void setKey(final AxKey key) {
98 Assertions.argumentNotNull(key, "usedKey may not be null");
106 public AxKey.Compatibility getCompatibility(final AxKey otherKey) {
107 return usedKey.getCompatibility(otherKey);
114 public boolean isCompatible(final AxKey otherKey) {
115 return usedKey.isCompatible(otherKey);
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"));
127 return usedKey.validate(result);
134 public void clean() {
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);
149 return builder.toString();
156 public AxConcept copyTo(final AxConcept target) {
157 Assertions.argumentNotNull(target, "target may not be null");
159 final Object copyObject = target;
160 Assertions.instanceOf(copyObject, AxKeyUse.class);
162 final AxKeyUse copy = ((AxKeyUse) copyObject);
164 copy.usedKey = usedKey.getClass().getDeclaredConstructor().newInstance();
165 } catch (final Exception e) {
166 throw new ApexRuntimeException("error copying concept key: " + e.getMessage(), e);
168 usedKey.copyTo(copy.usedKey);
177 public int hashCode() {
178 final int prime = 31;
180 result = prime * result + usedKey.hashCode();
188 public boolean equals(final Object obj) {
190 throw new IllegalArgumentException("comparison object may not be null");
196 if (getClass() != obj.getClass()) {
200 final AxKeyUse other = (AxKeyUse) obj;
201 return usedKey.equals(other.usedKey);
208 public int compareTo(final AxConcept otherObj) {
209 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
211 if (this == otherObj) {
214 if (getClass() != otherObj.getClass()) {
215 return this.hashCode() - otherObj.hashCode();
218 final AxKeyUse other = (AxKeyUse) otherObj;
220 return usedKey.compareTo(other.usedKey);