1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.model;
7   
8   import java.util.function.Predicate;
9   
10  import javax.xml.namespace.QName;
11  
12  import edu.umd.cs.findbugs.annotations.NonNull;
13  import edu.umd.cs.findbugs.annotations.Nullable;
14  
15  public interface INamedModelInstance extends IModelInstance, INamedInstance {
16  
17    /**
18     * Tests if the provided instance represents complex data. The data is complex
19     * if one of the following is true:
20     * <ul>
21     * <li>The instance is a {@link IAssemblyInstance}.</li>
22     * <li>The instance is a {@link IFieldInstance} that has flags.</li>
23     * </ul>
24     *
25     * This method can be used as a {@link Predicate}.
26     *
27     * @param instance
28     *          the instance to test
29     * @return {@code true} if the data is complex, or {@code false} otherwise
30     */
31    static boolean complexObjectFilter(INamedModelInstance instance) {
32      boolean retval = true;
33      if (instance instanceof IFieldInstance) {
34        IFieldInstance field = (IFieldInstance) instance;
35        retval = !field.getDefinition().getFlagInstances().isEmpty();
36      }
37      return retval;
38    }
39  
40    @Override
41    @NonNull
42    IModelDefinition getDefinition();
43  
44    /**
45     * Indicates if a flag's value can be used as a property name in the containing
46     * object in JSON who's value will be the object containing the flag. In such
47     * cases, the flag will not appear in the object. This is only allowed if the
48     * flag is required, as determined by a {@code true} result from
49     * {@link IFlagInstance#isRequired()}. The {@link IFlagInstance} can be
50     * retrieved using {@link #getEffectiveJsonKey()}.
51     *
52     * @return {@code true} if the flag's value can be used as a property name, or
53     *         {@code false} otherwise
54     * @see #getEffectiveJsonKey()
55     */
56    // TODO: remove once moved to the instance side
57    default boolean hasJsonKey() {
58      return getEffectiveJsonKey() != null;
59    }
60  
61    /**
62     * Get the JSON key flag instance for this model instance, if one is configured.
63     *
64     * @return the JSON key flag instance or {@code null} if a JSON key is
65     *         configured
66     */
67    @Nullable
68    IFlagInstance getEffectiveJsonKey();
69  
70    /**
71     * Get the JSON key associated with this instance.
72     *
73     * @return the configured JSON key or {@code null} if no JSON key is configured
74     */
75    @Nullable
76    IFlagInstance getJsonKey();
77  
78    @Override
79    default QName getReferencedDefinitionQName() {
80      return getContainingModule().toModelQName(getName());
81    }
82  
83    @Override
84    default QName getXmlQName() {
85      return getContainingModule().toModelQName(getEffectiveName());
86    }
87  }