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 edu.umd.cs.findbugs.annotations.NonNull;
11  import edu.umd.cs.findbugs.annotations.Nullable;
12  
13  public interface INamedModelInstance extends IModelInstance, INamedInstance {
14  
15    /**
16     * Tests if the provided instance represents complex data. The data is complex
17     * if one of the following is true:
18     * <ul>
19     * <li>The instance is a {@link IAssemblyInstance}.</li>
20     * <li>The instance is a {@link IFieldInstance} that has flags.</li>
21     * </ul>
22     *
23     * This method can be used as a {@link Predicate}.
24     *
25     * @param instance
26     *          the instance to test
27     * @return {@code true} if the data is complex, or {@code false} otherwise
28     */
29    static boolean complexObjectFilter(INamedModelInstance instance) {
30      boolean retval = true;
31      if (instance instanceof IFieldInstance) {
32        IFieldInstance field = (IFieldInstance) instance;
33        retval = !field.getDefinition().getFlagInstances().isEmpty();
34      }
35      return retval;
36    }
37  
38    @Override
39    @NonNull
40    IModelDefinition getDefinition();
41  
42    /**
43     * Indicates if a flag's value can be used as a property name in the containing
44     * object in JSON who's value will be the object containing the flag. In such
45     * cases, the flag will not appear in the object. This is only allowed if the
46     * flag is required, as determined by a {@code true} result from
47     * {@link IFlagInstance#isRequired()}. The {@link IFlagInstance} can be
48     * retrieved using {@link #getEffectiveJsonKey()}.
49     *
50     * @return {@code true} if the flag's value can be used as a property name, or
51     *         {@code false} otherwise
52     * @see #getEffectiveJsonKey()
53     */
54    // TODO: remove once moved to the instance side
55    default boolean hasJsonKey() {
56      return getEffectiveJsonKey() != null;
57    }
58  
59    /**
60     * Get the JSON key flag instance for this model instance, if one is configured.
61     *
62     * @return the JSON key flag instance or {@code null} if a JSON key is
63     *         configured
64     */
65    @Nullable
66    IFlagInstance getEffectiveJsonKey();
67  
68    /**
69     * Get the JSON key associated with this instance.
70     *
71     * @return the configured JSON key or {@code null} if no JSON key is configured
72     */
73    @Nullable
74    IFlagInstance getJsonKey();
75  }