001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package gov.nist.secauto.metaschema.databind.model;
007
008import gov.nist.secauto.metaschema.core.model.IBoundObject;
009import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance;
010import gov.nist.secauto.metaschema.core.util.ObjectUtils;
011import gov.nist.secauto.metaschema.databind.io.BindingException;
012import gov.nist.secauto.metaschema.databind.model.impl.InstanceModelChoiceGroup;
013import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler;
014import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
015
016import java.io.IOException;
017import java.lang.reflect.Field;
018
019import javax.xml.namespace.QName;
020
021import edu.umd.cs.findbugs.annotations.NonNull;
022import edu.umd.cs.findbugs.annotations.Nullable;
023
024/**
025 * Represents a choice group instance bound to Java field.
026 */
027public interface IBoundInstanceModelChoiceGroup
028    extends IBoundInstanceModel<IBoundObject>, IBoundContainerModelChoiceGroup, IChoiceGroupInstance {
029
030  /**
031   * Create a new bound choice group instance.
032   *
033   * @param field
034   *          the Java field the instance is bound to
035   * @param containingDefinition
036   *          the definition containing the instance
037   * @return the new instance
038   */
039  @NonNull
040  static IBoundInstanceModelChoiceGroup newInstance(
041      @NonNull Field field,
042      @NonNull IBoundDefinitionModelAssembly containingDefinition) {
043    return InstanceModelChoiceGroup.newInstance(field, containingDefinition);
044  }
045
046  @Override
047  default String getJsonName() {
048    // always the group-as name
049    return ObjectUtils.requireNonNull(getGroupAsName());
050  }
051
052  @Override
053  @NonNull
054  IBoundDefinitionModelAssembly getOwningDefinition();
055
056  @Override
057  default IBoundDefinitionModelAssembly getContainingDefinition() {
058    return getOwningDefinition();
059  }
060
061  /**
062   * Get the bound grouped model instance associated with the provided Java class.
063   *
064   * @param clazz
065   *          the Java class which should be bound to a grouped model instance
066   * @return the grouped model instance or {code null} if no instance was bound to
067   *         the requested class
068   */
069  @Nullable
070  IBoundInstanceModelGroupedNamed getGroupedModelInstance(@NonNull Class<?> clazz);
071
072  /**
073   * Get the bound grouped model instance associated with the provided XML
074   * qualified name.
075   *
076   * @param name
077   *          the XML qualified name which should be bound to a grouped model
078   *          instance
079   * @return the grouped model instance or {code null} if no instance was bound to
080   *         the requested XML qualified name
081   */
082  @Nullable
083  IBoundInstanceModelGroupedNamed getGroupedModelInstance(@NonNull QName name);
084
085  /**
086   * Get the bound grouped model instance associated with the provided JSON
087   * discriminator value.
088   *
089   * @param discriminator
090   *          the JSON discriminator value which should be bound to a grouped
091   *          model instance
092   * @return the grouped model instance or {code null} if no instance was bound to
093   *         the requested JSON discriminator value
094   */
095  @Nullable
096  IBoundInstanceModelGroupedNamed getGroupedModelInstance(@NonNull String discriminator);
097
098  /**
099   * 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}