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.qname.IEnhancedQName;
010import gov.nist.secauto.metaschema.core.util.ObjectUtils;
011import gov.nist.secauto.metaschema.databind.io.BindingException;
012import gov.nist.secauto.metaschema.databind.model.info.IFeatureScalarItemValueHandler;
013import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler;
014import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler;
015
016import java.io.IOException;
017
018import edu.umd.cs.findbugs.annotations.NonNull;
019import edu.umd.cs.findbugs.annotations.Nullable;
020
021public interface IBoundFieldValue extends IFeatureScalarItemValueHandler, IBoundProperty<Object> {
022  @Override
023  @Nullable
024  Object getDefaultValue();
025
026  /**
027   * Get the field definition that contain's the field value.
028   *
029   * @return the parent field definition
030   */
031  @NonNull
032  IBoundDefinitionModelFieldComplex getParentFieldDefinition();
033
034  /**
035   * Get the name of the JSON value key flag.
036   * <p>
037   * Note: if a JSON value key flag is specified, then the JSON value key name is
038   * expected to be ignored.
039   *
040   * @return the flag name or {@code null} if no JSON value key flag is configured
041   * @see #getJsonValueKeyName()
042   */
043  @Nullable
044  String getJsonValueKeyFlagName();
045
046  /**
047   * Get the name of the JSON value key.
048   * <p>
049   * Note: if a JSON value key flag is specified, then this value is expected to
050   * be ignored.
051   *
052   * @return the name
053   * @see #getJsonValueKeyFlagName()
054   */
055  @NonNull
056  String getJsonValueKeyName();
057
058  @Override
059  default Object getEffectiveDefaultValue() {
060    return getDefaultValue();
061  }
062
063  @Override
064  default Object readItem(IBoundObject parent, IItemReadHandler handler) throws IOException {
065    return handler.readItemFieldValue(ObjectUtils.requireNonNull(parent, "parent"), this);
066  }
067
068  @Override
069  default void writeItem(Object item, IItemWriteHandler handler) throws IOException {
070    handler.writeItemFieldValue(item, this);
071  }
072
073  @Override
074  default void deepCopy(@NonNull IBoundObject fromInstance, @NonNull IBoundObject toInstance) throws BindingException {
075    Object value = getValue(fromInstance);
076    if (value != null) {
077      setValue(toInstance, value);
078    }
079  }
080
081  @Override
082  default boolean canHandleXmlQName(IEnhancedQName qname) {
083    // REFACTOR: Is this correct?
084    return getJavaTypeAdapter().canHandleQName(qname);
085  }
086
087}