Simplify SecurityTokenInfo.Version using AutoValue

This commit is contained in:
Dominik Schürmann
2017-11-05 23:13:18 +01:00
parent 221eb194d9
commit 106dbdf4a9
2 changed files with 9 additions and 29 deletions

View File

@@ -148,31 +148,25 @@ public abstract class SecurityTokenInfo implements Parcelable {
if (!matcher.matches()) {
return null;
}
return new Version(matcher.group(1));
return Version.create(matcher.group(1));
}
public static class Version implements Comparable<Version> {
@AutoValue
public static abstract class Version implements Comparable<Version> {
private String version;
abstract String getVersion();
public final String get() {
return this.version;
}
public Version(String version) {
if (version == null) {
throw new IllegalArgumentException("Version can not be null");
}
public static Version create(@NonNull String version) {
if (!version.matches("[0-9]+(\\.[0-9]+)*")) {
throw new IllegalArgumentException("Invalid version format");
}
this.version = version;
return new AutoValue_SecurityTokenInfo_Version(version);
}
@Override
public int compareTo(@NonNull Version that) {
String[] thisParts = this.get().split("\\.");
String[] thatParts = that.get().split("\\.");
String[] thisParts = this.getVersion().split("\\.");
String[] thatParts = that.getVersion().split("\\.");
int length = Math.max(thisParts.length, thatParts.length);
for (int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ?
@@ -189,19 +183,5 @@ public abstract class SecurityTokenInfo implements Parcelable {
return 0;
}
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (this.getClass() != that.getClass()) {
return false;
}
return this.compareTo((Version) that) == 0;
}
}
}

View File

@@ -221,7 +221,7 @@ public class UsbTransport implements Transport {
String serialNo = usbConnection.getSerial();
SecurityTokenInfo.Version gnukVersion = SecurityTokenInfo.parseGnukVersionString(serialNo);
boolean versionGreaterEquals125 = gnukVersion != null
&& new SecurityTokenInfo.Version("1.2.5").compareTo(gnukVersion) <= 0;
&& SecurityTokenInfo.Version.create("1.2.5").compareTo(gnukVersion) <= 0;
return versionGreaterEquals125 ? TokenType.GNUK_1_25_AND_NEWER : TokenType.GNUK_OLD;
}
case VENDOR_LEDGER: {