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.IFieldInstanceGrouped;
10  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
11  import gov.nist.secauto.metaschema.databind.IBindingContext;
12  import gov.nist.secauto.metaschema.databind.model.annotations.BoundGroupedField;
13  import gov.nist.secauto.metaschema.databind.model.impl.DefinitionField;
14  import gov.nist.secauto.metaschema.databind.model.impl.InstanceModelGroupedFieldComplex;
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  
23  /**
24   * Represents a field model instance that is a member of a choice group
25   * instance.
26   */
27  public interface IBoundInstanceModelGroupedField
28      extends IBoundInstanceModelGroupedNamed, IFieldInstanceGrouped {
29  
30    /**
31     * Create a new field model instance instance that is a member of a choice group
32     * instance.
33     *
34     * @param annotation
35     *          the Java annotation the instance is bound to
36     * @param container
37     *          the choice group instance containing the instance
38     * @return the new instance
39     */
40    @NonNull
41    static IBoundInstanceModelGroupedField newInstance(
42        @NonNull BoundGroupedField annotation,
43        @NonNull IBoundInstanceModelChoiceGroup container) {
44      Class<? extends IBoundObject> clazz = annotation.binding();
45      IBindingContext bindingContext = container.getContainingDefinition().getBindingContext();
46      IBoundDefinitionModel<?> definition = bindingContext.getBoundDefinitionForClass(clazz);
47  
48      if (!(definition instanceof DefinitionField)) {
49        Field field = container.getField();
50        throw new IllegalStateException(String.format(
51            "The '%s' annotation, bound to '%s', field '%s' on class '%s' is not bound to a Metaschema field",
52            annotation.getClass(),
53            annotation.binding().getName(),
54            field.toString(),
55            field.getDeclaringClass().getName()));
56      }
57      return new InstanceModelGroupedFieldComplex(annotation, (DefinitionField) definition, container);
58    }
59  
60    @Override
61    IBoundDefinitionModelFieldComplex getDefinition();
62  
63    @Override
64    default IBoundObject readItem(IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
65      return handler.readItemField(ObjectUtils.requireNonNull(parent, "parent"), this);
66    }
67  
68    @Override
69    default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
70      handler.writeItemField(item, this);
71    }
72  }