140 lines
3.1 KiB
Java
140 lines
3.1 KiB
Java
package org.gcube.informationsystem.utils;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import org.gcube.com.fasterxml.jackson.annotation.JsonIgnore;
|
|
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
|
|
|
|
public class TypeVersion implements Comparable<TypeVersion> {
|
|
|
|
public final static String TYPE_VERSION_REGEX = PropertyDefinitionImpl.TYPE_VERSION_REGEX;
|
|
|
|
public final static Pattern TYPE_VERSION_PATTERN;
|
|
|
|
public static final String MINIMAL_VERSION_STRING = "1.0.0";
|
|
public static final TypeVersion MINIMAL_VERSION;
|
|
|
|
static {
|
|
TYPE_VERSION_PATTERN = Pattern.compile(TYPE_VERSION_REGEX);
|
|
MINIMAL_VERSION = new TypeVersion(MINIMAL_VERSION_STRING);
|
|
}
|
|
|
|
@JsonIgnore
|
|
protected int major;
|
|
@JsonIgnore
|
|
protected int minor;
|
|
@JsonIgnore
|
|
protected int revision;
|
|
|
|
protected TypeVersion(){}
|
|
|
|
public TypeVersion(String version) {
|
|
setVersion(version);
|
|
}
|
|
|
|
public TypeVersion(int major, int minor, int revision) {
|
|
this.major = major;
|
|
this.minor = minor;
|
|
this.revision = revision;
|
|
check();
|
|
}
|
|
|
|
public void setVersion(String version) {
|
|
if(!TYPE_VERSION_PATTERN.matcher(version).matches()) {
|
|
throw new RuntimeException("The provided version (i.e. " + version + ") MUST respect the regex " + TYPE_VERSION_REGEX);
|
|
}
|
|
String[] parts = version.split("\\.");
|
|
this.major = Integer.valueOf(parts[0]);
|
|
this.minor = Integer.valueOf(parts[1]);
|
|
this.revision = Integer.valueOf(parts[2]);
|
|
check();
|
|
}
|
|
|
|
/*
|
|
* The REGEX does not allow a 0.X.X Version.
|
|
* Anyway I added this check in case we decide to change the minimal version and the
|
|
* regex.
|
|
*/
|
|
protected void check() {
|
|
if(this.compareTo(MINIMAL_VERSION)<0) {
|
|
throw new RuntimeException("Minimal Allowed version is " + MINIMAL_VERSION_STRING);
|
|
}
|
|
}
|
|
|
|
public int getMajor() {
|
|
return major;
|
|
}
|
|
|
|
protected void setMajor(int major) {
|
|
this.major = major;
|
|
}
|
|
|
|
public int getMinor() {
|
|
return minor;
|
|
}
|
|
|
|
protected void setMinor(int minor) {
|
|
this.minor = minor;
|
|
}
|
|
|
|
public int getRevision() {
|
|
return revision;
|
|
}
|
|
|
|
protected void setRevision(int revision) {
|
|
this.revision = revision;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return major + "." + minor + "." + revision;
|
|
}
|
|
@Override
|
|
public int compareTo(TypeVersion other) {
|
|
if(other == null) {
|
|
return 1;
|
|
}
|
|
|
|
int compare = Integer.compare(major, other.major);
|
|
if(compare!=0) {
|
|
return compare;
|
|
}
|
|
|
|
compare = Integer.compare(minor, other.minor);
|
|
if(compare!=0) {
|
|
return compare;
|
|
}
|
|
|
|
compare = Integer.compare(revision, other.revision);
|
|
return compare;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
final int prime = 31;
|
|
int result = 1;
|
|
result = prime * result + major;
|
|
result = prime * result + minor;
|
|
result = prime * result + revision;
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if (this == obj)
|
|
return true;
|
|
if (obj == null)
|
|
return false;
|
|
if (getClass() != obj.getClass())
|
|
return false;
|
|
TypeVersion other = (TypeVersion) obj;
|
|
if (major != other.major)
|
|
return false;
|
|
if (minor != other.minor)
|
|
return false;
|
|
if (revision != other.revision)
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
} |