<commit_before>/*
 * Copyright (C) 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.xquery.functional;

import com.intellij.psi.PsiElement;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public final class PsiTreeWalker {
    public void each(Consumer<PsiElement> consumer) {
        PsiElement element = mElement;
        while (element != null) {
            consumer.accept(element);
            element = mNextElementFunction.apply(element);
        }
    }

    public Option<PsiElement> find(Predicate<PsiElement> matcher) {
        PsiElement element = mElement;
        while (element != null) {
            if (matcher.test(element)) {
                return Option.some(element);
            }
            element = mNextElementFunction.apply(element);
        }
        return Option.none();
    }

    // region Value Constructors

    public static PsiTreeWalker ancestors(PsiElement element) {
        return new PsiTreeWalker(element.getParent(), PsiElement::getParent);
    }

    public static PsiTreeWalker descendants(PsiElement element) {
        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getFirstChild);
    }

    public static PsiTreeWalker children(PsiElement element) {
        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getNextSibling);
    }<commit_msg>Split the PsiTreeWalker find function into findFirst and findAll.<commit_after>/*
 * Copyright (C) 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.xquery.functional;

import com.intellij.psi.PsiElement;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public final class PsiTreeWalker {
    public void each(Consumer<PsiElement> consumer) {
        PsiElement element = mElement;
        while (element != null) {
            consumer.accept(element);
            element = mNextElementFunction.apply(element);
        }
    }

    public Option<PsiElement> findFirst(Predicate<PsiElement> matcher) {
        PsiElement element = mElement;
        while (element != null) {
            if (matcher.test(element)) {
                return Option.some(element);
            }
            element = mNextElementFunction.apply(element);
        }
        return Option.none();
    }

    public void findAll(Predicate<PsiElement> matcher, Consumer<PsiElement> consumer) {
        PsiElement element = mElement;
        while (element != null) {
            if (matcher.test(element)) {
                consumer.accept(element);
            }
            element = mNextElementFunction.apply(element);
        }
    }

    // region Value Constructors

    public static PsiTreeWalker ancestors(PsiElement element) {
        return new PsiTreeWalker(element.getParent(), PsiElement::getParent);
    }

    public static PsiTreeWalker descendants(PsiElement element) {
        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getFirstChild);
    }

    public static PsiTreeWalker children(PsiElement element) {
        return new PsiTreeWalker(element.getFirstChild(), PsiElement::getNextSibling);
    }
{'fraction_non_alphanumeric': 0.08833839682168731, 'fraction_numerical': 0.0037391913998597805, 'mean_word_length': 3.099616858237548, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5835308', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1107, 'n_words': 341}