|
|
|
|
|
by _old_dude_
216 days ago
|
|
Yes, the version in Java is clearly less elegant.
Java has map+lambda and compareTo (<=>) but no tuple assignemnt and no splat. record AppVersion(int major, int minor, int patch) implements Comparable<AppVersion> {
public static AppVersion of(String version) {
var array = Arrays.copyOf(Arrays.stream(version.split("\\.")).mapToInt(Integer::parseInt).toArray(), 3);
return new AppVersion(array[0], array[1], array[2]);
}
public int compareTo(AppVersion other) {
return Comparator.comparingInt(AppVersion::major)
.thenComparingInt(AppVersion::minor)
.thenComparingInt(AppVersion::patch)
.compare(this, other);
}
public String toString() {
return "%d.%d.%d".formatted(major, minor, patch);
}
}
|
|