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.IAssemblyInstanceGrouped;
9   import gov.nist.secauto.metaschema.core.model.IBoundObject;
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.BoundGroupedAssembly;
13  import gov.nist.secauto.metaschema.databind.model.impl.InstanceModelGroupedAssembly;
14  import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler;
15  import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
16  
17  import java.io.IOException;
18  import java.lang.reflect.Field;
19  
20  import edu.umd.cs.findbugs.annotations.NonNull;
21  
22  /**
23   * Represents an assembly model instance that is a member of a choice group
24   * instance.
25   */
26  public interface IBoundInstanceModelGroupedAssembly
27      extends IBoundInstanceModelGroupedNamed, IAssemblyInstanceGrouped {
28  
29    /**
30     * Create a new assembly model instance instance that is a member of a choice
31     * group instance.
32     *
33     * @param annotation
34     *          the Java annotation the instance is bound to
35     * @param container
36     *          the choice group instance containing the instance
37     * @return the new instance
38     */
39    static IBoundInstanceModelGroupedAssembly newInstance(
40        @NonNull BoundGroupedAssembly annotation,
41        @NonNull IBoundInstanceModelChoiceGroup container) {
42      Class<? extends IBoundObject> clazz = annotation.binding();
43      IBindingContext bindingContext = container.getContainingDefinition().getBindingContext();
44      IBoundDefinitionModel<?> definition = bindingContext.getBoundDefinitionForClass(clazz);
45      if (definition instanceof IBoundDefinitionModelAssembly) {
46        return new InstanceModelGroupedAssembly(annotation, (IBoundDefinitionModelAssembly) definition, container);
47      }
48  
49      Field field = container.getField();
50      throw new IllegalStateException(String.format(
51          "The '%s' annotation, bound to '%s', on field '%s' on class '%s' is not bound to a Metaschema assembly",
52          annotation.getClass(),
53          annotation.binding().getName(),
54          field.toString(),
55          field.getDeclaringClass().getName()));
56    }
57  
58    @Override
59    IBoundDefinitionModelAssembly getDefinition();
60  
61    @Override
62    default IBoundObject readItem(IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
63      return handler.readItemAssembly(ObjectUtils.requireNonNull(parent, "parent"), this);
64    }
65  
66    @Override
67    default void writeItem(IBoundObject item, IItemWriteHandler handler) throws IOException {
68      handler.writeItemAssembly(item, this);
69    }
70  }