37c26fc520d020ea05ff3d6d3d18123db8945377
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / rserv / Version.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 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
22 package org.onap.aaf.auth.rserv;
23
24
25 /**
26  * Analyze and hold Version information for Code
27  *
28  * @author Jonathan
29  *
30  */
31 public class Version {
32     private Object[] parts;
33
34     public Version(String v) {
35         String sparts[] = v.split("\\.");
36         parts = new Object[sparts.length];
37         System.arraycopy(sparts, 0, parts, 0, sparts.length);
38         if (parts.length>1) { // has at least a minor
39           try {
40               parts[1]=Integer.decode(sparts[1]); // minor elements need to be converted to Integer for comparison
41           } catch (NumberFormatException e) {
42               // it's ok, leave it as a string
43               parts[1]=sparts[1]; // This useless piece of code forced by Sonar which calls empty Exceptions "Blockers".
44           }
45         }
46     }
47
48     public boolean equals(Object obj) {
49         if (obj instanceof Version) {
50             Version ver = (Version)obj;
51             int length = Math.min(parts.length, ver.parts.length);
52             for (int i=0;i<length;++i) { // match on declared parts
53                 if (i==1) {
54                     if (parts[1] instanceof Integer && ver.parts[1] instanceof Integer) {
55                         // Match on Minor version if this Version is less than Version to be checked
56                         if (((Integer)parts[1])<((Integer)ver.parts[1])) {
57                             return false;
58                         }
59                         continue; // don't match next line
60                     }
61                 }
62                 if (!parts[i].equals(ver.parts[i])) {
63                     return false; // other spots exact match
64                 }
65             }
66             return true;
67         }
68         return false;
69     }
70
71
72     /* (non-Javadoc)
73      * @see java.lang.Object#hashCode()
74      */
75     @Override
76     public int hashCode() {
77         return super.hashCode();
78     }
79
80     public String toString() {
81         StringBuilder sb = new StringBuilder();
82         boolean first = true;
83         for (Object obj : parts) {
84             if (first) {
85                 first = false;
86             } else {
87                 sb.append('.');
88             }
89             sb.append(obj.toString());
90         }
91         return sb.toString();
92     }
93 }