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 gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  
11  import java.util.Locale;
12  
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  import edu.umd.cs.findbugs.annotations.Nullable;
15  
16  /**
17   * An Metaschema model instance representing a grouped set of objects consisting
18   * of heterogeneous object types.
19   */
20  public interface IChoiceGroupInstance
21      extends IModelInstanceAbsolute, IContainerModelGrouped, IJsonInstance {
22  
23    /**
24     * The default max-occurs value for a choice group. {@code -1} represents an
25     * unbounded occurance.
26     */
27    int DEFAULT_CHOICE_GROUP_GROUP_AS_MAX_OCCURS = -1;
28  
29    /**
30     * The default JSON property value used to identify the specific type of the
31     * object.
32     */
33    @NonNull
34    String DEFAULT_JSON_DISCRIMINATOR_PROPERTY_NAME = "object-type";
35  
36    /**
37     * {@inheritDoc}
38     *
39     * @see #DEFAULT_CHOICE_GROUP_GROUP_AS_MAX_OCCURS
40     */
41    @Override
42    default int getMaxOccurs() {
43      return DEFAULT_CHOICE_GROUP_GROUP_AS_MAX_OCCURS;
44    }
45  
46    /**
47     * Provides the Metaschema model type of "CHOICE".
48     *
49     * @return the model type
50     */
51    @Override
52    default ModelType getModelType() {
53      return ModelType.CHOICE_GROUP;
54    }
55  
56    @Override
57    default String getJsonName() {
58      return ObjectUtils.requireNonNull(getGroupAsName(), "Missing group-as name");
59    }
60  
61    /**
62     * Get the JSON property to use to discriminate between JSON objects.
63     *
64     * @return the discriminator property
65     * @see #DEFAULT_JSON_DISCRIMINATOR_PROPERTY_NAME
66     */
67    @NonNull
68    String getJsonDiscriminatorProperty();
69  
70    @Override
71    default boolean isEffectiveValueWrappedInXml() {
72      return true;
73    }
74  
75    /**
76     * Get the effective name of the JSON key flag, if a JSON key is configured.
77     * <p>
78     * This name is expected to be in the same namespace as the containing model
79     * element (i.e. choice group, assembly, field).
80     *
81     * @return the name of the JSON key flag if configured, or {@code null}
82     *         otherwise
83     */
84    @Nullable
85    String getJsonKeyFlagInstanceName();
86  
87    /**
88     * Get the named model instance for the provided choice group item.
89     *
90     * @param item
91     *          the item to get the instance for
92     * @return the named model instance for the provided choice group item
93     */
94    @NonNull
95    default INamedModelInstanceGrouped getItemInstance(@NonNull Object item) {
96      throw new UnsupportedOperationException("Method not needed.");
97    }
98  
99    @Override
100   default MarkupMultiline getRemarks() {
101     // no remarks
102     return null;
103   }
104 
105   @SuppressWarnings("null")
106   @Override
107   default String toCoordinates() {
108     return String.format("%s-instance:%s:%s/%s@%d",
109         getModelType().toString().toLowerCase(Locale.ROOT),
110         getContainingDefinition().getContainingModule().getShortName(),
111         getContainingDefinition().getName(),
112         getGroupAsName(),
113         hashCode());
114   }
115 
116   /**
117    * A visitor callback.
118    *
119    * @param <CONTEXT>
120    *          the type of the context parameter
121    * @param <RESULT>
122    *          the type of the visitor result
123    * @param visitor
124    *          the calling visitor
125    * @param context
126    *          a parameter used to pass contextual information between visitors
127    * @return the visitor result
128    */
129   @Override
130   default <CONTEXT, RESULT> RESULT accept(@NonNull IModelElementVisitor<CONTEXT, RESULT> visitor, CONTEXT context) {
131     return visitor.visitChoiceGroupInstance(this, context);
132   }
133 }