1   
2   package dev.metaschema.core.metapath.item.node;
3   
4   import dev.metaschema.core.model.IDefinition;
5   import dev.metaschema.core.model.INamedInstance;
6   import dev.metaschema.core.model.IResourceLocation;
7   import dev.metaschema.core.qname.IEnhancedQName;
8   import edu.umd.cs.findbugs.annotations.NonNull;
9   import edu.umd.cs.findbugs.annotations.Nullable;
10  
11  /**
12   * Represents a Metapath node item that is based on an underlying Metaschema
13   * module definition and instance.
14   *
15   * @param <D>
16   *          the Java type of the definition associated with a Metaschema module
17   * @param <I>
18   *          the Java type of the instance associated with a Metaschema module
19   */
20  public interface IDefinitionNodeItem<D extends IDefinition, I extends INamedInstance> extends INodeItem {
21    /**
22     * Get the name of this node.
23     *
24     * @return the qualified name
25     */
26    @NonNull
27    default IEnhancedQName getQName() {
28      I instance = getInstance();
29      return instance == null
30          ? getDefinition().getQName()
31          : instance.getQName();
32    }
33  
34    /**
35     * Get the Metaschema definition associated with this node.
36     *
37     * @return the definition
38     */
39    @NonNull
40    D getDefinition();
41  
42    /**
43     * Retrieve the instance associated with this path segment.
44     *
45     * @return the instance of the segment, or {@code null} if it doesn't have one
46     */
47    I getInstance();
48  
49    @Override
50    @Nullable
51    default IResourceLocation getLocation() {
52      Object value = getValue();
53      return value == null ? null : getDefinition().getLocation(value);
54    }
55  }