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