This repository has been archived by the owner on May 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tradfri] Added better version check (#4930)
* Added better version check Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
- Loading branch information
Showing
4 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...rc/test/java/org/eclipse/smarthome/binding/tradfri/internal/model/TradfriVersionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.binding.tradfri.internal.model; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for {@link TradfriVersion}. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
public class TradfriVersionTest { | ||
|
||
private static final int LESS_THAN = -1; | ||
private static final int EQUAL_TO = 0; | ||
private static final int GREATER_THAN = 1; | ||
|
||
private static final String VERSION_STRING = "1.2.42"; | ||
private static final TradfriVersion VERSION = new TradfriVersion(VERSION_STRING); | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testIllegalArgumentException() throws IllegalArgumentException { | ||
new TradfriVersion("FAILURE"); | ||
} | ||
|
||
@Test | ||
public void testParts() { | ||
assertEquals(Arrays.asList(1, 2, 42), VERSION.parts); | ||
} | ||
|
||
@Test | ||
public void testCompareToEqualTo() { | ||
assertEquals(EQUAL_TO, VERSION.compareTo(VERSION)); | ||
assertEquals(EQUAL_TO, VERSION.compareTo(new TradfriVersion(VERSION_STRING))); | ||
} | ||
|
||
@Test | ||
public void testCompareToLessThan() { | ||
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("2"))); | ||
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("1.3"))); | ||
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("1.2.50"))); | ||
assertEquals(LESS_THAN, VERSION.compareTo(new TradfriVersion("1.2.42.5"))); | ||
} | ||
|
||
@Test | ||
public void testCompareToGreaterThan() { | ||
assertEquals(GREATER_THAN, VERSION.compareTo(new TradfriVersion("1"))); | ||
assertEquals(GREATER_THAN, VERSION.compareTo(new TradfriVersion("1.1"))); | ||
assertEquals(GREATER_THAN, VERSION.compareTo(new TradfriVersion("1.2.30"))); | ||
} | ||
|
||
@SuppressWarnings("unlikely-arg-type") | ||
@Test | ||
public void testEquals() { | ||
assertTrue(VERSION.equals(VERSION)); | ||
assertTrue(VERSION.equals(new TradfriVersion(VERSION_STRING))); | ||
|
||
assertFalse(VERSION.equals((TradfriVersion) null)); | ||
assertFalse(VERSION.equals(new Integer("1"))); | ||
assertFalse(VERSION.equals(new TradfriVersion("1.2.5"))); | ||
} | ||
|
||
@Test | ||
public void testToString() { | ||
assertEquals(VERSION_STRING, VERSION.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...ri/src/main/java/org/eclipse/smarthome/binding/tradfri/internal/model/TradfriVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.smarthome.binding.tradfri.internal.model; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
|
||
/** | ||
* The {@link TradfriVersion} class is a default implementation for comparing TRÅDFRI versions. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public class TradfriVersion implements Comparable<TradfriVersion> { | ||
private static final String VERSION_PATTERN = "[0-9]+(\\.[0-9]+)*"; | ||
private static final String VERSION_DELIMITER = "\\."; | ||
final List<Integer> parts; | ||
|
||
/** | ||
* Create a new instance. | ||
* | ||
* @param version the version string | ||
*/ | ||
public TradfriVersion(final String version) { | ||
if (!version.matches(VERSION_PATTERN)) { | ||
throw new IllegalArgumentException("TradfriVersion cannot be created as version has invalid format."); | ||
} | ||
parts = Arrays.stream(version.split(VERSION_DELIMITER)).map(part -> Integer.parseInt(part)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public int compareTo(final TradfriVersion other) { | ||
int minSize = Math.min(parts.size(), other.parts.size()); | ||
for (int i = 0; i < minSize; ++i) { | ||
int diff = parts.get(i) - other.parts.get(i); | ||
if (diff == 0) { | ||
continue; | ||
} else if (diff < 0) { | ||
return -1; | ||
} else { | ||
return 1; | ||
} | ||
} | ||
for (int i = minSize; i < parts.size(); ++i) { | ||
if (parts.get(i) != 0) { | ||
return 1; | ||
} | ||
} | ||
for (int i = minSize; i < other.parts.size(); ++i) { | ||
if (other.parts.get(i) != 0) { | ||
return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean equals(@Nullable Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
return compareTo((TradfriVersion) obj) == 0; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return parts.stream().map(String::valueOf).collect(Collectors.joining(".")); | ||
} | ||
} |