001/*
002 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package org.jdrupes.mdoclet.internal;
027
028import java.util.ResourceBundle;
029import java.util.stream.Collectors;
030
031import static java.util.ResourceBundle.getBundle;
032
033public final class Versions {
034
035    private Versions() { throw new AssertionError(); }
036
037    /**
038     * Returns the version of the {@code javadoc} tool and the Standard doclet.
039     *
040     * <p> This is typically the same as the version of the JDK platform being
041     * used to run the tool, but may be different when running the tool on an
042     * older version of the platform.
043     *
044     * @throws RuntimeException in an unlikely event of the version info
045     *                          not being available
046     *
047     * @apiNote This method does not return {@code null}, has the return type of
048     * {@code Optional<Runtime.Version>}, or throw a checked exception. Those
049     * would warp the API to cater for something that is probably a result of
050     * a build error anyway. Hence, {@code RuntimeException}.
051     *
052     * @return the version
053     */
054    public static Runtime.Version javadocVersion() throws RuntimeException {
055        /*
056         * The "org.jdrupes.mdoclet.internal.tool.resources.version" resource bundle is
057         * non-localized and represented by a class compiled from a source like this:
058         *
059         * $ cat build/.../support/gensrc/jdk.javadoc/jdk/javadoc/internal/tool/resources/version.java
060         * package org.jdrupes.mdoclet.internal.tool.resources;
061         *
062         * public final class version extends java.util.ListResourceBundle {
063         *     protected final Object[][] getContents() {
064         *         return new Object[][] {
065         *             { "full", "15-internal+0-2020-06-02-1426246.duke..." },
066         *             { "jdk", "15" },
067         *             { "release", "15-internal" },
068         *         };
069         *     }
070         * }
071         *
072         * The string keyed by "full" should be parseable by Runtime.Version.parse()
073         */
074        ResourceBundle bundle = getBundle("org.jdrupes.mdoclet.internal.tool.resources.version");
075        return Runtime.Version.parse(bundle.getString("full"));
076    }
077
078    /**
079     * Returns a short string representation of the provided version.
080     *
081     * <p> The string contains the dotted representation of the version number,
082     * followed by the prerelease info, if any.
083     * For example, "15", "15.1", "15.0.1", "15-internal".
084     *
085     * @return a short string representation of the provided version
086     *
087     * @throws NullPointerException if {@code v == null}
088     */
089    public static String shortVersionStringOf(Runtime.Version v) {
090        String svstr = v.version().stream()
091                .map(Object::toString)
092                .collect(Collectors.joining("."));
093        if (v.pre().isPresent()) {
094            svstr += "-" + v.pre().get();
095        }
096        return svstr;
097    }
098
099    /**
100     * Returns a full string representation of the provided version.
101     *
102     * <p> Examples of strings returned from this method are "14+36-1461" and
103     * "15-internal+0-2020-06-02-1426246.duke...".
104     *
105     * @return a full string representation of the provided version
106     *
107     * @throws NullPointerException if {@code v == null}
108     */
109    public static String fullVersionStringOf(Runtime.Version v) {
110        return v.toString();
111    }
112}