All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ibm.staf.STAFVersion Maven / Gradle / Ivy

Go to download

The Software Testing Automation Framework (STAF) is an open source, multi-platform, multi-language framework designed around the idea of reusable components, called services (such as process invocation, resource management, logging, and monitoring). STAF removes the tedium of building an automation infrastructure, thus enabling you to focus on building your automation solution. The STAF framework provides the foundation upon which to build higher level solutions, and provides a pluggable approach supported across a large variety of platforms and languages. This component contains the STAF Java API (http://staf.sourceforge.net/current/STAFJava.htm ) only. For more information see http://staf.sourceforge.net/ .

The newest version!
/*****************************************************************************/
/* Software Testing Automation Framework (STAF)                              */
/* (C) Copyright IBM Corp. 2005                                              */
/*                                                                           */
/* This software is licensed under the Eclipse Public License (EPL) V1.0.    */
/*****************************************************************************/

package com.ibm.staf;

public class STAFVersion
{
    private String fVersion;
    private int[]  fVersionArray = { 0, 0, 0, 0 };
    private String fText = new String("");

    public static int NUM_VERSION_LEVELS = 4;

    /*
       A STAF version must be of the following format unless it's blank or
       "", which equates to "no version" and is internally represented
       as 0.0.0.0
        
           a[.b[.c[.d]]] [text]
    
       where:
       - a, b, c, and d (if specified) are numeric
       - text is separated by one or more spaces from the version numbers
       
       Otherwise, a STAXException is thrown. 
    */   

    public STAFVersion(String version) throws NumberFormatException
    {
        fVersion = version;

        String versionStr = new String("");

        // Verify that the version is valid

        if (version.equals("") || version.equals(""))
        {
            // Do nothing
            return;
        }
        else
        {
            // Separate any text from the numeric version in version1

            int spaceIndex = fVersion.indexOf(" ");

            if (spaceIndex != -1)
            {
                versionStr = fVersion.substring(0, spaceIndex);
                fText = fVersion.substring(spaceIndex + 1).trim();
            }
            else
            {
                versionStr = fVersion;
            }
        }

        // Assign the versionArray values from the dot-separated numeric
        // values in versionStr.  If .b or .c or .d do not exist, then
        // .0 is substituted such that 2.5 == 2.5.0.0

        int dotIndex = -1;

        for (int i = 0; i < NUM_VERSION_LEVELS; i++)
        {
            dotIndex = versionStr.indexOf(".");

            if (dotIndex == -1)
            {
                if (!versionStr.equals(""))
                {
                    fVersionArray[i] = (new Integer(versionStr)).intValue();
                }

                break;
            }
            else
            {
                fVersionArray[i] = (new Integer(
                    versionStr.substring(0, dotIndex))).intValue();

                if (dotIndex < (versionStr.length() - 1))
                    versionStr = versionStr.substring(dotIndex + 1);
                else
                    versionStr = "";
            }
        }
    }

    public String getVersion()
    {
        return fVersion;
    }

    public int[] getVersionArray()
    {
        return fVersionArray;
    }

    public String getText()
    {
        return fText;
    }

    /*
       Compares this STAFVersion object to the given STAFVersion object.
       
       - If fVersion is equal to another version's fVersion, returns 0.
       - Each entry in fVersionArray is numerically compared with each
         corresponding entry in another version's fVersionArray.
       - If its fVersionArray is "equal" to another version's fVersionArray,
         then do:
         1) If fText is "" but another version's fText is not "", then return
            1 to indicate that this version is greater than another version.
         2) If fText is not "" but another version's fText is "", then return
            -1 to indicate that this version is less than another version.
         3) Return the result of a case-insensitive string compare of fText
            with another versions fText. 
       
       Note:  No version, e.g. blank or  has a fVersionVector set to
              {0,0,0,0} so that no version is less than any valid version.
   
       Parameters:
       
       version - the STAFVersion object to be compared to this STAFVersion
                 object.
       
       Returns:
       
       - The value 0 if the two STAFVersion objects are equal
       - A value less than 0 if this STAFVersion object is less than the
         given argument
       - A value greater than 0 if this STAFVersion object is greater than
         the given argument.
    */   

    public int compareTo(STAFVersion version)
    {
        // Check if the two versions are equal and, if so, return 0.

        if (fVersion.equals(version.getVersion()))
            return 0;
        
        // Compare numeric versions stored in the fVersionArray

        int[] versionArray = version.getVersionArray();

        for (int i = 0; i < NUM_VERSION_LEVELS; i++)
        {
            if (fVersionArray[i] < versionArray[i])
                return -1;
            else if (fVersionArray[i] > versionArray[i])
                return 1;
        }

        // Versions are equal so compare text

        if (fText.equals("") && !version.getText().equals(""))
            return 1;  
        else if (!fText.equals("") && version.getText().equals(""))
            return -1;
        else
            return fText.compareToIgnoreCase(version.getText());
    }

    public String toString()
    {
        return fVersion;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy