-
Notifications
You must be signed in to change notification settings - Fork 203
/
AbstractYamlTraversal.java
44 lines (38 loc) · 1.57 KB
/
AbstractYamlTraversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*******************************************************************************
* Copyright (c) 2017, 2018 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.ide.vscode.commons.yaml.path;
import java.util.stream.Stream;
import org.springframework.ide.vscode.commons.util.Streams;
/**
* Abstract superclass for implementing concrete {@link YamlTraversal}s.
* <p>
* Note that allthough this class provides a default implementation for both
* `traverse` and `traverseAmbiguously`, at least one of these methods <b>must<b>
* be overridden by the subclass (otherwise the methods will just call eachother
* in a infinite recursion loop).
* <p>
* To implement a non-ambiguous traversal, override the `traverse` method.
* <p>
* To implement an ambguous traversal, override the `traverseAmbiguously` method
* instead.
*
* @author Kris De Volder
*/
public abstract class AbstractYamlTraversal implements YamlTraversal {
@Override
public <T extends YamlNavigable<T>> Stream<T> traverseAmbiguously(T start) {
return Streams.fromNullable(traverse(start));
}
@Override
public <T extends YamlNavigable<T>> T traverse(T startNode) {
return traverseAmbiguously(startNode).findFirst().orElse(null);
}
}