net.intelie.pipes.Version Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes-api Show documentation
Show all versions of pipes-api Show documentation
Intelie Pipes' API classes and interfaces
package net.intelie.pipes;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Version implements Comparable {
private static final Pattern pattern = Pattern.compile("^(\\d+)(?:\\.(\\d+)(?:\\.(\\d+))?)?(-SNAPSHOT)?$");
private final String value;
private final int major;
private final int minor;
private final int patch;
private final boolean release;
public Version(String value, int major, int minor, int patch, boolean release) {
new VersionHolder(); //HACK: useless, coverage
this.value = value;
this.major = major;
this.minor = minor;
this.patch = patch;
this.release = release;
}
public static Version fromString(String version) {
if (version == null) return null;
Matcher matcher = pattern.matcher(version);
if (!matcher.find()) return null;
int major = parseInt(matcher, 1);
int minor = parseInt(matcher, 2);
int patch = parseInt(matcher, 3);
boolean release = matcher.matches() && matcher.group(4) == null;
return new Version(version, major, minor, patch, release);
}
private static int parseInt(Matcher matcher, int group) {
if (!matcher.matches()) return 0;
String grp = matcher.group(group);
if (grp == null) return 0;
return Integer.parseInt(grp);
}
public static Version current() {
return VersionHolder.instance;
}
public static Version load(String path) {
return getVersion(path);
}
private static Version getVersion(String path) {
try (InputStream stream = Version.class.getResourceAsStream(path)) {
if (stream == null)
return null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
return fromString(reader.readLine());
}
} catch (Throwable e) {
return null;
}
}
public String value() {
return value;
}
public int major() {
return major;
}
public int minor() {
return minor;
}
public int patch() {
return patch;
}
public boolean release() {
return release;
}
public String simple() {
return String.format((Locale) null, "%d.%d", major, minor);
}
@Override
public String toString() {
return complete();
}
public String complete() {
return String.format((Locale) null, "%d.%d.%d%s", major, minor, patch, (release ? "" : "-SNAPSHOT"));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Version that = (Version) o;
return Objects.equals(this.major, that.major) &&
Objects.equals(this.minor, that.minor) &&
Objects.equals(this.patch, that.patch) &&
Objects.equals(this.release, that.release);
}
@Override
public int hashCode() {
return Objects.hash(major, minor, patch, release);
}
@Override
public int compareTo(Version that) {
int v;
if ((v = Integer.compare(this.major, that.major)) != 0) return v;
if ((v = Integer.compare(this.minor, that.minor)) != 0) return v;
if ((v = Integer.compare(this.patch, that.patch)) != 0) return v;
if ((v = Boolean.compare(this.release, that.release)) != 0) return v;
return 0;
}
private static class VersionHolder {
public static final Version instance = load("/PIPES_VERSION");
}
}