001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.core.model;
007
008import java.util.Collection;
009import java.util.function.Predicate;
010
011import edu.umd.cs.findbugs.annotations.NonNull;
012import edu.umd.cs.findbugs.annotations.Nullable;
013
014/**
015 * Represents a Metaschema definition for a complex object that may contain
016 * flags.
017 */
018public interface IModelDefinition extends IDefinition, IContainer {
019  /**
020   * Tests if the provided definition represents complex data. The data is complex
021   * if one of the following is true:
022   * <ul>
023   * <li>The instance is a {@link IAssemblyDefinition}.
024   * <li>The instance is a {@link IFieldDefinition} that has flags.
025   * </ul>
026   *
027   * This method can be used as a {@link Predicate}.
028   *
029   * @param definition
030   *          the definition to test
031   * @return {@code true} if the data is complex, or {@code false} otherwise
032   */
033  static boolean complexObjectFilter(IModelDefinition definition) {
034    boolean retval = true;
035    if (definition instanceof IFieldDefinition) {
036      IFieldDefinition field = (IFieldDefinition) definition;
037      retval = !field.getFlagInstances().isEmpty();
038    }
039    return retval;
040  }
041
042  @Override
043  default boolean hasChildren() {
044    return !getFlagInstances().isEmpty();
045  }
046
047  /**
048   * Retrieves a flag instance, by the flag's effective name-based qualified name
049   * index.
050   *
051   * @param index
052   *          the flag's name-based qualified name index
053   * @return the matching flag instance, or {@code null} if there is no flag
054   *         matching the specified name
055   */
056  @Nullable
057  IFlagInstance getFlagInstanceByName(@NonNull Integer index);
058
059  /**
060   * Retrieves the flag instances for all flags defined on the containing
061   * definition.
062   *
063   * @return the flags
064   */
065  @NonNull
066  Collection<? extends IFlagInstance> getFlagInstances();
067
068  /**
069   * Retrieves the flag instance to use as the property name for the containing
070   * object in JSON whose value will be the object containing the flag.
071   *
072   * @return the flag instance if a JSON key is configured, or {@code null}
073   *         otherwise
074   */
075  // TODO: remove once moved to the instance side
076  @Nullable
077  IFlagInstance getJsonKey();
078}