blob: a6520c8bdbb3bd409bbd383dc803851085340c31 [file] [log] [blame]
package org.eclipse.epp.installer.core.eclipse;
import org.eclipse.epp.installer.core.product.IProductVersion;
import org.eclipse.epp.installer.internal.core.eclipse.EclipseUtils;
import org.eclipse.epp.installer.internal.core.eclipse.EclipseVersionRange;
/**
* An object that identifies the version of a particular Eclipse installation.
* This is used by both licensing and the installer
* and so must not import any Eclipse specific classes.
* <p>
* Copyright (c) 2003, Instantiations, Inc.<br>
* All Rights Reserved
*
* @author Dan Rubel
* @version $Revision: 1.2 $
*/
public class EclipseVersion implements IProductVersion
{
/**
* Constant representing an unknown version of Eclipse
*/
public static final EclipseVersion UNKNOWN = new EclipseVersion(0, 0, 0, null);
/**
* Constant string representing unknown version of Eclipse
*/
public static final String UNKNOWN_VERSION_TEXT = "???";
/**
* Symbol used as token in version string
*/
public static final String VERSION_TOKEN_SYMBOL = "\\.";
/** major version number... zero = unknown version */
private final int vMajor;
/** minor version number */
private final int vMinor;
/** service version number */
private final int vService;
/** Build ID, Milestone, or null if unknown */
private final String vBuild;
/**
* Construct a new instance representing a particular version of Eclipse
* @param vMajor major version number
* @param vMinor minor version number
* @param vService service version number
* @param vBuild the build ID or null if unknown
*/
public EclipseVersion(int vMajor, int vMinor, int vService, String vBuild) {
this.vMajor = vMajor;
this.vMinor = vMinor;
this.vService = vService;
this.vBuild = EclipseUtils.getMilestone(vBuild);
}
EclipseVersion(String versionString[]) {
if (versionString == null || versionString.length < 1) {
throw new IllegalArgumentException();
}
this.vMajor = Integer.parseInt(versionString[0]);
if (versionString.length > 1) {
this.vMinor = Integer.parseInt(versionString[1]);
} else {
this.vMinor = 0;
}
if (versionString.length > 2) {
this.vService = Integer.parseInt(versionString[2]);
} else {
this.vService = 0;
}
if (versionString.length > 3) {
this.vBuild = EclipseUtils.getMilestone(versionString[3]);
} else {
this.vBuild = EclipseUtils.getMilestone("");
}
}
public int getMajor() {
return vMajor;
}
public int getMinor() {
return vMinor;
}
public int getService() {
return vService;
}
public String getBuild() {
return vBuild;
}
public int hashCode() {
return vMajor * 10000 + vMinor * 1000 + vService * 100 + (vBuild == null ? 0 : vBuild.hashCode() % 100);
}
public boolean equals(Object o) {
if(o instanceof EclipseVersion) {
EclipseVersion ev = (EclipseVersion)o;
return (hashCode() == ev.hashCode());
}
return false;
}
/**
* Returns a string representation of the object.
*/
public String toString() {
if (vMajor == 0)
return UNKNOWN_VERSION_TEXT;
StringBuffer buf = new StringBuffer(10);
buf.append(vMajor);
buf.append(".");
buf.append(vMinor);
buf.append(".");
buf.append(vService);
// Append the build identifier or milestone if the service level is 0
// (e.g. display all 3.0.1 builds identically)
if (vService == 0 && vBuild != null && vBuild.length() > 0) {
buf.append(" ");
buf.append(vBuild);
}
return buf.toString();
}
/**
* Create an EclipseVersion object for given version string
*
* @param versionString
* @return EclipseVersion object for given version string
*/
public static EclipseVersion fromString(String versionString) {
String[] numbers = versionString.split(VERSION_TOKEN_SYMBOL);
if (numbers == null || numbers.length == 0) {
return null;
}
EclipseVersion version = new EclipseVersion(numbers);
return version;
}
/**
* Checks if EclipseVersion is in specified version range
* @param rangeString
* string Range representation
* @return boolean
* <code>true</code> if is in range, else <code>false</code>
*/
public boolean isInRange(String rangeString) {
EclipseVersionRange range = EclipseVersionRange.parseRangeString(rangeString);
if (range != null) {
int floorComp = compareTo(range.floor);
int ceilingComp = compareTo(range.ceiling);
if (range.floorIncluded) {
if (floorComp < 0) {
return false;
}
} else {
if (floorComp <= 0) {
return false;
}
}
if (range.ceilingIncluded) {
if (ceilingComp > 0) {
return false;
}
} else {
if (ceilingComp >= 0) {
return false;
}
}
return true;
} else {
EclipseVersion version = fromString(rangeString);
if (compareTo(version) > 0)
return true;
return false;
}
}
private int compareInt(int intValue1, int intValue2) {
if (intValue1 != intValue2) {
return intValue1 < intValue2 ? -1 : 1;
}
return 0;
}
/**
* Compare with given EclipseVersion object
* @param version
* @return int
* 0 if versions are equal, -1 if given version is greater and 1 else
*/
public int compareTo(IProductVersion version) {
int majorComp = compareInt(getMajor(), version.getMajor());
int minorComp = compareInt(getMinor(), version.getMinor());
int serviceComp = compareInt(getService(), version.getService());
if (majorComp != 0) {
return majorComp;
}
if (minorComp != 0) {
return minorComp;
}
if (serviceComp != 0) {
return serviceComp;
}
if (vBuild == null || vBuild.equals("")) {
return (version.getBuild() == null || version.getBuild().equals("")) ? 0 : -1;
}
if (version.getBuild() == null || version.getBuild().equals("")) {
return 1;
}
return vBuild.compareTo(version.getBuild());
}
/**
* Determine if the receiver is compatible with the specified version.
* This is used to determine if a message should be displayed
* warning the user that the product is designed for a different version of Eclipse
*/
public boolean isCompatible(EclipseVersion version) {
// if (vMajor == 3 && version.vMajor == 3 && vMinor == 1 && version.vMinor >= 1)
// return true;
if (vMajor != version.vMajor || vMinor != version.vMinor)
return false;
// Treat all service builds as compatible with the minor version GA (e.g. 3.0.1 = 3.0.0 GA)
if (vService > 0 || vBuild == null)
return version.vService > 0 || version.vBuild == null;
return vService == version.vService
&& (vBuild == null ? version.vBuild == null : vBuild.equals(version.vBuild));
}
}