1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.model;
7   
8   import dev.metaschema.core.model.util.ModuleUtils;
9   import dev.metaschema.core.util.ObjectUtils;
10  import edu.umd.cs.findbugs.annotations.NonNull;
11  import edu.umd.cs.findbugs.annotations.Nullable;
12  
13  /**
14   * Represents an arbitrary grouping of Metaschema model instances.
15   */
16  public interface INamedModelInstanceGrouped extends INamedModelInstance {
17    @Override
18    IChoiceGroupInstance getParentContainer();
19  
20    @Override
21    default IAssemblyDefinition getContainingDefinition() {
22      return getParentContainer().getContainingDefinition();
23    }
24  
25    /**
26     * Get the discriminator JSON property name to use to identify the type of a
27     * given instance object.
28     *
29     * @return the discriminator property name or {@code null} if the effective name
30     *         should be used instead
31     */
32    @Nullable
33    String getDiscriminatorValue();
34  
35    /**
36     * Get the effective discriminator JSON property name to use to identify the
37     * type of a given instance object.
38     *
39     * @return the discriminator property name
40     */
41    @NonNull
42    default String getEffectiveDisciminatorValue() {
43      String retval = getDiscriminatorValue();
44      if (retval == null) {
45        retval = getEffectiveName();
46      }
47      return retval;
48    }
49  
50    @Override
51    @Nullable
52    default IFlagInstance getEffectiveJsonKey() {
53      return getParentContainer().getJsonGroupAsBehavior() == JsonGroupAsBehavior.KEYED
54          ? ObjectUtils.requireNonNull(getJsonKey())
55          : null;
56    }
57  
58    @Override
59    @Nullable
60    default IFlagInstance getJsonKey() {
61      String name = getParentContainer().getJsonKeyFlagInstanceName();
62      return name == null
63          ? null
64          : ObjectUtils.requireNonNull(
65              getDefinition().getFlagInstanceByName(
66                  ModuleUtils.parseFlagName(getContainingModule(), name).getIndexPosition()));
67    }
68  
69    @Override
70    default int getMinOccurs() {
71      return getParentContainer().getMinOccurs();
72    }
73  
74    @Override
75    default int getMaxOccurs() {
76      return getParentContainer().getMaxOccurs();
77    }
78  
79  }