1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.model.impl;
7   
8   import dev.metaschema.core.model.IModule;
9   import dev.metaschema.core.model.JsonGroupAsBehavior;
10  import dev.metaschema.core.model.XmlGroupAsBehavior;
11  import dev.metaschema.core.model.util.ModuleUtils;
12  import dev.metaschema.core.qname.IEnhancedQName;
13  import dev.metaschema.databind.model.IGroupAs;
14  import dev.metaschema.databind.model.annotations.GroupAs;
15  import dev.metaschema.databind.model.annotations.ModelUtil;
16  import edu.umd.cs.findbugs.annotations.NonNull;
17  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18  
19  /**
20   * Default implementation of {@link IGroupAs} for bound model instances.
21   * <p>
22   * This class represents the group-as configuration for collection-type model
23   * instances, including the qualified name and grouping behaviors for XML and
24   * JSON serialization.
25   */
26  public class DefaultGroupAs implements IGroupAs {
27    @NonNull
28    private final IEnhancedQName qname;
29    @NonNull
30    private final GroupAs annotation;
31  
32    /**
33     * Constructs a new group-as configuration from the given annotation.
34     *
35     * @param annotation
36     *          the {@link GroupAs} annotation providing the configuration
37     * @param module
38     *          the module used to resolve the namespace for the group name
39     * @throws IllegalStateException
40     *           if the annotation's name value resolves to {@code null}
41     */
42    @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields")
43    public DefaultGroupAs(
44        @NonNull GroupAs annotation,
45        @NonNull IModule module) {
46      this.annotation = annotation;
47      String value = ModelUtil.resolveNoneOrDefault(annotation.name(), null);
48      if (value == null) {
49        throw new IllegalStateException(
50            String.format("The %s#groupName value '%s' resulted in an invalid null value",
51                GroupAs.class.getName(),
52                annotation.name()));
53      }
54      this.qname = ModuleUtils.parseModelName(module, value);
55    }
56  
57    @Override
58    public IEnhancedQName getGroupAsQName() {
59      return qname;
60    }
61  
62    @Override
63    public JsonGroupAsBehavior getJsonGroupAsBehavior() {
64      return annotation.inJson();
65    }
66  
67    @Override
68    public XmlGroupAsBehavior getXmlGroupAsBehavior() {
69      return annotation.inXml();
70    }
71  }