1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.core.metapath;
7   
8   import gov.nist.secauto.metaschema.core.metapath.item.IItem;
9   import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10  
11  import java.util.stream.Stream;
12  
13  import edu.umd.cs.findbugs.annotations.NonNull;
14  
15  public interface ICollectionValue {
16    /**
17     * Get the collection value as a sequence.
18     * <p>
19     * If the collection value is a sequence, then the sequence is returned.
20     *
21     * @return the resulting sequence
22     */
23    // TODO: rename to toSequence and resolve conflicting methods?
24    @NonNull
25    ISequence<?> asSequence();
26  
27    /**
28     * Get the stream of items for the collection value.
29     * <p>
30     * If the collection value is a sequence, then the items in the collection are
31     * returned.
32     *
33     * @param value
34     *          the collection value
35     * @return the sequence of related items
36     */
37    @NonNull
38    static Stream<? extends IItem> normalizeAsItems(@NonNull ICollectionValue value) {
39      return value instanceof IItem
40          ? ObjectUtils.notNull(Stream.of((IItem) value))
41          : value.asSequence().stream();
42    }
43  
44    /**
45     * Get the stream of items for the collection value.
46     * <p>
47     * If the collection value contains items, then these items are returned.
48     *
49     * @return a stream of related items
50     */
51    @NonNull
52    Stream<? extends IItem> flatten();
53  }