001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.core.model;
007
008import java.util.Locale;
009
010import dev.metaschema.core.datatype.markup.MarkupLine;
011import dev.metaschema.core.qname.IEnhancedQName;
012import dev.metaschema.core.util.ObjectUtils;
013import edu.umd.cs.findbugs.annotations.NonNull;
014import edu.umd.cs.findbugs.annotations.Nullable;
015
016/**
017 * A trait indicating that the implementation is a localized definition that is
018 * declared in-line as an instance.
019 *
020 * @param <DEFINITION>
021 *          the associated definition Java type
022 * @param <INSTANCE>
023 *          the associated instance Java type
024 */
025public interface IFeatureDefinitionInstanceInlined<
026    DEFINITION extends IDefinition,
027    INSTANCE extends INamedInstance>
028    extends IDefinition, INamedInstance {
029
030  @Override
031  default IEnhancedQName getDefinitionQName() {
032    return getReferencedDefinitionQName();
033  }
034
035  @Override
036  default DEFINITION getDefinition() {
037    return ObjectUtils.asType(this);
038  }
039
040  @Override
041  default boolean isInlineDefinition() {
042    return true;
043  }
044
045  @Override
046  default boolean isInline() {
047    // has to be inline
048    return true;
049  }
050
051  @Override
052  @NonNull
053  default INSTANCE getInlineInstance() {
054    return ObjectUtils.asType(this);
055  }
056
057  @Override
058  default String getEffectiveFormalName() {
059    return getFormalName();
060  }
061
062  @Override
063  default MarkupLine getEffectiveDescription() {
064    return getDescription();
065  }
066
067  @Override
068  default String getEffectiveName() {
069    // don't use use-name
070    return getName();
071  }
072
073  @Override
074  default Integer getEffectiveIndex() {
075    return getIndex();
076  }
077
078  @Override
079  @Nullable
080  default Object getEffectiveDefaultValue() {
081    // This is an inline instance that is both a definition and an instance. Don't
082    // delegate to the definition, since this would be redundant.
083    return getDefaultValue();
084  }
085
086  @Override
087  default ISource getSource() {
088    return getContainingModule().getSource();
089  }
090
091  /**
092   * Generates a "coordinate" string for the provided inline definition instance.
093   *
094   * A coordinate consists of the element's:
095   * <ul>
096   * <li>containing Metaschema module's short name
097   * <li>model type
098   * <li>definition name
099   * <li>hash code
100   * </ul>
101   *
102   * @return the coordinate
103   */
104  @SuppressWarnings("null")
105  @Override
106  default String toCoordinates() {
107    IModule module = getContainingModule();
108    return String.format("%s-inline-definition:%s:%s/%s@%d",
109        getModelType().toString().toLowerCase(Locale.ROOT),
110        module.getShortName(),
111        getContainingDefinition().getName(),
112        getName(),
113        hashCode());
114  }
115}