1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.format;
7   
8   import java.util.List;
9   import java.util.stream.Collectors;
10  import java.util.stream.Stream;
11  
12  import dev.metaschema.core.metapath.item.node.INodeItem;
13  import dev.metaschema.core.model.IMetapathQueryable;
14  import edu.umd.cs.findbugs.annotations.NonNull;
15  
16  /**
17   * A named segment of a path that can be formatted.
18   */
19  public interface IPathSegment extends IMetapathQueryable {
20    /**
21     * Get the path for this node item using the provided formatter.
22     *
23     * @param formatter
24     *          the path formatter to use to produce the path
25     *
26     * @return the formatted path
27     */
28    @NonNull
29    default String toPath(@NonNull IPathFormatter formatter) {
30      return formatter.format(this);
31    }
32  
33    /**
34     * Apply formatting for the path segment. This is a visitor pattern that will be
35     * called to format each segment in a larger path.
36     *
37     * @param formatter
38     *          the path formatter
39     * @return a textual representation of the path segment
40     */
41    @NonNull
42    String format(@NonNull IPathFormatter formatter);
43  
44    /**
45     * Get a list of path segments, starting at the root and descending.
46     *
47     * @return a list of path segments in descending order
48     */
49    @SuppressWarnings("null")
50    @NonNull
51    default List<IPathSegment> getPath() {
52      return getPathStream().collect(Collectors.toUnmodifiableList());
53    }
54  
55    /**
56     * Get a stream of path segments, starting at the root and descending.
57     *
58     * @return a stream of path segments in descending order
59     */
60    @NonNull
61    Stream<? extends IPathSegment> getPathStream();
62  
63    /**
64     * Get the value associated with the path segment.
65     *
66     * @return the value or {@code null} if no value is associated with this path
67     *         segment
68     */
69    @Override
70    INodeItem getNodeItem();
71  }