Use ValidationResult for models v2.0
[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-2020 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.parameters.BeanValidationResult;
29 import org.onap.policy.common.parameters.ValidationResult;
30 import org.onap.policy.common.utils.validation.Assertions;
31
32 /**
33  * This class records a usage of a key in the system. When the list of keys being used by a concept
34  * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is
35  * created for every key occurrence. The list of keys returned by the getKeys() method is a list of
36  * {@link PfKeyUse} objects.
37  *
38  * <p>Validation checks that each key is valid.
39  */
40 @EqualsAndHashCode(callSuper = false)
41 @ToString
42 public class PfKeyUse extends PfKey {
43     private static final long serialVersionUID = 2007147220109881705L;
44
45     private PfKey usedKey;
46
47     /**
48      * The Default Constructor creates this concept with a null key.
49      */
50     public PfKeyUse() {
51         this(new PfConceptKey());
52     }
53
54     /**
55      * This constructor creates an instance of this class, and holds a reference to a used key.
56      *
57      * @param usedKey a used key
58      */
59     public PfKeyUse(@NonNull final PfKey usedKey) {
60         this.usedKey = usedKey;
61     }
62
63     /**
64      * Copy constructor.
65      *
66      * @param copyConcept the concept to copy from
67      */
68     public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
69         super(copyConcept);
70         this.usedKey = PfUtils.makeCopy(copyConcept.usedKey);
71     }
72
73     @Override
74     public PfKey getKey() {
75         return usedKey;
76     }
77
78     @Override
79     public List<PfKey> getKeys() {
80         return usedKey.getKeys();
81     }
82
83     @Override
84     public String getId() {
85         return usedKey.getId();
86     }
87
88     @Override
89     public boolean isNullKey() {
90         return usedKey.isNullKey();
91     }
92
93     /**
94      * Sets the key.
95      *
96      * @param key the key
97      */
98     public void setKey(@NonNull final PfKey key) {
99         this.usedKey = key;
100     }
101
102     @Override
103     public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
104         return usedKey.getCompatibility(otherKey);
105     }
106
107     @Override
108     public boolean isCompatible(@NonNull final PfKey otherKey) {
109         return usedKey.isCompatible(otherKey);
110     }
111
112     @Override
113     public boolean isNewerThan(@NonNull final PfKey otherKey) {
114         return usedKey.isCompatible(otherKey);
115     }
116
117     @Override
118     public int getMajorVersion() {
119         return usedKey.getMajorVersion();
120     }
121
122     @Override
123     public int getMinorVersion() {
124         return usedKey.getMinorVersion();
125     }
126
127     @Override
128     public int getPatchVersion() {
129         return usedKey.getPatchVersion();
130     }
131
132     @Override
133     public void clean() {
134         usedKey.clean();
135     }
136
137     @Override
138     public ValidationResult validate(@NonNull String fieldName) {
139         BeanValidationResult result = new BeanValidationResult(fieldName, this);
140         result.addResult(validateKeyNotNull("usedKey", usedKey));
141         return result;
142     }
143
144     @Override
145     public int compareTo(final PfConcept otherObj) {
146         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
147
148         if (this == otherObj) {
149             return 0;
150         }
151         if (getClass() != otherObj.getClass()) {
152             return getClass().getName().compareTo(otherObj.getClass().getName());
153         }
154
155         final PfKeyUse other = (PfKeyUse) otherObj;
156
157         return usedKey.compareTo(other.usedKey);
158     }
159 }