commit dc5bbb4209fbe640b1e9f31fae393e2d1b0cfdab
parent a6c62c40362cdc847d223bf6b7f75ba87ca3efc9
Author: Jan Dankert <jan.dankert@hansemerkur.de>
Date: Thu, 7 Nov 2019 17:03:39 +0100
Compare versions with special versions object.
Diffstat:
5 files changed, 81 insertions(+), 12 deletions(-)
diff --git a/.editorconfig b/.editorconfig
@@ -0,0 +1,9 @@
+root = true
+
+# Unix-style newlines with a newline ending every file
+[*]
+end_of_line = lf
+
+# Tab indentation (no size specified)
+[*.java]
+indent_style = tab+
\ No newline at end of file
diff --git a/src/de/openrat/client/CMSClient.java b/src/de/openrat/client/CMSClient.java
@@ -62,7 +62,7 @@ public class CMSClient
/**
* the api version which we are supporting.
*/
- public final static int SUPPORTED_API_VERSION = 2;
+ public final static Version SUPPORTED_API_VERSION = new Version("2");
/**
* The internal connection-object to the cms.
diff --git a/src/de/openrat/client/Version.java b/src/de/openrat/client/Version.java
@@ -0,0 +1,56 @@
+package de.openrat.client;
+
+public class Version implements Comparable<Version> {
+
+ private String version;
+
+ public final String get() {
+ return this.version;
+ }
+
+ public Version(String version) {
+ if (version == null)
+ throw new IllegalArgumentException("Version can not be null");
+ if (!version.matches("[0-9]+(\\.[0-9]+)*"))
+ throw new IllegalArgumentException("Invalid version format");
+ this.version = version;
+ }
+
+ @Override
+ public int compareTo(Version that) {
+ if (that == null)
+ return 1;
+ String[] thisParts = this.get().split("\\.");
+ String[] thatParts = that.get().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 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;
+ }
+
+
+ @Override
+ public String toString() {
+ return "version:" + get();
+ }
+}
+
diff --git a/src/de/openrat/client/util/CMSRequest.java b/src/de/openrat/client/util/CMSRequest.java
@@ -29,6 +29,7 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import de.openrat.client.Version;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
@@ -258,9 +259,9 @@ public class CMSRequest
CMSResponse cmsResponse = new CMSResponse();
// Do we support the server api version?
- int apiVersion = Integer.parseInt(rootNode.getFirstChildByName("api").getValue());
+ Version apiVersion = new Version( rootNode.getFirstChildByName("api").getValue());
- if (apiVersion != CMSClient.SUPPORTED_API_VERSION)
+ if (! apiVersion.equals( CMSClient.SUPPORTED_API_VERSION ))
{
// oh no, the server api is older or newer than our client api.
// there is nothing we can do.
@@ -269,7 +270,7 @@ public class CMSRequest
}
cmsResponse.setApi(apiVersion);
- cmsResponse.setVersion(rootNode.getFirstChildByName("version").getValue());
+ cmsResponse.setVersion( new Version(rootNode.getFirstChildByName("version").getValue()));
List<String> errorList = new ArrayList<String>();
for (CMSNode errorNode : rootNode.getFirstChildByName("errors").getChildren())
diff --git a/src/de/openrat/client/util/CMSResponse.java b/src/de/openrat/client/util/CMSResponse.java
@@ -20,6 +20,8 @@ Boston, MA 02110-1301, USA.
*/
package de.openrat.client.util;
+import de.openrat.client.Version;
+
import java.util.List;
@@ -35,8 +37,8 @@ public class CMSResponse
private CMSSession session;
private int httpStatus;
- private int api;
- private String version;
+ private Version api;
+ private Version version;
/**
* Inhalt des Feldes <code>error</code>.
@@ -103,18 +105,18 @@ public class CMSResponse
*
* @return api
*/
- public int getApi()
+ public Version getApi()
{
return api;
}
/**
* Setzt das Feld <code>api</code>.
- *
+ *
* @param api
* api
*/
- public void setApi(int api)
+ public void setApi(Version api)
{
this.api = api;
}
@@ -124,18 +126,18 @@ public class CMSResponse
*
* @return version
*/
- public String getVersion()
+ public Version getVersion()
{
return version;
}
/**
* Setzt das Feld <code>version</code>.
- *
+ *
* @param version
* version
*/
- public void setVersion(String version)
+ public void setVersion(Version version)
{
this.version = version;
}