Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[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 implements Comparable<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         /**
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.
95          * <p>
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.
100          */
101         @Override
102         public int compareTo(Version o)
103         {
104                 if (major != o.major)
105                 {
106                         if (major > o.major)
107                                 return 3;
108                         return -3;
109                 }
110                 if (minor != o.minor)
111                 {
112                         if (minor > o.minor)
113                                 return 2;
114                         return -2;
115                 }
116                 if (patch != o.patch)
117                 {
118                         if (patch > o.patch)
119                                 return 1;
120                         return -1;
121                 }
122                 return 0;
123         }
124
125         static class VersionJSONAdapter extends TypeAdapter<Version>
126         {
127                 @Override
128                 public void write(JsonWriter out, Version value) throws IOException
129                 {
130                         out.value(value.toSemverString());
131                 }
132
133                 @Override
134                 public Version read(JsonReader in) throws IOException
135                 {
136                         return parseSemver(in.nextString());
137                 }
138         }
139 }