001/*
002 * Copyright (c) 1998, 2022, 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.doclets.toolkit.util;
027
028import javax.lang.model.element.AnnotationValue;
029import javax.lang.model.element.Element;
030import javax.lang.model.element.ExecutableElement;
031
032import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseConfiguration;
033
034import java.util.HashMap;
035import java.util.Map;
036import java.util.Objects;
037import java.util.Set;
038import java.util.TreeSet;
039
040/**
041 * Build list of all the preview packages, classes, constructors, fields and methods.
042 */
043public class PreviewAPIListBuilder extends SummaryAPIListBuilder {
044
045    final private Map<Element, JEP> elementJeps = new HashMap<>();
046    final private Map<String, JEP> jeps = new HashMap<>();
047
048    /**
049     * The JEP for a preview feature in this release.
050     */
051    public record JEP(int number, String title, String status)
052            implements Comparable<JEP> {
053        @Override
054        public int compareTo(JEP o) {
055            return number - o.number;
056        }
057    }
058
059    /**
060     * Constructor.
061     *
062     * @param configuration the current configuration of the doclet
063     */
064    public PreviewAPIListBuilder(BaseConfiguration configuration) {
065        super(configuration, configuration.utils::isPreviewAPI);
066        buildSummaryAPIInfo();
067    }
068
069    @Override
070    protected void handleElement(Element e) {
071        String feature = Objects.requireNonNull(utils.getPreviewFeature(e),
072            "Preview feature not specified").toString();
073        JEP jep = jeps.computeIfAbsent(feature, (featureName) -> {
074            Map<? extends ExecutableElement, ? extends AnnotationValue> anno
075                = configuration.workArounds.getJepInfo(featureName);
076            int number = 0;
077            String title = "";
078            String status = "Preview"; // Default value is not returned by the
079                                       // method we use above.
080            for (var entry : anno.entrySet()) {
081                if ("number"
082                    .equals(entry.getKey().getSimpleName().toString())) {
083                    number = (int) entry.getValue().getValue();
084                } else if ("title"
085                    .equals(entry.getKey().getSimpleName().toString())) {
086                    title = (String) entry.getValue().getValue();
087                } else if ("status"
088                    .equals(entry.getKey().getSimpleName().toString())) {
089                    status = (String) entry.getValue().getValue();
090                } else {
091                    throw new IllegalArgumentException(
092                        entry.getKey().getSimpleName().toString());
093                }
094            }
095            return new JEP(number, title, status);
096        });
097        elementJeps.put(e, jep);
098    }
099
100    /**
101     * {@return a sorted set of preview feature JEPs in this release}
102     */
103    public Set<JEP> getJEPs() {
104        return new TreeSet<>(jeps.values());
105    }
106
107    /**
108     * {@return the JEP for a preview element}
109     */
110    public JEP getJEP(Element e) {
111        return elementJeps.get(e);
112    }
113}