]> git.basschouten.com Git - openhab-addons.git/blob
3e08b7c0392fa9d13daa0bc51dc39e7ffdfb2d56
[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.tradfri.internal.model;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * The {@link TradfriVersion} class is a default implementation for comparing TRÅDFRI versions.
24  *
25  * @author Christoph Weitkamp - Initial contribution
26  */
27 @NonNullByDefault
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;
32
33     /**
34      * Create a new instance.
35      *
36      * @param version the version string
37      */
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.");
41         }
42         parts = Arrays.stream(version.split(VERSION_DELIMITER)).map(part -> Integer.parseInt(part))
43                 .collect(Collectors.toList());
44     }
45
46     @Override
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);
51             if (diff == 0) {
52                 continue;
53             } else if (diff < 0) {
54                 return -1;
55             } else {
56                 return 1;
57             }
58         }
59         for (int i = minSize; i < parts.size(); ++i) {
60             if (parts.get(i) != 0) {
61                 return 1;
62             }
63         }
64         for (int i = minSize; i < other.parts.size(); ++i) {
65             if (other.parts.get(i) != 0) {
66                 return -1;
67             }
68         }
69         return 0;
70     }
71
72     @Override
73     public boolean equals(@Nullable Object obj) {
74         if (this == obj) {
75             return true;
76         }
77         if (obj == null) {
78             return false;
79         }
80         if (getClass() != obj.getClass()) {
81             return false;
82         }
83         return compareTo((TradfriVersion) obj) == 0;
84     }
85
86     @Override
87     public String toString() {
88         return parts.stream().map(String::valueOf).collect(Collectors.joining("."));
89     }
90 }