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