Fix some null warnings
[deliverable/tracecompass.git] / common / org.eclipse.tracecompass.common.core / src / org / eclipse / tracecompass / common / core / collect / StreamFlattener.java
1 /*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.common.core.collect;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.function.Function;
15 import java.util.stream.Stream;
16
17 /**
18 * Generic utility class to "flatten" a data structure using the {@link Stream}
19 * API.
20 *
21 * @author Alexandre Montplaisir
22 *
23 * @param <T>
24 * The type of container, or "node" in the tree
25 * @since 2.0
26 */
27 public class StreamFlattener<T> {
28
29 private final Function<T, Stream<T>> fGetChildrenFunction;
30
31 /**
32 * Constructor
33 *
34 * @param getChildrenFunction
35 * The function to use to get each element's children. Should
36 * return a {@link Stream} of those children.
37 */
38 public StreamFlattener(Function<T, Stream<T>> getChildrenFunction) {
39 fGetChildrenFunction = getChildrenFunction;
40 }
41
42 /**
43 * Do an in-order flattening of the data structure, starting at the given
44 * element (or node).
45 *
46 * @param element
47 * The tree node or similar from which to start
48 * @return A unified Stream of all the children that were found,
49 * recursively.
50 */
51 public Stream<T> flatten(T element) {
52 Stream<T> ret = Stream.concat(
53 Stream.of(element),
54 fGetChildrenFunction.apply(element).flatMap(this::flatten));
55 return checkNotNull(ret);
56 }
57 }
This page took 0.031472 seconds and 5 git commands to generate.