X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.model%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fmodel%2Futil%2FVersion.java;h=00aeed89bed13ba29783fca6e8182395a2bcfa4a;hb=8bed58cd47f4e53a0a83e066d38864aa6875502f;hp=b0a729827806dd45ec5b9cf30644c42a1de54627;hpb=47ea68ed5c444dd14864412639f6a6fd60ab8a0f;p=Mograsim.git diff --git a/net.mograsim.logic.model/src/net/mograsim/logic/model/util/Version.java b/net.mograsim.logic.model/src/net/mograsim/logic/model/util/Version.java index b0a72982..00aeed89 100644 --- a/net.mograsim.logic.model/src/net/mograsim/logic/model/util/Version.java +++ b/net.mograsim.logic.model/src/net/mograsim/logic/model/util/Version.java @@ -1,8 +1,17 @@ package net.mograsim.logic.model.util; -public final class Version +import java.io.IOException; + +import com.google.gson.TypeAdapter; +import com.google.gson.annotations.JsonAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import net.mograsim.logic.model.util.Version.VersionJSONAdapter; + +@JsonAdapter(VersionJSONAdapter.class) +public final class Version implements Comparable { - public final static Version jsonCompVersion = new Version(0, 1, 3); public final int major, minor, patch; public Version(int major, int minor, int patch) @@ -20,10 +29,22 @@ public final class Version @Override public String toString() + { + return toSemverString(); + } + + public String toSemverString() { return major + "." + minor + "." + patch; } + public static Version parseSemver(String semver) + { + String[] semverParts = semver.split("\\."); + return new Version(Integer.parseInt(semverParts[0]), semverParts.length > 1 ? Integer.parseInt(semverParts[1]) : 0, + semverParts.length > 2 ? Integer.parseInt(semverParts[2]) : 0); + } + @Override public int hashCode() { @@ -54,7 +75,7 @@ public final class Version public boolean is(int major) { - return major != this.major; + return major == this.major; } public boolean is(int major, int minor) @@ -66,4 +87,53 @@ public final class Version { return is(major, minor) && this.patch == patch; } + + /** + * Compares this {@link Version} with the specified version.
+ * As required by {@link Comparable#compareTo(Object)}, returns a negative integer, zero, or a positive integer as this version is less + * (earlier) than, equal to, or greater (later) than the specified version. + *

+ * If the versions are equal ({@link #major}, {@link #minor}, {@link #patch} are the same), returns 0.
+ * If they differ in {@link #patch}, but neither {@link #major} or {@link #minor} , returns +-1.
+ * If they differ in {@link #minor}, but not {@link #major}, returns +-2.
+ * If they differ in {@link #major}, returns +-3. + */ + @Override + public int compareTo(Version o) + { + if (major != o.major) + { + if (major > o.major) + return 3; + return -3; + } + if (minor != o.minor) + { + if (minor > o.minor) + return 2; + return -2; + } + if (patch != o.patch) + { + if (patch > o.patch) + return 1; + return -1; + } + return 0; + } + + static class VersionJSONAdapter extends TypeAdapter + { + @Override + public void write(JsonWriter out, Version value) throws IOException + { + out.value(value.toSemverString()); + } + + @Override + public Version read(JsonReader in) throws IOException + { + return parseSemver(in.nextString()); + } + } } \ No newline at end of file