commit: af344112881d635949535ba6a5fc9e11ad284dc5 Author: Miroslav Šulc <fordfrog <AT> gentoo <DOT> org> AuthorDate: Sun Nov 3 11:26:07 2019 +0000 Commit: Miroslav Šulc <fordfrog <AT> gentoo <DOT> org> CommitDate: Sun Nov 3 11:26:07 2019 +0000 URL: https://gitweb.gentoo.org/proj/java-ebuilder.git/commit/?id=af344112
encapsulated java version in new container Signed-off-by: Miroslav Šulc <fordfrog <AT> gentoo.org> .../gentoo/java/ebuilder/maven/JavaVersion.java | 87 ++++++++++++++++++++++ .../gentoo/java/ebuilder/maven/MavenParser.java | 10 ++- .../gentoo/java/ebuilder/maven/MavenProject.java | 12 +-- 3 files changed, 99 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/JavaVersion.java b/src/main/java/org/gentoo/java/ebuilder/maven/JavaVersion.java new file mode 100644 index 0000000..5aa600d --- /dev/null +++ b/src/main/java/org/gentoo/java/ebuilder/maven/JavaVersion.java @@ -0,0 +1,87 @@ +package org.gentoo.java.ebuilder.maven; + +import java.text.MessageFormat; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Java versionNumber container. + * + * @author fordfrog + */ +public class JavaVersion { + + /** + * Pattern for parsing Java versionNumber. + */ + private static final Pattern PATTERN_VERSION + = Pattern.compile("^(?:1\\.)(\\d+)$"); + /** + * Java versionNumber number. + */ + private final int versionNumber; + /** + * Java version string. + */ + private final String versionString; + + /** + * Creates new instance of JavaVersion. Parses the version string and uses + * the main Java version. Terminates if the version is not valid. + * + * @param versionString version string + */ + public JavaVersion(final String versionString) { + this.versionString = versionString; + + final Matcher matcher = PATTERN_VERSION.matcher(versionString); + + if (!matcher.matches()) { + throw new IllegalArgumentException(MessageFormat.format( + "Java version \"{0}\" is not valid!", versionString)); + } + + versionNumber = Integer.parseInt(matcher.group(1), 10); + } + + /** + * Compares this JavaVersion to the passed JavaVersion. + * + * @param javaVersion JavaVersion to compare to. + * + * @return -1 if this versionNumber is lower, 1 if this versionNumber is + * higher and 0 if both are equal + */ + public int compareTo(final JavaVersion javaVersion) { + if (javaVersion.getVersionNumber() > versionNumber) { + return -1; + } else if (javaVersion.getVersionNumber() < versionNumber) { + return 1; + } else { + return 0; + } + } + + /** + * Getter for {@link #versionNumber}. + * + * @return {@link #versionNumber} + */ + public int getVersionNumber() { + return versionNumber; + } + + /** + * Getter for {@link #versionString}. + * + * @return {@link #versionString} + */ + public String getVersionString() { + return versionString; + } + + @Override + public String toString() { + return versionString; + } +} diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java index 942ee27..7c9d162 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenParser.java @@ -208,7 +208,7 @@ public class MavenParser { case "source": if ("maven-compiler-plugin".equals(artifactId)) { mavenProject.setSourceVersion( - reader.getElementText()); + new JavaVersion(reader.getElementText())); } else { consumeElement(reader); } @@ -217,7 +217,7 @@ public class MavenParser { case "target": if ("maven-compiler-plugin".equals(artifactId)) { mavenProject.setTargetVersion( - reader.getElementText()); + new JavaVersion(reader.getElementText())); } else { consumeElement(reader); } @@ -572,10 +572,12 @@ public class MavenParser { if (reader.isStartElement()) { switch (reader.getLocalName()) { case "maven.compiler.source": - mavenProject.setSourceVersion(reader.getElementText()); + mavenProject.setSourceVersion( + new JavaVersion(reader.getElementText())); break; case "maven.compiler.target": - mavenProject.setTargetVersion(reader.getElementText()); + mavenProject.setTargetVersion( + new JavaVersion(reader.getElementText())); break; case "project.build.sourceEncoding": mavenProject.setSourceEncoding(reader.getElementText()); diff --git a/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java b/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java index 6cead37..07fa60e 100644 --- a/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java +++ b/src/main/java/org/gentoo/java/ebuilder/maven/MavenProject.java @@ -68,11 +68,11 @@ public class MavenProject { /** * Source compile version. */ - private String sourceVersion = "1.8"; + private JavaVersion sourceVersion = new JavaVersion("1.8"); /** * Target compile version. */ - private String targetVersion = "1.8"; + private JavaVersion targetVersion = new JavaVersion("1.8"); /** * Test resource directories. */ @@ -382,7 +382,7 @@ public class MavenProject { * * @return {@link #sourceVersion} */ - public String getSourceVersion() { + public JavaVersion getSourceVersion() { return sourceVersion; } @@ -391,7 +391,7 @@ public class MavenProject { * * @param sourceVersion {@link #sourceVersion} */ - public void setSourceVersion(final String sourceVersion) { + public void setSourceVersion(final JavaVersion sourceVersion) { this.sourceVersion = sourceVersion; } @@ -400,7 +400,7 @@ public class MavenProject { * * @return {@link #targetVersion} */ - public String getTargetVersion() { + public JavaVersion getTargetVersion() { return targetVersion; } @@ -409,7 +409,7 @@ public class MavenProject { * * @param targetVersion {@link #targetVersion} */ - public void setTargetVersion(final String targetVersion) { + public void setTargetVersion(final JavaVersion targetVersion) { this.targetVersion = targetVersion; }