1 package net.mograsim.logic.model.util;
3 import java.io.IOException;
5 import com.google.gson.TypeAdapter;
6 import com.google.gson.annotations.JsonAdapter;
7 import com.google.gson.stream.JsonReader;
8 import com.google.gson.stream.JsonWriter;
10 import net.mograsim.logic.model.util.Version.VersionJSONAdapter;
12 @JsonAdapter(VersionJSONAdapter.class)
13 public final class Version implements Comparable<Version>
15 public final int major, minor, patch;
17 public Version(int major, int minor, int patch)
25 public int[] getVersionNumbers()
27 return new int[] { major, minor, patch };
31 public String toString()
33 return toSemverString();
36 public String toSemverString()
38 return major + "." + minor + "." + patch;
41 public static Version parseSemver(String semver)
43 String[] semverParts = semver.split("\\.");
44 return new Version(Integer.parseInt(semverParts[0]), semverParts.length > 1 ? Integer.parseInt(semverParts[1]) : 0,
45 semverParts.length > 2 ? Integer.parseInt(semverParts[2]) : 0);
53 result = prime * result + major;
54 result = prime * result + minor;
55 result = prime * result + patch;
60 public boolean equals(Object obj)
64 if (!(obj instanceof Version))
66 Version other = (Version) obj;
67 if (major != other.major)
69 if (minor != other.minor)
71 if (patch != other.patch)
76 public boolean is(int major)
78 return major == this.major;
81 public boolean is(int major, int minor)
83 return is(major) && this.minor == minor;
86 public boolean is(int major, int minor, int patch)
88 return is(major, minor) && this.patch == patch;
92 * Compares this {@link Version} with the specified version.<br>
93 * As required by {@link Comparable#compareTo(Object)}, returns a negative integer, zero, or a positive integer as this version is less
94 * (earlier) than, equal to, or greater (later) than the specified version.
96 * If the versions are equal ({@link #major}, {@link #minor}, {@link #patch} are the same), returns 0. <br>
97 * If they differ in {@link #patch}, but neither {@link #major} or {@link #minor} , returns +-1. <br>
98 * If they differ in {@link #minor}, but not {@link #major}, returns +-2.<br>
99 * If they differ in {@link #major}, returns +-3.
102 public int compareTo(Version o)
104 if (major != o.major)
110 if (minor != o.minor)
116 if (patch != o.patch)
125 static class VersionJSONAdapter extends TypeAdapter<Version>
128 public void write(JsonWriter out, Version value) throws IOException
130 out.value(value.toSemverString());
134 public Version read(JsonReader in) throws IOException
136 return parseSemver(in.nextString());