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.util.ObjectUtils;
9   import edu.umd.cs.findbugs.annotations.NonNull;
10  import edu.umd.cs.findbugs.annotations.Nullable;
11  
12  /**
13   * Represents a named model instance with absolute positioning and JSON
14   * serialization support.
15   * <p>
16   * Provides JSON name resolution based on cardinality and grouping behavior,
17   * with support for JSON key configuration.
18   */
19  public interface INamedModelInstanceAbsolute extends INamedModelInstance, IModelInstanceAbsolute, IJsonInstance {
20    @Override
21    default String getJsonName() {
22      @NonNull
23      String retval;
24      if (getMaxOccurs() == -1 || getMaxOccurs() > 1) {
25        @NonNull
26        String groupAsName = ObjectUtils.requireNonNull(getGroupAsName(),
27            ObjectUtils.notNull(String.format("null group-as name in instance '%s' on definition '%s' in '%s'",
28                this.getName(),
29                this.getContainingDefinition().getName(),
30                this.getContainingModule().getLocation())));
31        retval = groupAsName;
32      } else {
33        retval = getEffectiveName();
34      }
35      return retval;
36    }
37  
38    @Override
39    @Nullable
40    default IFlagInstance getEffectiveJsonKey() {
41      return getJsonGroupAsBehavior() == JsonGroupAsBehavior.KEYED
42          ? getJsonKey()
43          : null;
44    }
45  
46    @Override
47    @Nullable
48    default IFlagInstance getJsonKey() {
49      return getDefinition().getJsonKey();
50    }
51  }