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

io.microsphere.util.VersionUtils Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.microsphere.util;

import javax.lang.model.SourceVersion;

import static io.microsphere.constants.SymbolConstants.DOT_CHAR;
import static io.microsphere.util.SystemUtils.JAVA_VERSION;
import static io.microsphere.util.Version.of;
import static javax.lang.model.SourceVersion.latest;

/**
 * The utility class for version
 *
 * @author Mercy
 * @see Version
 * @since 1.0.0
 */
public abstract class VersionUtils extends BaseUtils {

    /**
     * The latest {@link SourceVersion Java Release Version}
     */
    public static final SourceVersion LATEST_JAVA_VERSION = latest();

    /**
     * The {@link Version} instance for current Java Version
     */
    public static final Version CURRENT_JAVA_VERSION = of(currentJavaVersion());

    /**
     * The {@link Version} instance for Java 8
     */
    public static final Version JAVA_VERSION_8 = of(8);

    /**
     * The {@link Version} instance for Java 9
     */
    public static final Version JAVA_VERSION_9 = of(9);

    /**
     * The {@link Version} instance for Java 10
     */
    public static final Version JAVA_VERSION_10 = of(10);

    /**
     * The {@link Version} instance for Java 11
     */
    public static final Version JAVA_VERSION_11 = of(11);

    /**
     * The {@link Version} instance for Java 12
     */
    public static final Version JAVA_VERSION_12 = of(12);

    /**
     * The {@link Version} instance for Java 13
     */
    public static final Version JAVA_VERSION_13 = of(13);

    /**
     * The {@link Version} instance for Java 14
     */
    public static final Version JAVA_VERSION_14 = of(14);

    /**
     * The {@link Version} instance for Java 15
     */
    public static final Version JAVA_VERSION_15 = of(15);

    /**
     * The {@link Version} instance for Java 16
     */
    public static final Version JAVA_VERSION_16 = of(16);

    /**
     * The {@link Version} instance for Java 17
     */
    public static final Version JAVA_VERSION_17 = of(17);

    /**
     * The {@link Version} instance for Java 18
     */
    public static final Version JAVA_VERSION_18 = of(18);

    /**
     * The {@link Version} instance for Java 19
     */
    public static final Version JAVA_VERSION_19 = of(19);

    /**
     * The {@link Version} instance for Java 20
     */
    public static final Version JAVA_VERSION_20 = of(20);

    /**
     * The {@link Version} instance for Java 21
     */
    public static final Version JAVA_VERSION_21 = of(21);

    /**
     * The {@link Version} instance for Java 22
     */
    public static final Version JAVA_VERSION_22 = of(22);

    /**
     * Determine whether the current Java version matches the specified version
     *
     * @param comparedVersion the {@link Version} to be compared
     * @param versionOperator the {@link Version.Operator}
     * @return true if {@link Version.Operator} {@link Version.Operator#test(Object, Object) matches}
     * {@link #CURRENT_JAVA_VERSION current version} and comparedVersion
     */
    public static boolean testCurrentJavaVersion(Version comparedVersion, Version.Operator versionOperator) {
        return versionOperator.test(CURRENT_JAVA_VERSION, comparedVersion);
    }

    private static String currentJavaVersion() {
        String javaVersion = JAVA_VERSION;
        int firstDotIndex = javaVersion.indexOf(DOT_CHAR);
        if (firstDotIndex > -1) {
            String majorVersion = javaVersion.substring(0, firstDotIndex);
            if ("1".equals(majorVersion)) { // JDK Version is like "1.x.y"
                // it takes 'x' as the major version
                int startIndex = firstDotIndex + 1;
                int endIndex = javaVersion.indexOf(DOT_CHAR, startIndex);
                majorVersion = javaVersion.substring(startIndex, endIndex);
                return majorVersion;
            }
        }
        return javaVersion;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy