1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.codegen.typeinfo;
7   
8   import com.squareup.javapoet.FieldSpec;
9   import com.squareup.javapoet.MethodSpec;
10  
11  import dev.metaschema.core.datatype.markup.MarkupLine;
12  import dev.metaschema.core.model.INamedInstance;
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  
15  public interface INamedInstanceTypeInfo extends IInstanceTypeInfo {
16    @Override
17    INamedInstance getInstance();
18  
19    /**
20     * {@inheritDoc}
21     *
22     * <p>
23     * This implementation adds the effective description from the named instance as
24     * the field's Javadoc content.
25     */
26    @Override
27    default void buildFieldJavadoc(FieldSpec.Builder builder) {
28      MarkupLine description = getInstance().getEffectiveDescription();
29      if (description != null) {
30        builder.addJavadoc("$L\n", description.toHtml());
31      }
32    }
33  
34    /**
35     * {@inheritDoc}
36     *
37     * <p>
38     * This implementation generates getter Javadoc using the instance's formal name
39     * (if available) or property name, adds the effective description, and includes
40     * an appropriate {@code @return} tag based on whether the property is required
41     * or a collection.
42     */
43    @Override
44    default void buildGetterJavadoc(@NonNull MethodSpec.Builder builder) {
45      MarkupLine description = getInstance().getEffectiveDescription();
46      String formalName = getInstance().getEffectiveFormalName();
47      String propertyName = getInstance().getEffectiveName();
48  
49      // Use formal name if available, otherwise property name
50      if (formalName != null) {
51        builder.addJavadoc("Get the \"{@literal $L}\".\n", formalName);
52      } else {
53        builder.addJavadoc("Get the {@code $L} property.\n", propertyName);
54      }
55  
56      // Add description as a second paragraph if available
57      if (description != null) {
58        builder.addJavadoc("\n");
59        builder.addJavadoc("<p>\n");
60        builder.addJavadoc("$L\n", description.toHtml());
61      }
62  
63      builder.addJavadoc("\n");
64      // Collections are always @NonNull (lazy initialized), required properties are
65      // @NonNull
66      if (isRequired() || isCollectionType()) {
67        builder.addJavadoc("@return the $L value\n", propertyName);
68      } else {
69        builder.addJavadoc("@return the $L value, or {@code null} if not set\n", propertyName);
70      }
71    }
72  
73    /**
74     * {@inheritDoc}
75     *
76     * <p>
77     * This implementation generates setter Javadoc using the instance's formal name
78     * (if available) or property name, adds the effective description, and includes
79     * a {@code @param} tag for the value parameter.
80     */
81    @Override
82    default void buildSetterJavadoc(@NonNull MethodSpec.Builder builder, @NonNull String paramName) {
83      MarkupLine description = getInstance().getEffectiveDescription();
84      String formalName = getInstance().getEffectiveFormalName();
85      String propertyName = getInstance().getEffectiveName();
86  
87      // Use formal name if available, otherwise property name
88      if (formalName != null) {
89        builder.addJavadoc("Set the \"{@literal $L}\".\n", formalName);
90      } else {
91        builder.addJavadoc("Set the {@code $L} property.\n", propertyName);
92      }
93  
94      // Add description as a second paragraph if available
95      if (description != null) {
96        builder.addJavadoc("\n");
97        builder.addJavadoc("<p>\n");
98        builder.addJavadoc("$L\n", description.toHtml());
99      }
100 
101     builder.addJavadoc("\n");
102     builder.addJavadoc("@param $L\n", paramName);
103     // Collections and required properties require non-null values;
104     // optional properties can be set to null to clear
105     if (isRequired() || isCollectionType()) {
106       builder.addJavadoc("          the $L value to set\n", propertyName);
107     } else {
108       builder.addJavadoc("          the $L value to set, or {@code null} to clear\n", propertyName);
109     }
110   }
111 }