001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.core.model;
007
008import dev.metaschema.core.model.util.ModuleUtils;
009import dev.metaschema.core.util.ObjectUtils;
010import edu.umd.cs.findbugs.annotations.NonNull;
011import edu.umd.cs.findbugs.annotations.Nullable;
012
013/**
014 * Represents an arbitrary grouping of Metaschema model instances.
015 */
016public interface INamedModelInstanceGrouped extends INamedModelInstance {
017  @Override
018  IChoiceGroupInstance getParentContainer();
019
020  @Override
021  default IAssemblyDefinition getContainingDefinition() {
022    return getParentContainer().getContainingDefinition();
023  }
024
025  /**
026   * Get the discriminator JSON property name to use to identify the type of a
027   * given instance object.
028   *
029   * @return the discriminator property name or {@code null} if the effective name
030   *         should be used instead
031   */
032  @Nullable
033  String getDiscriminatorValue();
034
035  /**
036   * Get the effective discriminator JSON property name to use to identify the
037   * type of a given instance object.
038   *
039   * @return the discriminator property name
040   */
041  @NonNull
042  default String getEffectiveDisciminatorValue() {
043    String retval = getDiscriminatorValue();
044    if (retval == null) {
045      retval = getEffectiveName();
046    }
047    return retval;
048  }
049
050  @Override
051  @Nullable
052  default IFlagInstance getEffectiveJsonKey() {
053    return getParentContainer().getJsonGroupAsBehavior() == JsonGroupAsBehavior.KEYED
054        ? ObjectUtils.requireNonNull(getJsonKey())
055        : null;
056  }
057
058  @Override
059  @Nullable
060  default IFlagInstance getJsonKey() {
061    String name = getParentContainer().getJsonKeyFlagInstanceName();
062    return name == null
063        ? null
064        : ObjectUtils.requireNonNull(
065            getDefinition().getFlagInstanceByName(
066                ModuleUtils.parseFlagName(getContainingModule(), name).getIndexPosition()));
067  }
068
069  @Override
070  default int getMinOccurs() {
071    return getParentContainer().getMinOccurs();
072  }
073
074  @Override
075  default int getMaxOccurs() {
076    return getParentContainer().getMaxOccurs();
077  }
078
079}