/**
 *  Version of Java Virtual Machine.
 *   @author  Josef.Pelikan@mff.cuni.cz
 *   @version 0.008 (2001/07/19)
 *   @since   0.008
 */
public class VMVersion {

    /** VM version as double number (1.31 for 1.3.1). */
    protected static double vmVersion;

    /** Micro$oft VM. */
    protected static boolean vmMicrosoft;

    /** Information has been retrieved. */
    protected static boolean sniffed = false;

    /**
     *  Returns Java VM version as double number (1.31 for 1.3.1).
     *   @return Java VM version.
     */
    public static double version () {
        if ( !sniffed ) sniff();
        return vmVersion;
    }

    /**
     *  Returns Micro$oft Java VM flag.
     *   @return <code>true</code> if running under Micro$oft VM.
     */
    public static boolean microsoft () {
        if ( !sniffed ) sniff();
        return vmMicrosoft;
    }

    /** Sniff information about current JVM. */
    protected static void sniff () {
        sniffed = true;
            // Java version number:
        String ver = System.getProperty("java.version","1.0");
        boolean wasDot = false;
        vmVersion = 0.0;
        int i;
        double fract = 0.1;
        for ( i = 0; i < ver.length(); i++ ) {
            int digit = ver.charAt(i) - '0';
            if ( wasDot ) {
                if ( digit >= 0 && digit <= 9 ) {
                    vmVersion += fract * digit;
                    fract *= 0.1;
                    }
                }
            else
                if ( ver.charAt(i) == '.' ) wasDot = true;
                else
                if ( digit >= 0 && digit <= 9 )
                    vmVersion = vmVersion * 10.0 + digit;
            }
            // Micro$oft flag:
        String vendor = System.getProperty("java.vendor","Sun");
        vmMicrosoft = vendor.indexOf("Microsoft") >= 0;
    }

}
