ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Conor MacNeill" <co...@cortexebusiness.com.au>
Subject RE: [PATCH] Available.java - to detect JDK versions
Date Mon, 24 Jul 2000 04:20:35 GMT
Tim,

Some quick feedback. I don't like the use of doubles. Wouldn't it be better
to compare minor and major version numbers separately as integers and
properly parse the version numbers supplied.

Have you considered how versions such as 10.1 and 1.10 will come out. From
my quick look (in other words, I might be wrong) these will be treated as
equal to 5.1 and 1.5 resp. Also, what about if someone mistypes a version as
1..1?

Conor

--
Conor MacNeill
conor@cortexebusiness.com.au
Cortex eBusiness
http://www.cortexebusiness.com.au

> -----Original Message-----
> From: Tim Fennell [mailto:tfenne@rcn.com]
> Sent: Monday, 24 July 2000 13:44
> To: ant-dev@jakarta.apache.org
> Subject: [PATCH] Available.java - to detect JDK versions
>
>
> Here is the patch. It supports the syntax suggested:
> 	1.2.2 - matches specified version
> 	1.2+  - matches 1.2 or higher
> 	1.2-	- matches 1.2 or lower
>
> Note both the +/- version are *inclusive*, so check for 1.99- if
> you want pre
> 1.2 etc!
>
> -t
>
> ---------------------------Begin Patch----------------------------
> Index: Available.java
> ===================================================================
> RCS file:
> /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs
> /Available.jav
> a,v
> retrieving revision 1.6
> diff -u -r1.6 Available.java
> --- Available.java	2000/07/06 16:48:13	1.6
> +++ Available.java	2000/07/24 03:43:23
> @@ -62,6 +62,7 @@
>   * Will set the given property if the requested resource is available at
> runtime.
>   *
>   * @author Stefano Mazzocchi <a
> href="mailto:stefano@apache.org">stefano@apache.org</a>
> + * @author Tim Fennell <a
> href="mailto:tfennell@sapient.com">tfennell@sapient.com</a>
>   */
>
>  public class Available extends Task {
> @@ -70,6 +71,7 @@
>      private String classname;
>      private String file;
>      private String resource;
> +    private String jdk;
>
>      public void setProperty(String property) {
>          this.property = property;
> @@ -94,10 +96,16 @@
>          this.resource = resource;
>      }
>
> +    /** Setter method to set the value of the JDK to check for. */
> +    public void setJdk(String jdk) {
> +        this.jdk = jdk;
> +    }
> +
>      public void init() throws BuildException {
>          if ((classname != null) && !checkClass(classname)) return;
>          if ((file != null) && !checkFile(file)) return;
>          if ((resource != null) && !checkResource(resource)) return;
> +        if ((jdk != null) && !checkJdk(jdk)) return;
>
>          this.project.setProperty(property, "true");
>      }
> @@ -125,4 +133,75 @@
>              return false;
>          }
>      }
> +
> +    /**
> +     * Checks to see if the current JDK version (the one this
> method is called
> +     * within) matches a criteria. Either that it is 'equal',
> 'equal or higher'
> +     * or 'equal of lower'.
> +     * @param jdk version of the JDK to check for. Terminate
> with a '+' to
> check
> +     *        for a version or higher. Terminate with a '-' to
> check for a
> +     *         version or lower.
> +     * @return boolean true if the current JDK matches the criteria.
> +     */
> +    private boolean checkJdk(String jdk) {
> +        double checkVersion = 0.0;
> +        double currentVersion = 0.0;
> +        int    returnCheck  = 0;
> +        int    returnValue  = 0;
> +
> +        // Determine if we are looking for an exact match,
> greater than or less
> +        // than... And convert the jdk string to a double.
> +        if (jdk.endsWith("-")) {
> +            returnCheck = -1;
> +            checkVersion = stringToDouble(jdk.substring(0,
> jdk.length() -1));
> +        }
> +        else if (jdk.endsWith("+")) {
> +            returnCheck = +1;
> +            checkVersion = stringToDouble(jdk.substring(0,
> jdk.length() -1));
> +        }
> +        else {
> +            returnCheck = 0;
> +            checkVersion = stringToDouble(jdk);
> +        }
> +
> +        // Get the current JDK version as a double
> +        currentVersion =
> stringToDouble(System.getProperty("java.version"));
> +
> +        // Check the current version against the checkVersion
> +        if (checkVersion == currentVersion)
> +            returnValue = 0;
> +        else if (currentVersion < checkVersion)
> +            returnValue = -1;
> +        else if (currentVersion > checkVersion)
> +            returnValue = +1;
> +
> +        // If the versions are an exact match, return true.
> Otherwise check to
> +        // see if the returnValue is equal to the checkValue.
> +        if (returnValue == 0) return true;
> +        else return (returnValue == returnCheck);
> +
> +    }
> +
> +    /**
> +     * Utility method to convert strings with multiple decimal
> places into
> +     * doubles that can be safely compared with one another for numerical
> +     * equality.
> +     * @param inString the String to convert to a decimal.
> +     * @return double the value of the string as a double.
> +     */
> +    public static double stringToDouble(String inString) {
> +        double returnValue = 0.0;
> +        int    divisor     = 1;
> +        String digits      = null;
> +        StringTokenizer tokenizer = new StringTokenizer(inString, ".");
> +
> +        while (tokenizer.hasMoreTokens()) {
> +            digits = tokenizer.nextToken();
> +            returnValue += (Double.parseDouble(digits) / (divisor *
> digits.length()) );
> +            divisor *= 10;
> +        }
> +
> +        return returnValue;
> +    }
> +
>  }
>
>


Mime
View raw message