Add a simple Binding query language
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / query / QueryResult.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.api.query;
9
10 import com.google.common.annotations.Beta;
11 import java.util.List;
12 import java.util.Spliterator;
13 import java.util.stream.Collectors;
14 import java.util.stream.Stream;
15 import java.util.stream.StreamSupport;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18
19 /**
20  * Result of executing a {@link QueryExpression}. It is composed of one or more result values, which can be accessed via
21  * {@link #spliterator()}, {@link #stream()} and {@link #getValues()} methods.
22  *
23  * @param <T> Result object type
24  */
25 @Beta
26 public interface QueryResult<T extends DataObject> {
27     /**
28      * Returns a spliterator over values of the result.
29      *
30      * @return Returns the a spliterator which visits query results.
31      */
32     // TODO: @throws IllegalStateException if values have been already been consumed?
33     // FIXME: we really may want to wrap each entry in a CheckedValue, so that we can communicate fetch problems
34     @NonNull Spliterator<? extends T> spliterator();
35
36     /**
37      * Returns a sequential {@link Stream} of values from the result.
38      *
39      * @return A stream of non-null values.
40      */
41     default @NonNull Stream<? extends T> stream() {
42         return StreamSupport.stream(spliterator(), false);
43     }
44
45     /**
46      * Returns a parallel {@link Stream} of values from the result.
47      *
48      * @return A stream of non-null values.
49      */
50     default @NonNull Stream<? extends T> parallelStream() {
51         return StreamSupport.stream(spliterator(), true);
52     }
53
54     default @NonNull List<? extends T> getValues() {
55         return stream().collect(Collectors.toUnmodifiableList());
56     }
57 }