summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/Version.java
blob: 8e6cbc97ff47783fae73d13768ded27e677e87e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.earth2me.essentials.update;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Version implements Comparable<Version>
{
	public enum Type
	{
		STABLE, PREVIEW, DEVELOPER
	}

	public int getMajor()
	{
		return major;
	}

	public int getMinor()
	{
		return minor;
	}

	public int getBuild()
	{
		return build;
	}

	public Type getType()
	{
		return type;
	}
	private final transient int major;
	private final transient int minor;
	private final transient int build;
	private final transient Type type;

	public Version(final String versionString)
	{
		final Matcher matcher = Pattern.compile("(Pre|Dev)?([0-9]+)[_\\.]([0-9]+)[_\\.]([0-9]+).*").matcher(versionString);
		if (!matcher.matches() || matcher.groupCount() < 4)
		{
			type = Type.DEVELOPER;
			major = 99;
			minor = build = 0;
			return;
		}
		if (versionString.startsWith("Pre"))
		{
			type = Type.PREVIEW;
		}
		else if (versionString.startsWith("Dev"))
		{
			type = Type.DEVELOPER;
		}
		else
		{
			type = Type.STABLE;
		}
		major = Integer.parseInt(matcher.group(2));
		minor = Integer.parseInt(matcher.group(3));
		build = Integer.parseInt(matcher.group(4));
	}

	@Override
	public int compareTo(final Version other)
	{
		int ret = 0;
		if (other.getType() == Type.DEVELOPER && getType() != Type.DEVELOPER)
		{
			ret = -1;
		}
		else if (getType() == Type.DEVELOPER && other.getType() != Type.DEVELOPER)
		{
			ret = 1;
		}
		else if (other.getMajor() > getMajor())
		{
			ret = -1;
		}
		else if (getMajor() > other.getMajor())
		{
			ret = 1;
		}
		else if (other.getMinor() > getMinor())
		{
			ret = -1;
		}
		else if (getMinor() > other.getMinor())
		{
			ret = 1;
		}
		else if (other.getBuild() > getBuild())
		{
			ret = -1;
		}
		else if (getBuild() > other.getBuild())
		{
			ret = 1;
		}
		else if (other.getType() == Type.STABLE && getType() == Type.PREVIEW)
		{
			ret = -1;
		}
		else if (getType() == Type.STABLE && other.getType() == Type.PREVIEW)
		{
			ret = 1;
		}
		return ret;
	}

	@Override
	public boolean equals(final Object obj)
	{
		if (obj == null)
		{
			return false;
		}
		if (getClass() != obj.getClass())
		{
			return false;
		}
		final Version other = (Version)obj;
		if (this.major != other.major)
		{
			return false;
		}
		if (this.minor != other.minor)
		{
			return false;
		}
		if (this.build != other.build)
		{
			return false;
		}
		if (this.type != other.type)
		{
			return false;
		}
		return true;
	}

	@Override
	public int hashCode()
	{
		int hash = 5;
		hash = 71 * hash + this.major;
		hash = 71 * hash + this.minor;
		hash = 71 * hash + this.build;
		hash = 71 * hash + (this.type != null ? this.type.hashCode() : 0);
		return hash;
	}

	@Override
	public String toString()
	{
		final StringBuilder builder = new StringBuilder();
		if (type == Type.DEVELOPER)
		{
			builder.append("Dev");
		}
		if (type == Type.PREVIEW)
		{
			builder.append("Pre");
		}
		builder.append(major);
		builder.append('.');
		builder.append(minor);
		builder.append('.');
		builder.append(build);
		return builder.toString();
	}
}