Added basic Subsonic interface

This commit is contained in:
CappielloAntonio 2021-07-24 12:27:45 +02:00
parent 3fa634df39
commit 30ded0951b
5 changed files with 221 additions and 0 deletions

View file

@ -0,0 +1,13 @@
package com.cappielloantonio.play.subsonic.base;
public class SubsonicIncompatibilityException extends RuntimeException{
private final Version serverApiVersion;
private final Version minClientApiVersion;
public SubsonicIncompatibilityException(Version serverApiVersion, Version minClientApiVersion) {
super(String.format("Server API version %s is lower than minimal supported API version %s.", serverApiVersion, minClientApiVersion));
this.serverApiVersion = serverApiVersion;
this.minClientApiVersion = minClientApiVersion;
}
}

View file

@ -0,0 +1,56 @@
package com.cappielloantonio.play.subsonic.base;
public class Version implements Comparable<Version> {
private static final String VERSION_PATTERN = "[0-9]+(\\.[0-9]+)*";
private final String versionString;
public static Version of(String versionString) {
return new Version(versionString);
}
private Version(String versionString) {
if (versionString == null || !versionString.matches(VERSION_PATTERN)) {
throw new IllegalArgumentException("Invalid version format");
}
this.versionString = versionString;
}
public String getVersionString() {
return versionString;
}
public boolean isLowerThan(Version version) {
return compareTo(version) < 0;
}
@Override
public int compareTo(Version that) {
if (that == null) {
return 1;
}
String[] thisParts = this.getVersionString().split("\\.");
String[] thatParts = that.getVersionString().split("\\.");
int length = Math.max(thisParts.length, thatParts.length);
for (int i = 0; i < length; i++) {
int thisPart = i < thisParts.length ? Integer.parseInt(thisParts[i]) : 0;
int thatPart = i < thatParts.length ? Integer.parseInt(thatParts[i]) : 0;
if (thisPart < thatPart) {
return -1;
}
if (thisPart > thatPart) {
return 1;
}
}
return 0;
}
@Override
public String toString() {
return versionString;
}
}