Replace copyTo methods with copy constructors
[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  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
24 import java.util.List;
25 import lombok.EqualsAndHashCode;
26 import lombok.NonNull;
27 import lombok.ToString;
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
33  * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is
34  * created for every key occurrence. The list of keys returned by the getKeys() method is a list of
35  * {@link PfKeyUse} objects.
36  *
37  * <p>Validation checks that each key is valid.
38  */
39 @EqualsAndHashCode(callSuper = false)
40 @ToString
41 public class PfKeyUse extends PfKey {
42     private static final long serialVersionUID = 2007147220109881705L;
43
44     private PfKey usedKey;
45
46     /**
47      * The Default Constructor creates this concept with a null key.
48      */
49     public PfKeyUse() {
50         this(new PfConceptKey());
51     }
52
53     /**
54      * This constructor creates an instance of this class, and holds a reference to a used key.
55      *
56      * @param usedKey a used key
57      */
58     public PfKeyUse(@NonNull final PfKey usedKey) {
59         this.usedKey = usedKey;
60     }
61
62     /**
63      * Copy constructor.
64      *
65      * @param copyConcept the concept to copy from
66      */
67     public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
68         super(copyConcept);
69         this.usedKey = PfUtils.makeCopy(copyConcept.usedKey);
70     }
71
72     @Override
73     public PfKey getKey() {
74         return usedKey;
75     }
76
77     @Override
78     public List<PfKey> getKeys() {
79         return usedKey.getKeys();
80     }
81
82     @Override
83     public String getId() {
84         return usedKey.getId();
85     }
86
87     @Override
88     public boolean isNullKey() {
89         return usedKey.isNullKey();
90     }
91
92     /**
93      * Sets the key.
94      *
95      * @param key the key
96      */
97     public void setKey(@NonNull final PfKey key) {
98         this.usedKey = key;
99     }
100
101     @Override
102     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
103         return usedKey.getCompatibility(otherKey);
104     }
105
106     @Override
107     public boolean isCompatible(@NonNull final PfKey otherKey) {
108         return usedKey.isCompatible(otherKey);
109     }
110
111     @Override
112     public boolean isNewerThan(@NonNull final PfKey otherKey) {
113         return usedKey.isCompatible(otherKey);
114     }
115
116     @Override
117     public int getMajorVersion() {
118         return usedKey.getMajorVersion();
119     }
120
121     @Override
122     public int getMinorVersion() {
123         return usedKey.getMinorVersion();
124     }
125
126     @Override
127     public int getPatchVersion() {
128         return usedKey.getPatchVersion();
129     }
130
131     @Override
132     public void clean() {
133         usedKey.clean();
134     }
135
136     @Override
137     public PfValidationResult validate(@NonNull final PfValidationResult result) {
138         if (usedKey.isNullKey()) {
139             result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
140                     "usedKey is a null key"));
141         }
142         return usedKey.validate(result);
143     }
144
145     @Override
146     public int compareTo(final PfConcept otherObj) {
147         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
148
149         if (this == otherObj) {
150             return 0;
151         }
152         if (getClass() != otherObj.getClass()) {
153             return getClass().getName().compareTo(otherObj.getClass().getName());
154         }
155
156         final PfKeyUse other = (PfKeyUse) otherObj;
157
158         return usedKey.compareTo(other.usedKey);
159     }
160 }