1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.databind.model;
7   
8   import java.io.IOException;
9   
10  import dev.metaschema.core.model.IBoundObject;
11  import dev.metaschema.core.qname.IEnhancedQName;
12  import dev.metaschema.core.util.ObjectUtils;
13  import dev.metaschema.databind.io.BindingException;
14  import dev.metaschema.databind.model.info.IFeatureScalarItemValueHandler;
15  import dev.metaschema.databind.model.info.IItemReadHandler;
16  import dev.metaschema.databind.model.info.IItemWriteHandler;
17  import edu.umd.cs.findbugs.annotations.NonNull;
18  import edu.umd.cs.findbugs.annotations.Nullable;
19  
20  /**
21   * Represents the bound value of a field definition.
22   * <p>
23   * This interface provides access to the scalar value within a field, including
24   * support for JSON value key handling and default values.
25   */
26  public interface IBoundFieldValue extends IFeatureScalarItemValueHandler, IBoundProperty<Object> {
27    @Override
28    @Nullable
29    Object getDefaultValue();
30  
31    /**
32     * Get the field definition that contain's the field value.
33     *
34     * @return the parent field definition
35     */
36    @NonNull
37    IBoundDefinitionModelFieldComplex getParentFieldDefinition();
38  
39    /**
40     * Get the name of the JSON value key flag.
41     * <p>
42     * Note: if a JSON value key flag is specified, then the JSON value key name is
43     * expected to be ignored.
44     *
45     * @return the flag name or {@code null} if no JSON value key flag is configured
46     * @see #getJsonValueKeyName()
47     */
48    @Nullable
49    String getJsonValueKeyFlagName();
50  
51    /**
52     * Get the name of the JSON value key.
53     * <p>
54     * Note: if a JSON value key flag is specified, then this value is expected to
55     * be ignored.
56     *
57     * @return the name
58     * @see #getJsonValueKeyFlagName()
59     */
60    @NonNull
61    String getJsonValueKeyName();
62  
63    @Override
64    default Object getEffectiveDefaultValue() {
65      return getDefaultValue();
66    }
67  
68    @Override
69    default Object readItem(IBoundObject parent, IItemReadHandler handler) throws IOException {
70      return handler.readItemFieldValue(ObjectUtils.requireNonNull(parent, "parent"), this);
71    }
72  
73    @Override
74    default void writeItem(Object item, IItemWriteHandler handler) throws IOException {
75      handler.writeItemFieldValue(item, this);
76    }
77  
78    @Override
79    default void deepCopy(@NonNull IBoundObject fromInstance, @NonNull IBoundObject toInstance) throws BindingException {
80      Object value = getValue(fromInstance);
81      if (value != null) {
82        setValue(toInstance, value);
83      }
84    }
85  
86    @Override
87    default boolean canHandleXmlQName(IEnhancedQName qname) {
88      // REFACTOR: Is this correct?
89      return getJavaTypeAdapter().canHandleQName(qname);
90    }
91  
92  }