001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.core.model;
007
008import dev.metaschema.core.util.ObjectUtils;
009import edu.umd.cs.findbugs.annotations.NonNull;
010import edu.umd.cs.findbugs.annotations.Nullable;
011
012/**
013 * Represents a named model instance with absolute positioning and JSON
014 * serialization support.
015 * <p>
016 * Provides JSON name resolution based on cardinality and grouping behavior,
017 * with support for JSON key configuration.
018 */
019public interface INamedModelInstanceAbsolute extends INamedModelInstance, IModelInstanceAbsolute, IJsonInstance {
020  @Override
021  default String getJsonName() {
022    @NonNull
023    String retval;
024    if (getMaxOccurs() == -1 || getMaxOccurs() > 1) {
025      @NonNull
026      String groupAsName = ObjectUtils.requireNonNull(getGroupAsName(),
027          ObjectUtils.notNull(String.format("null group-as name in instance '%s' on definition '%s' in '%s'",
028              this.getName(),
029              this.getContainingDefinition().getName(),
030              this.getContainingModule().getLocation())));
031      retval = groupAsName;
032    } else {
033      retval = getEffectiveName();
034    }
035    return retval;
036  }
037
038  @Override
039  @Nullable
040  default IFlagInstance getEffectiveJsonKey() {
041    return getJsonGroupAsBehavior() == JsonGroupAsBehavior.KEYED
042        ? getJsonKey()
043        : null;
044  }
045
046  @Override
047  @Nullable
048  default IFlagInstance getJsonKey() {
049    return getDefinition().getJsonKey();
050  }
051}