001/*
002 * SPDX-FileCopyrightText: none
003 * SPDX-License-Identifier: CC0-1.0
004 */
005
006package dev.metaschema.core.model;
007
008import dev.metaschema.core.model.util.ModuleUtils;
009import dev.metaschema.core.util.ObjectUtils;
010import edu.umd.cs.findbugs.annotations.NonNull;
011
012/**
013 * A base class for name members of a containing model.
014 *
015 * @param <PARENT>
016 *          the Java type of the parent model container for this instance
017 * @param <PARENT_DEFINITION>
018 *          the Java type of the containing assembly definition
019 */
020public abstract class AbstractNamedModelInstance<
021    PARENT extends IContainerModel,
022    PARENT_DEFINITION extends IAssemblyDefinition>
023    extends AbstractNamedInstance<PARENT>
024    implements INamedModelInstance {
025
026  /**
027   * Construct a new instance.
028   *
029   * @param parent
030   *          the parent containing the instance
031   */
032  protected AbstractNamedModelInstance(@NonNull PARENT parent) {
033    super(parent, name -> ModuleUtils.parseModelName(parent.getOwningDefinition().getContainingModule(), name));
034  }
035
036  @Override
037  public final PARENT_DEFINITION getContainingDefinition() {
038    // TODO: look for ways to avoid this cast. The problem is that IContainerModel
039    // is not easily generalized, since this interface is extended by core model
040    // interfaces. Perhaps moving default implementation into abstract or concrete
041    // implementation is a possible path?
042    return ObjectUtils.asType(getParentContainer().getOwningDefinition());
043  }
044
045  @Override
046  public IModule getContainingModule() {
047    return getContainingDefinition().getContainingModule();
048  }
049}