1 /*******************************************************************************
2 * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.common
.core
.collect
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.function
.Function
;
15 import java
.util
.stream
.Stream
;
18 * Generic utility class to "flatten" a data structure using the {@link Stream}
21 * @author Alexandre Montplaisir
24 * The type of container, or "node" in the tree
27 public class StreamFlattener
<T
> {
29 private final Function
<T
, Stream
<T
>> fGetChildrenFunction
;
34 * @param getChildrenFunction
35 * The function to use to get each element's children. Should
36 * return a {@link Stream} of those children.
38 public StreamFlattener(Function
<T
, Stream
<T
>> getChildrenFunction
) {
39 fGetChildrenFunction
= getChildrenFunction
;
43 * Do an in-order flattening of the data structure, starting at the given
47 * The tree node or similar from which to start
48 * @return A unified Stream of all the children that were found,
51 public Stream
<T
> flatten(T element
) {
52 Stream
<T
> ret
= Stream
.concat(
54 fGetChildrenFunction
.apply(element
).flatMap(this::flatten
));
55 return checkNotNull(ret
);
This page took 0.031472 seconds and 5 git commands to generate.