Restructured JSON (de)serializing: ViewModels can be (de)serialized too
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / util / Version.java
1 package net.mograsim.logic.model.util;
2
3 import java.io.IOException;
4
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;
9
10 import net.mograsim.logic.model.util.Version.VersionJSONAdapter;
11
12 @JsonAdapter(VersionJSONAdapter.class)
13 public final class Version
14 {
15         public final int major, minor, patch;
16
17         public Version(int major, int minor, int patch)
18         {
19                 super();
20                 this.major = major;
21                 this.minor = minor;
22                 this.patch = patch;
23         }
24
25         public int[] getVersionNumbers()
26         {
27                 return new int[] { major, minor, patch };
28         }
29
30         @Override
31         public String toString()
32         {
33                 return toSemverString();
34         }
35
36         public String toSemverString()
37         {
38                 return major + "." + minor + "." + patch;
39         }
40
41         public static Version parseSemver(String semver)
42         {
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);
46         }
47
48         @Override
49         public int hashCode()
50         {
51                 final int prime = 31;
52                 int result = 1;
53                 result = prime * result + major;
54                 result = prime * result + minor;
55                 result = prime * result + patch;
56                 return result;
57         }
58
59         @Override
60         public boolean equals(Object obj)
61         {
62                 if (this == obj)
63                         return true;
64                 if (!(obj instanceof Version))
65                         return false;
66                 Version other = (Version) obj;
67                 if (major != other.major)
68                         return false;
69                 if (minor != other.minor)
70                         return false;
71                 if (patch != other.patch)
72                         return false;
73                 return true;
74         }
75
76         public boolean is(int major)
77         {
78                 return major == this.major;
79         }
80
81         public boolean is(int major, int minor)
82         {
83                 return is(major) && this.minor == minor;
84         }
85
86         public boolean is(int major, int minor, int patch)
87         {
88                 return is(major, minor) && this.patch == patch;
89         }
90
91         static class VersionJSONAdapter extends TypeAdapter<Version>
92         {
93                 @Override
94                 public void write(JsonWriter out, Version value) throws IOException
95                 {
96                         out.value(value.toSemverString());
97                 }
98
99                 @Override
100                 public Version read(JsonReader in) throws IOException
101                 {
102                         return parseSemver(in.nextString());
103                 }
104         }
105 }