1   
2   package dev.metaschema.core.metapath.item.node;
3   
4   import dev.metaschema.core.util.ObjectUtils;
5   import edu.umd.cs.findbugs.annotations.Nullable;
6   
7   /**
8    * A common base class for node item implementations.
9    */
10  public abstract class AbstractNodeItem implements INodeItem {
11  
12    /**
13     * Generates a string signature for this node item in the format:
14     * {@code type\u2ABBlocation_metapath\u2ABC} or
15     * {@code type\u2ABBlocation_metapath\u2ABC(value)} where:
16     * <ul>
17     * <li>type: The node type signature
18     * <li>location_metapath: A Metapath for the node's location in the document
19     * <li>value: Optional value signature if a value is present
20     * </ul>
21     * The special characters \u2ABB and \u2ABC are used as delimiters to clearly
22     * separate the type from the location Metapath expression.
23     *
24     * @return the string signature of this node item
25     */
26    @SuppressWarnings("checkstyle:AvoidEscapedUnicodeCharacters")
27    @Override
28    public final String toSignature() {
29      StringBuilder builder = new StringBuilder()
30          .append(getType().toSignature())
31          .append('\u2ABB')
32          .append(getMetapath())
33          .append('\u2ABC');
34      String value = getValueSignature();
35      if (value != null) {
36        builder.append('(')
37            .append(value)
38            .append(')');
39      }
40      return ObjectUtils.notNull(builder.toString());
41    }
42  
43    /**
44     * Get the signature of this node's value.
45     *
46     * @return the value's signature or {@code null} if the node has no value
47     */
48    @Nullable
49    protected abstract String getValueSignature();
50  
51    @Override
52    public final String toString() {
53      return toSignature();
54    }
55  }