Remove more duplicate code from models
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / authorative / concepts / ToscaNameVersion.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.authorative.concepts;
22
23 import lombok.AllArgsConstructor;
24 import lombok.Data;
25 import lombok.NoArgsConstructor;
26 import lombok.NonNull;
27 import org.apache.commons.lang3.ObjectUtils;
28 import org.onap.policy.models.base.PfConceptKey;
29 import org.onap.policy.models.base.PfKey;
30
31 /**
32  * Concept with an optional name and version.
33  */
34 @Data
35 @NoArgsConstructor
36 @AllArgsConstructor
37 public class ToscaNameVersion {
38
39     private String name;
40
41     private String version;
42
43     public ToscaNameVersion(@NonNull PfKey key) {
44         this.name = key.getName();
45         this.version = key.getVersion();
46     }
47
48     public ToscaNameVersion(ToscaNameVersion source) {
49         this.name = source.name;
50         this.version = source.version;
51     }
52
53     /**
54      * Create a PfConcceptKey from the TOSCA identifier.
55      *
56      * @return the key
57      */
58     public PfConceptKey asConceptKey() {
59         return new PfConceptKey(name, version);
60     }
61
62     protected int commonCompareTo(ToscaNameVersion other) {
63         if (this == other) {
64             return 0;
65         }
66
67         if (other == null) {
68             return 1;
69         }
70
71         if (getClass() != other.getClass()) {
72             return getClass().getSimpleName().compareTo(other.getClass().getSimpleName());
73         }
74
75         int result = ObjectUtils.compare(getName(), other.getName());
76         if (result != 0) {
77             return result;
78         }
79
80         return ObjectUtils.compare(getVersion(), other.getVersion());
81     }
82
83     @Override
84     public String toString() {
85         return this.name + " " + this.version;
86     }
87 }