Add DAO module for Models
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyUse.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import java.util.List;
24
25 import lombok.EqualsAndHashCode;
26 import lombok.ToString;
27
28 import org.onap.policy.common.utils.validation.Assertions;
29 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
30
31 /**
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.
35  *
36  * <p>Validation checks that each key is valid.
37  */
38 @EqualsAndHashCode(callSuper = false)
39 @ToString
40 public class PfKeyUse extends PfKey {
41     private static final long serialVersionUID = 2007147220109881705L;
42
43     private PfKey usedKey;
44
45     /**
46      * The Default Constructor creates this concept with a null key.
47      */
48     public PfKeyUse() {
49         this(new PfConceptKey());
50     }
51
52     /**
53      * Copy constructor.
54      *
55      * @param copyConcept the concept to copy from
56      */
57     public PfKeyUse(final PfKeyUse copyConcept) {
58         super(copyConcept);
59     }
60
61     /**
62      * This constructor creates an instance of this class, and holds a reference to a used key.
63      *
64      * @param usedKey a used key
65      */
66     public PfKeyUse(final PfKey usedKey) {
67         Assertions.argumentNotNull(usedKey, "usedKey may not be null");
68         this.usedKey = usedKey;
69     }
70
71     @Override
72     public PfKey getKey() {
73         return usedKey;
74     }
75
76     @Override
77     public List<PfKey> getKeys() {
78         return usedKey.getKeys();
79     }
80
81     @Override
82     public String getId() {
83         return usedKey.getId();
84     }
85
86     /**
87      * Sets the key.
88      *
89      * @param key the key
90      */
91     public void setKey(final PfKey key) {
92         Assertions.argumentNotNull(key, "usedKey may not be null");
93         this.usedKey = key;
94     }
95
96     @Override
97     public PfKey.Compatibility getCompatibility(final PfKey otherKey) {
98         return usedKey.getCompatibility(otherKey);
99     }
100
101     @Override
102     public boolean isCompatible(final PfKey otherKey) {
103         return usedKey.isCompatible(otherKey);
104     }
105
106     @Override
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"));
111         }
112         return usedKey.validate(result);
113     }
114
115     @Override
116     public void clean() {
117         usedKey.clean();
118     }
119
120     @Override
121     public PfConcept copyTo(final PfConcept target) {
122         Assertions.argumentNotNull(target, "target may not be null");
123
124         final Object copyObject = target;
125         Assertions.instanceOf(copyObject, PfKeyUse.class);
126
127         final PfKeyUse copy = ((PfKeyUse) copyObject);
128         try {
129             copy.usedKey = usedKey.getClass().newInstance();
130         } catch (final Exception e) {
131             throw new PfModelRuntimeException("error copying concept key: " + e.getMessage(), e);
132         }
133         usedKey.copyTo(copy.usedKey);
134
135         return copy;
136     }
137
138     @Override
139     public int compareTo(final PfConcept otherObj) {
140         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
141
142         if (this == otherObj) {
143             return 0;
144         }
145         if (getClass() != otherObj.getClass()) {
146             return this.hashCode() - otherObj.hashCode();
147         }
148
149         final PfKeyUse other = (PfKeyUse) otherObj;
150
151         return usedKey.compareTo(other.usedKey);
152     }
153 }