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.tradfri.internal.model;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
23 * The {@link TradfriVersion} class is a default implementation for comparing TRÅDFRI versions.
25 * @author Christoph Weitkamp - Initial contribution
28 public class TradfriVersion implements Comparable<TradfriVersion> {
29 private static final String VERSION_PATTERN = "[0-9]+(\\.[0-9]+)*";
30 private static final String VERSION_DELIMITER = "\\.";
31 final List<Integer> parts;
34 * Create a new instance.
36 * @param version the version string
38 public TradfriVersion(final String version) {
39 if (!version.matches(VERSION_PATTERN)) {
40 throw new IllegalArgumentException("TradfriVersion cannot be created as version has invalid format.");
42 parts = Arrays.stream(version.split(VERSION_DELIMITER)).map(part -> Integer.parseInt(part))
43 .collect(Collectors.toList());
47 public int compareTo(final TradfriVersion other) {
48 int minSize = Math.min(parts.size(), other.parts.size());
49 for (int i = 0; i < minSize; ++i) {
50 int diff = parts.get(i) - other.parts.get(i);
53 } else if (diff < 0) {
59 for (int i = minSize; i < parts.size(); ++i) {
60 if (parts.get(i) != 0) {
64 for (int i = minSize; i < other.parts.size(); ++i) {
65 if (other.parts.get(i) != 0) {
73 public boolean equals(@Nullable Object obj) {
80 if (getClass() != obj.getClass()) {
83 return compareTo((TradfriVersion) obj) == 0;
87 public String toString() {
88 return parts.stream().map(String::valueOf).collect(Collectors.joining("."));