001/*
002 * Copyright (c) 2003, 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.builders;
027
028import static org.jdrupes.mdoclet.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
029
030import java.util.*;
031
032import javax.lang.model.element.Element;
033import javax.lang.model.element.TypeElement;
034import javax.lang.model.element.VariableElement;
035
036import org.jdrupes.mdoclet.internal.doclets.toolkit.BaseOptions;
037import org.jdrupes.mdoclet.internal.doclets.toolkit.Content;
038import org.jdrupes.mdoclet.internal.doclets.toolkit.DocletException;
039import org.jdrupes.mdoclet.internal.doclets.toolkit.EnumConstantWriter;
040
041/**
042 * Builds documentation for a enum constants.
043 */
044public class EnumConstantBuilder extends AbstractMemberBuilder {
045
046    /**
047     * The writer to output the enum constants documentation.
048     */
049    private final EnumConstantWriter writer;
050
051    /**
052     * The set of enum constants being documented.
053     */
054    private final List<? extends Element> enumConstants;
055
056    /**
057     * The current enum constant that is being documented at this point
058     * in time.
059     */
060    private VariableElement currentElement;
061
062    /**
063     * Construct a new EnumConstantsBuilder.
064     *
065     * @param context  the build context.
066     * @param typeElement the class whose members are being documented.
067     * @param writer the doclet specific writer.
068     */
069    private EnumConstantBuilder(Context context,
070            TypeElement typeElement, EnumConstantWriter writer) {
071        super(context, typeElement);
072        this.writer = Objects.requireNonNull(writer);
073        enumConstants = getVisibleMembers(ENUM_CONSTANTS);
074    }
075
076    /**
077     * Construct a new EnumConstantsBuilder.
078     *
079     * @param context  the build context.
080     * @param typeElement the class whose members are being documented.
081     * @param writer the doclet specific writer.
082     * @return the new EnumConstantsBuilder
083     */
084    public static EnumConstantBuilder getInstance(Context context,
085            TypeElement typeElement, EnumConstantWriter writer) {
086        return new EnumConstantBuilder(context, typeElement, writer);
087    }
088
089    /**
090     * Returns whether or not there are members to document.
091     *
092     * @return whether or not there are members to document
093     */
094    @Override
095    public boolean hasMembersToDocument() {
096        return !enumConstants.isEmpty();
097    }
098
099    @Override
100    public void build(Content target) throws DocletException {
101        buildEnumConstant(target);
102    }
103
104    /**
105     * Build the enum constant documentation.
106     *
107     * @param target the content to which the documentation will be added
108     * @throws DocletException is there is a problem while building the documentation
109     */
110    protected void buildEnumConstant(Content target) throws DocletException {
111        if (hasMembersToDocument()) {
112            Content enumConstantsDetailsHeader
113                = writer.getEnumConstantsDetailsHeader(typeElement,
114                    target);
115            Content memberList = writer.getMemberList();
116
117            for (Element enumConstant : enumConstants) {
118                currentElement = (VariableElement) enumConstant;
119                Content enumConstants
120                    = writer.getEnumConstantsHeader(currentElement,
121                        memberList);
122
123                buildSignature(enumConstants);
124                buildDeprecationInfo(enumConstants);
125                buildPreviewInfo(enumConstants);
126                buildEnumConstantComments(enumConstants);
127                buildTagInfo(enumConstants);
128
129                memberList.add(writer.getMemberListItem(enumConstants));
130            }
131            Content enumConstantDetails = writer.getEnumConstantsDetails(
132                enumConstantsDetailsHeader, memberList);
133            target.add(enumConstantDetails);
134        }
135    }
136
137    /**
138     * Build the signature.
139     *
140     * @param target the content to which the documentation will be added
141     */
142    protected void buildSignature(Content target) {
143        target.add(writer.getSignature(currentElement));
144    }
145
146    /**
147     * Build the deprecation information.
148     *
149     * @param target the content to which the documentation will be added
150     */
151    protected void buildDeprecationInfo(Content target) {
152        writer.addDeprecated(currentElement, target);
153    }
154
155    /**
156     * Build the preview information.
157     *
158     * @param target the content to which the documentation will be added
159     */
160    protected void buildPreviewInfo(Content target) {
161        writer.addPreview(currentElement, target);
162    }
163
164    /**
165     * Build the comments for the enum constant.  Do nothing if
166     * {@link BaseOptions#noComment()} is set to true.
167     *
168     * @param target the content to which the documentation will be added
169     */
170    protected void buildEnumConstantComments(Content target) {
171        if (!options.noComment()) {
172            writer.addComments(currentElement, target);
173        }
174    }
175
176    /**
177     * Build the tag information.
178     *
179     * @param target the content to which the documentation will be added
180     */
181    protected void buildTagInfo(Content target) {
182        writer.addTags(currentElement, target);
183    }
184
185    /**
186     * Return the enum constant writer for this builder.
187     *
188     * @return the enum constant writer for this builder.
189     */
190    public EnumConstantWriter getWriter() {
191        return writer;
192    }
193}