1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.model;
7   
8   import java.io.IOException;
9   import java.lang.reflect.Field;
10  
11  import dev.metaschema.core.model.IBoundObject;
12  import dev.metaschema.core.model.IChoiceGroupInstance;
13  import dev.metaschema.core.model.IFeatureContainerModelGrouped;
14  import dev.metaschema.core.qname.IEnhancedQName;
15  import dev.metaschema.core.util.ObjectUtils;
16  import dev.metaschema.databind.io.BindingException;
17  import dev.metaschema.databind.model.impl.InstanceModelChoiceGroup;
18  import dev.metaschema.databind.model.info.IItemReadHandler;
19  import dev.metaschema.databind.model.info.IItemWriteHandler;
20  import edu.umd.cs.findbugs.annotations.NonNull;
21  import edu.umd.cs.findbugs.annotations.Nullable;
22  
23  /**
24   * Represents a choice group instance bound to Java field.
25   */
26  public interface IBoundInstanceModelChoiceGroup
27      extends IBoundInstanceModel<IBoundObject>, IFeatureContainerModelGrouped<
28          IBoundInstanceModelGroupedNamed,
29          IBoundInstanceModelGroupedField,
30          IBoundInstanceModelGroupedAssembly>,
31      IChoiceGroupInstance {
32  
33    /**
34     * Create a new bound choice group instance.
35     *
36     * @param field
37     *          the Java field the instance is bound to
38     * @param containingDefinition
39     *          the definition containing the instance
40     * @return the new instance
41     */
42    @NonNull
43    static IBoundInstanceModelChoiceGroup newInstance(
44        @NonNull Field field,
45        @NonNull IBoundDefinitionModelAssembly containingDefinition) {
46      return InstanceModelChoiceGroup.newInstance(field, containingDefinition);
47    }
48  
49    @Override
50    default String getJsonName() {
51      // always the group-as name
52      return ObjectUtils.requireNonNull(getGroupAsName());
53    }
54  
55    @Override
56    @NonNull
57    IBoundDefinitionModelAssembly getOwningDefinition();
58  
59    @Override
60    default IBoundDefinitionModelAssembly getContainingDefinition() {
61      return getOwningDefinition();
62    }
63  
64    /**
65     * Get the bound grouped model instance associated with the provided Java class.
66     *
67     * @param clazz
68     *          the Java class which should be bound to a grouped model instance
69     * @return the grouped model instance or {code null} if no instance was bound to
70     *         the requested class
71     */
72    @Nullable
73    IBoundInstanceModelGroupedNamed getGroupedModelInstance(@NonNull Class<?> clazz);
74  
75    /**
76     * Get the bound grouped model instance associated with the provided XML
77     * qualified name.
78     *
79     * @param name
80     *          the XML qualified name which should be bound to a grouped model
81     *          instance
82     * @return the grouped model instance or {code null} if no instance was bound to
83     *         the requested XML qualified name
84     */
85    @Nullable
86    IBoundInstanceModelGroupedNamed getGroupedModelInstance(@NonNull IEnhancedQName name);
87  
88    /**
89     * Get the bound grouped model instance associated with the provided JSON
90     * discriminator value.
91     *
92     * @param discriminator
93     *          the JSON discriminator value which should be bound to a grouped
94     *          model instance
95     * @return the grouped model instance or {code null} if no instance was bound to
96     *         the requested JSON discriminator value
97     */
98    @Nullable
99    IBoundInstanceModelGroupedNamed getGroupedModelInstance(@NonNull String discriminator);
100 
101   /**
102    * Get the bound grouped model instance associated with the provided item.
103    *
104    * @param item
105    *          the item which should be bound to a grouped model instance
106    * @return the grouped model instance or {code null} if no instance was bound to
107    *         the requested item
108    */
109   @Override
110   @NonNull
111   default IBoundInstanceModelGroupedNamed getItemInstance(Object item) {
112     return ObjectUtils.requireNonNull(getGroupedModelInstance(item.getClass()));
113   }
114 
115   @Override
116   default IBoundObject readItem(IBoundObject parent, IItemReadHandler handler) throws IOException {
117     return handler.readChoiceGroupItem(ObjectUtils.requireNonNull(parent, "parent"), this);
118   }
119 
120   @Override
121   default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
122     handler.writeChoiceGroupItem(item, this);
123   }
124 
125   @Override
126   default IBoundObject deepCopyItem(IBoundObject item, IBoundObject parentInstance) throws BindingException {
127     IBoundInstanceModelGroupedNamed itemInstance = getItemInstance(item);
128     return itemInstance.deepCopyItem(item, parentInstance);
129   }
130 
131   @Override
132   default boolean canHandleXmlQName(@NonNull IEnhancedQName qname) {
133     return getGroupedModelInstance(qname) != null;
134   }
135 }