2016-01-01 23:35:54 +00:00
|
|
|
#include "JavaVersion.h"
|
2022-11-03 19:41:55 +00:00
|
|
|
|
|
|
|
#include "StringUtils.h"
|
2016-01-01 23:35:54 +00:00
|
|
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QString>
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
JavaVersion& JavaVersion::operator=(const QString& javaVersionString)
|
2016-01-01 23:35:54 +00:00
|
|
|
{
|
2016-06-16 01:20:23 +01:00
|
|
|
m_string = javaVersionString;
|
2016-01-01 23:35:54 +00:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
auto getCapturedInteger = [](const QRegularExpressionMatch& match, const QString& what) -> int {
|
2016-01-01 23:35:54 +00:00
|
|
|
auto str = match.captured(what);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (str.isEmpty()) {
|
2016-01-01 23:35:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return str.toInt();
|
|
|
|
};
|
|
|
|
|
|
|
|
QRegularExpression pattern;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (javaVersionString.startsWith("1.")) {
|
|
|
|
pattern = QRegularExpression("1[.](?<major>[0-9]+)([.](?<minor>[0-9]+))?(_(?<security>[0-9]+)?)?(-(?<prerelease>[a-zA-Z0-9]+))?");
|
|
|
|
} else {
|
2016-01-01 23:35:54 +00:00
|
|
|
pattern = QRegularExpression("(?<major>[0-9]+)([.](?<minor>[0-9]+))?([.](?<security>[0-9]+))?(-(?<prerelease>[a-zA-Z0-9]+))?");
|
|
|
|
}
|
|
|
|
|
2016-06-16 01:20:23 +01:00
|
|
|
auto match = pattern.match(m_string);
|
|
|
|
m_parseable = match.hasMatch();
|
|
|
|
m_major = getCapturedInteger(match, "major");
|
|
|
|
m_minor = getCapturedInteger(match, "minor");
|
|
|
|
m_security = getCapturedInteger(match, "security");
|
|
|
|
m_prerelease = match.captured("prerelease");
|
2016-01-01 23:35:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
JavaVersion::JavaVersion(const QString& rhs)
|
2016-01-01 23:35:54 +00:00
|
|
|
{
|
|
|
|
operator=(rhs);
|
|
|
|
}
|
|
|
|
|
2023-07-29 10:00:33 +01:00
|
|
|
QString JavaVersion::toString() const
|
2016-01-01 23:35:54 +00:00
|
|
|
{
|
2016-06-16 01:20:23 +01:00
|
|
|
return m_string;
|
2016-01-01 23:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JavaVersion::requiresPermGen()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_parseable) {
|
2016-06-16 01:20:23 +01:00
|
|
|
return m_major < 8;
|
2016-01-01 23:35:54 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool JavaVersion::operator<(const JavaVersion& rhs)
|
2016-01-01 23:35:54 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_parseable && rhs.m_parseable) {
|
2017-09-26 18:16:46 +01:00
|
|
|
auto major = m_major;
|
|
|
|
auto rmajor = rhs.m_major;
|
|
|
|
|
|
|
|
// HACK: discourage using java 9
|
2023-08-02 17:35:35 +01:00
|
|
|
if (major > 8)
|
2017-09-26 18:16:46 +01:00
|
|
|
major = -major;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (rmajor > 8)
|
2017-09-26 18:16:46 +01:00
|
|
|
rmajor = -rmajor;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (major < rmajor)
|
2016-01-01 23:35:54 +00:00
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (major > rmajor)
|
2016-01-01 23:35:54 +00:00
|
|
|
return false;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_minor < rhs.m_minor)
|
2016-01-01 23:35:54 +00:00
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_minor > rhs.m_minor)
|
2016-01-01 23:35:54 +00:00
|
|
|
return false;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_security < rhs.m_security)
|
2016-01-01 23:35:54 +00:00
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_security > rhs.m_security)
|
2016-01-01 23:35:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// everything else being equal, consider prerelease status
|
2016-06-16 01:20:23 +01:00
|
|
|
bool thisPre = !m_prerelease.isEmpty();
|
|
|
|
bool rhsPre = !rhs.m_prerelease.isEmpty();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (thisPre && !rhsPre) {
|
2016-01-01 23:35:54 +00:00
|
|
|
// this is a prerelease and the other one isn't -> lesser
|
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else if (!thisPre && rhsPre) {
|
2016-01-01 23:35:54 +00:00
|
|
|
// this isn't a prerelease and the other one is -> greater
|
|
|
|
return false;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else if (thisPre && rhsPre) {
|
2016-01-01 23:35:54 +00:00
|
|
|
// both are prereleases - use natural compare...
|
2022-11-03 19:41:55 +00:00
|
|
|
return StringUtils::naturalCompare(m_prerelease, rhs.m_prerelease, Qt::CaseSensitive) < 0;
|
2016-01-01 23:35:54 +00:00
|
|
|
}
|
|
|
|
// neither is prerelease, so they are the same -> this cannot be less than rhs
|
|
|
|
return false;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else
|
|
|
|
return StringUtils::naturalCompare(m_string, rhs.m_string, Qt::CaseSensitive) < 0;
|
2016-01-01 23:35:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool JavaVersion::operator==(const JavaVersion& rhs)
|
2016-01-01 23:35:54 +00:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_parseable && rhs.m_parseable) {
|
2016-06-16 01:20:23 +01:00
|
|
|
return m_major == rhs.m_major && m_minor == rhs.m_minor && m_security == rhs.m_security && m_prerelease == rhs.m_prerelease;
|
2016-01-01 23:35:54 +00:00
|
|
|
}
|
2016-06-16 01:20:23 +01:00
|
|
|
return m_string == rhs.m_string;
|
2016-01-01 23:35:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
bool JavaVersion::operator>(const JavaVersion& rhs)
|
2016-01-01 23:35:54 +00:00
|
|
|
{
|
|
|
|
return (!operator<(rhs)) && (!operator==(rhs));
|
|
|
|
}
|