2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.hue.internal.api.dto.clip1;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
19 * Represents the version of the API of the form 1.0 or 1.2.1
21 * @author Samuel Leisering - Initial contribution
23 public class ApiVersion {
24 private final int major;
25 private final int minor;
26 private final int micro;
28 private static final Pattern VERSION_PATTERN = Pattern.compile("^([0-9]+)\\.([0-9]+)(\\.([0-9]+))?$");
30 public ApiVersion(int major, int minor, int micro) {
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);
44 return new ApiVersion(major, minor, micro);
47 throw new IllegalArgumentException("Version \"" + version + "\" is not valid");
51 * returns the major version part of the version
53 * @return the major part of the version
55 public int getMajor() {
60 * returns the minor version part of the version
62 * @return the minor part of the version
64 public int getMinor() {
69 * returns the micro version part of the version
71 * @return the micro part of the version
73 public int getMicro() {
78 * compare API versions according to {@link java.util.Comparator#compare(Object, Object)}
83 public int compare(ApiVersion other) {
84 int c = Integer.compare(major, other.major);
86 c = Integer.compare(minor, other.minor);
88 c = Integer.compare(micro, other.micro);
95 public int hashCode() {
98 result = prime * result + major;
99 result = prime * result + micro;
100 result = prime * result + minor;
105 public boolean equals(Object obj) {
112 if (getClass() != obj.getClass()) {
115 ApiVersion other = (ApiVersion) obj;
116 if (major != other.major) {
119 if (micro != other.micro) {
122 return minor == other.minor;
126 public String toString() {
127 return major + "." + minor + "." + micro;