public IJavaVersion getValidJavaVersion(JavaVersionRepository repository) {

        boolean requires64Bit = getMemory() > Memory.MAX_32_BIT_MEMORY;

        IJavaVersion best64Bit = repository.getBest64BitVersion();
        if (best64Bit != null && isJavaValid(best64Bit.getVersionNumber()))
            return best64Bit;
        else if (best64Bit == null && requires64Bit)
            return null;

        IJavaVersion bestVersion = null;
        for (IJavaVersion version : repository.getVersions()) {
            if (isJavaValid(version.getVersionNumber()) && (!requires64Bit || version.is64Bit()))
                bestVersion = version;
        }

        return bestVersion;
    }