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.IFeatureDefinitionInstanceInlined;
10  import gov.nist.secauto.metaschema.core.model.IFlagInstance;
11  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
12  import gov.nist.secauto.metaschema.databind.io.BindingException;
13  import gov.nist.secauto.metaschema.databind.model.impl.InstanceFlagInline;
14  import gov.nist.secauto.metaschema.databind.model.info.IFeatureScalarItemValueHandler;
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 javax.xml.namespace.QName;
22  
23  import edu.umd.cs.findbugs.annotations.NonNull;
24  
25  /**
26   * Represents a flag instance bound to Java data.
27   */
28  public interface IBoundInstanceFlag
29      extends IFlagInstance, IBoundDefinitionFlag,
30      IFeatureScalarItemValueHandler,
31      IBoundInstance<Object>,
32      IFeatureDefinitionInstanceInlined<IBoundDefinitionFlag, IBoundInstanceFlag> {
33  
34    /**
35     * Create a new bound flag instance.
36     *
37     * @param field
38     *          the Java field the instance is bound to
39     * @param containingDefinition
40     *          the definition containing the instance
41     * @return the new instance
42     */
43    @NonNull
44    static IBoundInstanceFlag newInstance(
45        @NonNull Field field,
46        @NonNull IBoundDefinitionModel<IBoundObject> containingDefinition) {
47      return new InstanceFlagInline(field, containingDefinition);
48    }
49  
50    /**
51     * Determines if this flag's value is used as the property name for the JSON
52     * object that holds the remaining data based on this flag's containing
53     * definition.
54     *
55     * @return {@code true} if this flag is used as a JSON key, or {@code false}
56     *         otherwise
57     */
58    boolean isJsonKey();
59  
60    /**
61     * Determines if this flag is used as a JSON "value key". A "value key" is a
62     * flag who's value is used as the property name for the containing objects
63     * value.
64     *
65     * @return {@code true} if the flag is used as a JSON "value key", or
66     *         {@code false} otherwise
67     */
68    boolean isJsonValueKey();
69  
70    // Flag Instance Features
71    // ======================
72  
73    @Override
74    @NonNull
75    IBoundDefinitionModel<IBoundObject> getContainingDefinition();
76  
77    @Override
78    @NonNull
79    default IBoundDefinitionModel<IBoundObject> getParentContainer() {
80      return getContainingDefinition();
81    }
82  
83    /**
84     * {@inheritDoc}
85     * <p>
86     * For an inline instance, this instance is the definition.
87     */
88    @Override
89    @NonNull
90    IBoundDefinitionFlag getDefinition();
91  
92    @Override
93    @NonNull
94    default IBoundInstanceFlag getInlineInstance() {
95      // always inline
96      return this;
97    }
98  
99    @Override
100   default void deepCopy(@NonNull IBoundObject fromInstance, @NonNull IBoundObject toInstance) throws BindingException {
101     Object value = getValue(fromInstance);
102     if (value != null) {
103       setValue(toInstance, deepCopyItem(value, toInstance));
104     }
105   }
106 
107   @Override
108   @NonNull
109   default Object readItem(IBoundObject parent, @NonNull IItemReadHandler handler) throws IOException {
110     return handler.readItemFlag(ObjectUtils.requireNonNull(parent, "parent"), this);
111   }
112 
113   @Override
114   default void writeItem(Object item, IItemWriteHandler handler) throws IOException {
115     handler.writeItemFlag(item, this);
116   }
117 
118   @Override
119   default boolean canHandleXmlQName(@NonNull QName qname) {
120     return qname.equals(getXmlQName());
121   }
122 }