rootMostValueMatching method

T? rootMostValueMatching(
  1. Path relativePath,
  2. bool predicate(
    1. T value
    )
)

The root-most value on relativePath matching predicate, or null.

Implementation

T? rootMostValueMatching(
  Path relativePath,
  bool Function(T value) predicate,
) {
  final T? value = this.value;
  if (value != null && predicate(value)) {
    return value;
  }
  ImmutableTree<T> current = this;
  for (final ChildKey key in relativePath.segments) {
    final ImmutableTree<T>? next = current.children[key];
    if (next == null) {
      return null;
    }
    final T? nextValue = next.value;
    if (nextValue != null && predicate(nextValue)) {
      return nextValue;
    }
    current = next;
  }
  return null;
}