com.powsybl.afs.NodePath Maven / Gradle / Ivy
/**
* Copyright (c) 2017, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.afs;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* @author Geoffroy Jamgotchian
*/
public class NodePath {
private final List path;
private final Function, String> pathToString;
public NodePath(List path, Function, String> pathToString) {
this.path = Objects.requireNonNull(path);
this.pathToString = Objects.requireNonNull(pathToString);
}
public List toList() {
return path;
}
@Override
public String toString() {
return pathToString.apply(path);
}
private static > void addPath(N node, Predicate pathStop, List path) {
if (!pathStop.test(node)) {
addPath(node.getParent().orElse(null), pathStop, path);
}
path.add(node.getName());
}
public static > NodePath find(N node, Predicate pathStop, Function, String> pathToString) {
Objects.requireNonNull(node);
Objects.requireNonNull(pathStop);
Objects.requireNonNull(pathToString);
List path = new ArrayList<>(1);
addPath(node, pathStop, path);
return new NodePath(path, pathToString);
}
}