diff options
author | Scott Jackson <daneren2005@gmail.com> | 2014-07-21 15:46:53 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2014-07-21 15:46:53 -0700 |
commit | 993be4f3866585cdb9016d69f4eef22ac7eb2eb7 (patch) | |
tree | 7b6307ae11a00b229f08ba56f69cfe55e0d98242 | |
parent | ced0c1db4fef12173c0b65cb394dcb410aae8007 (diff) | |
download | dsub-993be4f3866585cdb9016d69f4eef22ac7eb2eb7.tar.gz dsub-993be4f3866585cdb9016d69f4eef22ac7eb2eb7.tar.bz2 dsub-993be4f3866585cdb9016d69f4eef22ac7eb2eb7.zip |
Added some of the Util checks to ServerInfo, fix formatting
-rw-r--r-- | src/github/daneren2005/dsub/domain/ServerInfo.java | 145 |
1 files changed, 98 insertions, 47 deletions
diff --git a/src/github/daneren2005/dsub/domain/ServerInfo.java b/src/github/daneren2005/dsub/domain/ServerInfo.java index 806d4df5..0109ab30 100644 --- a/src/github/daneren2005/dsub/domain/ServerInfo.java +++ b/src/github/daneren2005/dsub/domain/ServerInfo.java @@ -26,62 +26,113 @@ package github.daneren2005.dsub.domain; public class ServerInfo implements Serializable { public static final int TYPE_SUBSONIC = 1; public static final int TYPE_MADSONIC = 2; + private static final Map<Integer, ServerInfo> SERVERS = new ConcurrentHashMap<Integer, ServerInfo>(); - private boolean isLicenseValid; - private Version restVersion; + private boolean isLicenseValid; + private Version restVersion; private int type; public ServerInfo() { type = TYPE_SUBSONIC; } - public boolean isLicenseValid() { - return isLicenseValid; - } + public boolean isLicenseValid() { + return isLicenseValid; + } - public void setLicenseValid(boolean licenseValid) { - isLicenseValid = licenseValid; - } + public void setLicenseValid(boolean licenseValid) { + isLicenseValid = licenseValid; + } - public Version getRestVersion() { - return restVersion; - } + public Version getRestVersion() { + return restVersion; + } - public void setRestVersion(Version restVersion) { - this.restVersion = restVersion; - } - - public int getRestType() { - return type; - } - public void setRestType(int type) { - this.type = type; - } - - public boolean isStockSubsonic() { - return type == TYPE_SUBSONIC; - } - public boolean isMadsonic() { - return type == TYPE_MADSONIC; - } + public void setRestVersion(Version restVersion) { + this.restVersion = restVersion; + } - @Override - public boolean equals(Object o) { - if(this == 0) { - return true; - } else if(o == null || getClass() != o.getClass()) { - return false; - } - - final ServerInfo info = (ServerInfo) o; - - if(this.type != info.type) { - return false; - } else if(this.restVersion == null || info.restVersion == null) { - // Should never be null unless just starting up - return false; - } else { - return this.restVersion.equals(info.restVersion); - } - } + public int getRestType() { + return type; + } + public void setRestType(int type) { + this.type = type; + } + + public boolean isStockSubsonic() { + return type == TYPE_SUBSONIC; + } + public boolean isMadsonic() { + return type == TYPE_MADSONIC; + } + + @Override + public boolean equals(Object o) { + if(this == 0) { + return true; + } else if(o == null || getClass() != o.getClass()) { + return false; + } + + final ServerInfo info = (ServerInfo) o; + + if(this.type != info.type) { + return false; + } else if(this.restVersion == null || info.restVersion == null) { + // Should never be null unless just starting up + return false; + } else { + return this.restVersion.equals(info.restVersion); + } + } + + public void saveServerInfo(Context context) { + saveServerInfo(context, Util.getActiveServer(context)); + } + public void saveServerInfo(Context context, int instance) { + ServerInfo current = SERVERS.get(instance); + if(!this.equals(current)) { + SERVERS.put(instance, this); + FileUtil.serialize(context, this, getCacheName(context, instance)); + } + } + + public static ServerInfo getServerInfo(Context context) { + return getServerInfo(context, Util.getActiveServer(context)); + } + public static ServerInfo getServerInfo(Context context, int instance) { + ServerInfo current = SERVERS.get(instance); + if(current != null) { + return current; + } + + current = FileUtil.deserialize(context, getCacheName(context, instance), ServerInfo.class); + if(current != null) { + SERVERS.put(instance, current); + } + + return current; + } + + public static boolean checkServerVersion(Context context, String requiredVersion) { + return checkServerVersion(context, requiredVersion, Util.getActiveServer(context)); + } + public static boolean checkServerVersion(Context context, String requiredVersion, int instance) { + ServerInfo server = getServerInfo(context, instance); + if(server == null) { + return false; + } + + Version version = server.getRestVersion(); + if(version == null) { + return false; + } + + Version required = new Version(requiredVersion); + return version.compareTo(required) >= 0; + } + + private static String getCacheName(context, instance) { + return "server-" + Util.getRestUrl(context, null, instance, false).hashCode() + ".ser"; + } } |