1 /*
2 * SPDX-FileCopyrightText: none
3 * SPDX-License-Identifier: CC0-1.0
4 */
5
6 package gov.nist.secauto.metaschema.core.model;
7
8 import edu.umd.cs.findbugs.annotations.NonNull;
9 import edu.umd.cs.findbugs.annotations.Nullable;
10
11 public interface IFieldDefinition extends IModelDefinition, IValuedDefinition, IField {
12
13 @Override
14 default boolean isInline() {
15 // not inline by default
16 return false;
17 }
18
19 @Override
20 default IFieldInstance getInlineInstance() {
21 // not inline by default
22 return null;
23 }
24
25 /**
26 * Retrieves the key to use as the field name for this field's value in JSON.
27 *
28 * @return a string or a FlagInstance value
29 */
30 @Nullable
31 default Object getJsonValueKey() {
32 Object retval = getJsonValueKeyFlagInstance();
33 if (retval == null) {
34 retval = getEffectiveJsonValueKeyName();
35 }
36 return retval;
37 }
38
39 /**
40 * Check if a JSON value key flag is configured.
41 *
42 * @return {@code true} if a JSON value key flag is configured, or {@code false}
43 * otherwise
44 */
45 default boolean hasJsonValueKeyFlagInstance() {
46 return getJsonValueKeyFlagInstance() != null;
47 }
48
49 /**
50 * Retrieves the flag instance who's value will be used as the "value key".
51 *
52 * @return the configured flag instance, or {@code null} if a flag is not
53 * configured as the "value key"
54 */
55 @Nullable
56 IFlagInstance getJsonValueKeyFlagInstance();
57
58 /**
59 * Retrieves the configured static label to use as the value key, or the type
60 * specific name if a label is not configured.
61 *
62 * @return the value key label
63 */
64 @Nullable
65 String getJsonValueKeyName();
66
67 /**
68 * Retrieves the configured static label to use as the value key, or the type
69 * specific name if a label is not configured.
70 *
71 * @return the value key label
72 */
73 @NonNull
74 default String getEffectiveJsonValueKeyName() {
75 String retval = getJsonValueKeyName();
76 if (retval == null || retval.isEmpty()) {
77 retval = getJavaTypeAdapter().getDefaultJsonValueKey();
78 }
79 return retval;
80 }
81
82 /**
83 * Get the value of the field's value from the field item object.
84 *
85 * @param item
86 * the field item
87 * @return the field's value or {@code null} if it has no value
88 */
89 default Object getFieldValue(@NonNull Object item) {
90 // no value by default
91 return null;
92 }
93
94 /**
95 * A visitor callback.
96 *
97 * @param <CONTEXT>
98 * the type of the context parameter
99 * @param <RESULT>
100 * the type of the visitor result
101 * @param visitor
102 * the calling visitor
103 * @param context
104 * a parameter used to pass contextual information between visitors
105 * @return the visitor result
106 */
107 @Override
108 default <CONTEXT, RESULT> RESULT accept(@NonNull IModelElementVisitor<CONTEXT, RESULT> visitor, CONTEXT context) {
109 return visitor.visitFieldDefinition(this, context);
110 }
111 }