1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package dev.metaschema.core.metapath.cst.path;
7   
8   import java.util.Collections;
9   import java.util.List;
10  import java.util.stream.Stream;
11  
12  import dev.metaschema.core.metapath.IExpression;
13  import dev.metaschema.core.metapath.item.node.INodeItem;
14  import dev.metaschema.core.util.ObjectUtils;
15  import edu.umd.cs.findbugs.annotations.NonNull;
16  
17  /**
18   * A Metapath path expression that tests a node based on a set of conditions.
19   * <p>
20   * Based on the XPath 3.1
21   * <a href="https://www.w3.org/TR/xpath-31/#node-tests">node test</a> syntax.
22   */
23  public interface INodeTestExpression extends IExpression {
24    @SuppressWarnings("null")
25    @Override
26    default List<? extends IExpression> getChildren() {
27      return Collections.emptyList();
28    }
29  
30    @Override
31    default Class<INodeItem> getBaseResultType() {
32      return INodeItem.class;
33    }
34  
35    @Override
36    default Class<INodeItem> getStaticResultType() {
37      return getBaseResultType();
38    }
39  
40    /**
41     * Check the provided stream of items to determine if each item matches this
42     * test. All items that match are returned.
43     * <p>
44     * This is an intermediate stream operation.
45     *
46     * @param <T>
47     *          the item Java type
48     * @param stream
49     *          the items to check if they match
50     * @return the matching items
51     */
52    @NonNull
53    default <T extends INodeItem> Stream<T> filterStream(@NonNull Stream<T> stream) {
54      return ObjectUtils.notNull(stream.filter(this::match));
55    }
56  
57    /**
58     * Check the provided item to determine if it matches this test.
59     *
60     * @param item
61     *          the item to check for a match
62     * @return {@code true} if the item matches or {@code false} otherwise
63     */
64    boolean match(@NonNull INodeItem item);
65  }