2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import java.util.List;
25 import lombok.EqualsAndHashCode;
26 import lombok.ToString;
28 import org.onap.policy.common.utils.validation.Assertions;
29 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
32 * 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
33 * getKeys() method of the {@link PfConcept} class, an instance of this class is created for every key occurrence. The
34 * list of keys returned by the getKeys() method is a list of {@link PfKeyUse} objects.
36 * <p>Validation checks that each key is valid.
38 @EqualsAndHashCode(callSuper = false)
40 public class PfKeyUse extends PfKey {
41 private static final long serialVersionUID = 2007147220109881705L;
43 private PfKey usedKey;
46 * The Default Constructor creates this concept with a null key.
49 this(new PfConceptKey());
55 * @param copyConcept the concept to copy from
57 public PfKeyUse(final PfKeyUse copyConcept) {
62 * This constructor creates an instance of this class, and holds a reference to a used key.
64 * @param usedKey a used key
66 public PfKeyUse(final PfKey usedKey) {
67 Assertions.argumentNotNull(usedKey, "usedKey may not be null");
68 this.usedKey = usedKey;
72 public PfKey getKey() {
77 public List<PfKey> getKeys() {
78 return usedKey.getKeys();
82 public String getId() {
83 return usedKey.getId();
91 public void setKey(final PfKey key) {
92 Assertions.argumentNotNull(key, "usedKey may not be null");
97 public PfKey.Compatibility getCompatibility(final PfKey otherKey) {
98 return usedKey.getCompatibility(otherKey);
102 public boolean isCompatible(final PfKey otherKey) {
103 return usedKey.isCompatible(otherKey);
107 public PfValidationResult validate(final PfValidationResult result) {
108 if (usedKey.equals(PfConceptKey.getNullKey())) {
109 result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
110 "usedKey is a null key"));
112 return usedKey.validate(result);
116 public void clean() {
121 public PfConcept copyTo(final PfConcept target) {
122 Assertions.argumentNotNull(target, "target may not be null");
124 final Object copyObject = target;
125 Assertions.instanceOf(copyObject, PfKeyUse.class);
127 final PfKeyUse copy = ((PfKeyUse) copyObject);
129 copy.usedKey = usedKey.getClass().newInstance();
130 } catch (final Exception e) {
131 throw new PfModelRuntimeException("error copying concept key: " + e.getMessage(), e);
133 usedKey.copyTo(copy.usedKey);
139 public int compareTo(final PfConcept otherObj) {
140 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
142 if (this == otherObj) {
145 if (getClass() != otherObj.getClass()) {
146 return this.hashCode() - otherObj.hashCode();
149 final PfKeyUse other = (PfKeyUse) otherObj;
151 return usedKey.compareTo(other.usedKey);