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 gov.nist.secauto.metaschema.core.model.util.ModuleUtils;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  
11  import edu.umd.cs.findbugs.annotations.NonNull;
12  
13  /**
14   * A base class for name members of a containing model.
15   *
16   * @param <PARENT>
17   *          the Java type of the parent model container for this instance
18   * @param <PARENT_DEFINITION>
19   *          the Java type of the containing assembly definition
20   */
21  public abstract class AbstractNamedModelInstance<
22      PARENT extends IContainerModel,
23      PARENT_DEFINITION extends IAssemblyDefinition>
24      extends AbstractNamedInstance<PARENT>
25      implements INamedModelInstance {
26  
27    /**
28     * Construct a new instance.
29     *
30     * @param parent
31     *          the parent containing the instance
32     */
33    protected AbstractNamedModelInstance(@NonNull PARENT parent) {
34      super(parent, name -> ModuleUtils.parseModelName(parent.getOwningDefinition().getContainingModule(), name));
35    }
36  
37    @Override
38    public final PARENT_DEFINITION getContainingDefinition() {
39      // TODO: look for ways to avoid this cast. The problem is that IContainerModel
40      // is not easily generalized, since this interface is extended by core model
41      // interfaces. Perhaps moving default implementation into abstract or concrete
42      // implementation is a possible path?
43      return ObjectUtils.asType(getParentContainer().getOwningDefinition());
44    }
45  
46    @Override
47    public IModule getContainingModule() {
48      return getContainingDefinition().getContainingModule();
49    }
50  }