001/*
002 * (C) Copyright 2012-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 */
017package org.nuxeo.launcher.config;
018
019import java.text.ParseException;
020import java.util.regex.Matcher;
021import java.util.regex.Pattern;
022
023public class JVMVersion implements Comparable<JVMVersion> {
024
025    public final int major;
026
027    public final int update;
028
029    public enum UpTo {
030        MAJOR, UPDATE;
031    }
032
033    public JVMVersion(int major, int minor) {
034        this.major = major;
035        update = minor;
036    }
037
038    public int compareTo(JVMVersion o, UpTo upTo) {
039        if (major != o.major) {
040            return major - o.major;
041        }
042        if (upTo == UpTo.MAJOR) {
043            return 0;
044        }
045        if (update != o.update) {
046            return update - o.update;
047        }
048        return 0;
049    }
050
051
052    @Override
053    public int compareTo(JVMVersion o) {
054        return compareTo(o, UpTo.UPDATE);
055    }
056
057    @Override
058    public String toString() {
059        return String.format("%d.%d", major, update);
060    }
061
062    public static JVMVersion parse(String version) throws ParseException {
063        if (version.startsWith("1.")) {
064            return parsePreJdk9(version);
065        } else {
066            return parseJdk9(version);
067        }
068    }
069
070    static final Pattern PreJDK9Pattern = Pattern.compile("1\\.(\\d)\\.\\d(?:_(\\d+))?(?:-.*)?");
071
072    static final Pattern JDK9_PATTERN = Pattern.compile("(\\d+)(?:-ea)?(?:\\.(\\d+)(?:\\.(\\d+)(\\..*)?)?)?");
073
074    public static JVMVersion parsePreJdk9(String version) throws ParseException {
075        Matcher matcher = PreJDK9Pattern.matcher(version);
076        if (!matcher.matches()) {
077            throw new ParseException("Cannot parse " + version + " as a pre JVM 9 version", -1);
078        }
079        final int groupCount = matcher.groupCount();
080        String major = matcher.group(1);
081        String minor = groupCount >= 2 ? matcher.group(2) : null;
082        return new JVMVersion(Integer.parseInt(major), minor == null ? 0 : Integer.parseInt(minor));
083    }
084
085    public static JVMVersion parseJdk9(String version) throws ParseException {
086        Matcher matcher = JDK9_PATTERN.matcher(version);
087        if (!matcher.matches()) {
088            throw new ParseException("Cannot parse " + version + " as a JDK 9+ version", -1);
089        }
090        final int groupCount = matcher.groupCount();
091        String major = matcher.group(1);
092        String minor = groupCount >= 2 ? matcher.group(2) : null;
093        return new JVMVersion(Integer.parseInt(major), minor == null ? 0 : Integer.parseInt(minor));
094    }
095
096}