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 javax.ws.rs.core.Response;
27 import lombok.EqualsAndHashCode;
28 import lombok.NonNull;
29 import lombok.ToString;
31 import org.onap.policy.common.utils.validation.Assertions;
32 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
35 * This class records a usage of a key in the system. When the list of keys being used by a concept
36 * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is
37 * created for every key occurrence. The list of keys returned by the getKeys() method is a list of
38 * {@link PfKeyUse} objects.
40 * <p>Validation checks that each key is valid.
42 @EqualsAndHashCode(callSuper = false)
44 public class PfKeyUse extends PfKey {
45 private static final long serialVersionUID = 2007147220109881705L;
47 private PfKey usedKey;
50 * The Default Constructor creates this concept with a null key.
53 this(new PfConceptKey());
57 * This constructor creates an instance of this class, and holds a reference to a used key.
59 * @param usedKey a used key
61 public PfKeyUse(@NonNull final PfKey usedKey) {
62 this.usedKey = usedKey;
68 * @param copyConcept the concept to copy from
70 public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
75 public PfKey getKey() {
80 public List<PfKey> getKeys() {
81 return usedKey.getKeys();
85 public String getId() {
86 return usedKey.getId();
90 public boolean isNullKey() {
91 return usedKey.isNullKey();
99 public void setKey(@NonNull final PfKey key) {
104 public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
105 return usedKey.getCompatibility(otherKey);
109 public boolean isCompatible(@NonNull final PfKey otherKey) {
110 return usedKey.isCompatible(otherKey);
114 public void clean() {
119 public PfValidationResult validate(@NonNull final PfValidationResult result) {
120 if (usedKey.isNullKey()) {
121 result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
122 "usedKey is a null key"));
124 return usedKey.validate(result);
128 public int compareTo(final PfConcept otherObj) {
129 Assertions.argumentNotNull(otherObj, "comparison object may not be null");
131 if (this == otherObj) {
134 if (getClass() != otherObj.getClass()) {
135 return this.hashCode() - otherObj.hashCode();
138 final PfKeyUse other = (PfKeyUse) otherObj;
140 return usedKey.compareTo(other.usedKey);
144 public PfConcept copyTo(@NonNull final PfConcept target) {
145 final Object copyObject = target;
146 Assertions.instanceOf(copyObject, PfKeyUse.class);
148 final PfKeyUse copy = ((PfKeyUse) copyObject);
150 copy.usedKey = usedKey.getClass().newInstance();
151 } catch (final Exception e) {
152 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
153 "error copying concept key: " + e.getMessage(), e);
155 usedKey.copyTo(copy.usedKey);