summaryrefslogtreecommitdiffstats
path: root/api/logic/java/JavaVersion.cpp
blob: 84fc48a48171b19b3f00b1fedcddfdbefb826c52 (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
#include "JavaVersion.h"
#include <MMCStrings.h>

#include <QRegularExpression>
#include <QString>

JavaVersion & JavaVersion::operator=(const QString & javaVersionString)
{
	string = javaVersionString;

	auto getCapturedInteger = [](const QRegularExpressionMatch & match, const QString &what) -> int
	{
		auto str = match.captured(what);
		if(str.isEmpty())
		{
			return 0;
		}
		return str.toInt();
	};

	QRegularExpression pattern;
	if(javaVersionString.startsWith("1."))
	{
		pattern = QRegularExpression ("1[.](?<major>[0-9]+)([.](?<minor>[0-9]+))?(_(?<security>[0-9]+)?)?(-(?<prerelease>[a-zA-Z0-9]+))?");
	}
	else
	{
		pattern = QRegularExpression("(?<major>[0-9]+)([.](?<minor>[0-9]+))?([.](?<security>[0-9]+))?(-(?<prerelease>[a-zA-Z0-9]+))?");
	}

	auto match = pattern.match(string);
	parseable = match.hasMatch();
	major = getCapturedInteger(match, "major");
	minor = getCapturedInteger(match, "minor");
	security = getCapturedInteger(match, "security");
	prerelease = match.captured("prerelease");
	return *this;
}

JavaVersion::JavaVersion(const QString &rhs)
{
	operator=(rhs);
}

QString JavaVersion::toString()
{
	return string;
}

bool JavaVersion::requiresPermGen()
{
	if(parseable)
	{
		return major < 8;
	}
	return true;
}

bool JavaVersion::operator<(const JavaVersion &rhs)
{
	if(parseable && rhs.parseable)
	{
		if(major < rhs.major)
			return true;
		if(major > rhs.major)
			return false;
		if(minor < rhs.minor)
			return true;
		if(minor > rhs.minor)
			return false;
		if(security < rhs.security)
			return true;
		if(security > rhs.security)
			return false;

		// everything else being equal, consider prerelease status
		bool thisPre = !prerelease.isEmpty();
		bool rhsPre = !rhs.prerelease.isEmpty();
		if(thisPre && !rhsPre)
		{
			// this is a prerelease and the other one isn't -> lesser
			return true;
		}
		else if(!thisPre && rhsPre)
		{
			// this isn't a prerelease and the other one is -> greater
			return false;
		}
		else if(thisPre && rhsPre)
		{
			// both are prereleases - use natural compare...
			return Strings::naturalCompare(prerelease, rhs.prerelease, Qt::CaseSensitive) < 0;
		}
		// neither is prerelease, so they are the same -> this cannot be less than rhs
		return false;
	}
	else return Strings::naturalCompare(string, rhs.string, Qt::CaseSensitive) < 0;
}

bool JavaVersion::operator==(const JavaVersion &rhs)
{
	if(parseable && rhs.parseable)
	{
		return major == rhs.major && minor == rhs.minor && security == rhs.security && prerelease == rhs.prerelease;
	}
	return string == rhs.string;
}

bool JavaVersion::operator>(const JavaVersion &rhs)
{
	return (!operator<(rhs)) && (!operator==(rhs));
}