]> git.basschouten.com Git - openhab-addons.git/blob
0180aff0a84a653e9708979c487fe9be0fe82f44
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hue.internal.api.dto.clip1;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 /**
19  * Represents the version of the API of the form 1.0 or 1.2.1
20  *
21  * @author Samuel Leisering - Initial contribution
22  */
23 public class ApiVersion {
24     private final int major;
25     private final int minor;
26     private final int micro;
27
28     private static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+)\\.([0-9]+)(\\.([0-9]+))?$");
29
30     public ApiVersion(int major, int minor, int micro) {
31         this.major = major;
32         this.minor = minor;
33         this.micro = micro;
34     }
35
36     public static ApiVersion of(String version) {
37         Matcher matcher = VERSION_PATTERN.matcher(version);
38         if (matcher.matches()) {
39             int major = Integer.parseInt(matcher.group(1));
40             int minor = Integer.parseInt(matcher.group(2));
41             String microString = matcher.group(4);
42             int micro = Integer.parseInt(microString == null ? "0" : microString);
43
44             return new ApiVersion(major, minor, micro);
45         }
46
47         throw new IllegalArgumentException("Version \"" + version + "\" is not valid");
48     }
49
50     /**
51      * returns the major version part of the version
52      *
53      * @return the major part of the version
54      */
55     public int getMajor() {
56         return major;
57     }
58
59     /**
60      * returns the minor version part of the version
61      *
62      * @return the minor part of the version
63      */
64     public int getMinor() {
65         return minor;
66     }
67
68     /**
69      * returns the micro version part of the version
70      *
71      * @return the micro part of the version
72      */
73     public int getMicro() {
74         return micro;
75     }
76
77     /**
78      * compare API versions according to {@link java.util.Comparator#compare(Object, Object)}
79      *
80      * @param other
81      * @return
82      */
83     public int compare(ApiVersion other) {
84         int c = Integer.compare(major, other.major);
85         if (c == 0) {
86             c = Integer.compare(minor, other.minor);
87             if (c == 0) {
88                 c = Integer.compare(micro, other.micro);
89             }
90         }
91         return c;
92     }
93
94     @Override
95     public int hashCode() {
96         final int prime = 31;
97         int result = 1;
98         result = prime * result + major;
99         result = prime * result + micro;
100         result = prime * result + minor;
101         return result;
102     }
103
104     @Override
105     public boolean equals(Object obj) {
106         if (this == obj) {
107             return true;
108         }
109         if (obj == null) {
110             return false;
111         }
112         if (getClass() != obj.getClass()) {
113             return false;
114         }
115         ApiVersion other = (ApiVersion) obj;
116         if (major != other.major) {
117             return false;
118         }
119         if (micro != other.micro) {
120             return false;
121         }
122         return minor == other.minor;
123     }
124
125     @Override
126     public String toString() {
127         return major + "." + minor + "." + micro;
128     }
129 }