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