<commit_before>/*
 * Copyright 2015 the original author or authors.
 *
 * 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 org.gradle.internal.filewatch;

import org.gradle.api.Action;
import org.gradle.api.internal.file.FileSystemSubset;
import org.gradle.internal.BiAction;
import org.gradle.internal.UncheckedException;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class FileSystemChangeWaiter implements BiAction<FileSystemSubset, Runnable> {

    private final FileWatcherFactory fileWatcherFactory;

    public FileSystemChangeWaiter(FileWatcherFactory fileWatcherFactory) {
        this.fileWatcherFactory = fileWatcherFactory;
    }

    @Override
    public void execute(FileSystemSubset taskFileSystemInputs, Runnable notifier) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();

        FileWatcher watcher = fileWatcherFactory.watch(
            taskFileSystemInputs,
            new Action<Throwable>() {
                @Override
                public void execute(Throwable throwable) {
                    error.set(throwable);
                    latch.countDown();
                }
            },
            new FileWatcherListener() {
                @Override
                public void onChange(FileWatcher watcher, FileWatcherEvent event) {
                    watcher.stop();
                    latch.countDown();
                }
            }
        );

        try {
            notifier.run();
        } catch (Exception e) {
            watcher.stop();
            throw UncheckedException.throwAsUncheckedException(e);
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }

        Throwable throwable = error.get();
        if (throwable != null) {
            throw UncheckedException.throwAsUncheckedException(throwable);
        }

    }
}
<commit_msg>add firing delay to fix problems in tests when events fire when file change is in progress<commit_after>/*
 * Copyright 2015 the original author or authors.
 *
 * 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 org.gradle.internal.filewatch;

import org.gradle.api.Action;
import org.gradle.api.internal.file.FileSystemSubset;
import org.gradle.internal.BiAction;
import org.gradle.internal.UncheckedException;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class FileSystemChangeWaiter implements BiAction<FileSystemSubset, Runnable> {
    private static final long FIRING_DELAY_TIMEMILLIS = 250L;
    private final FileWatcherFactory fileWatcherFactory;

    public FileSystemChangeWaiter(FileWatcherFactory fileWatcherFactory) {
        this.fileWatcherFactory = fileWatcherFactory;
    }

    @Override
    public void execute(FileSystemSubset taskFileSystemInputs, Runnable notifier) {
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();

        FileWatcher watcher = fileWatcherFactory.watch(
            taskFileSystemInputs,
            new Action<Throwable>() {
                @Override
                public void execute(Throwable throwable) {
                    error.set(throwable);
                    latch.countDown();
                }
            },
            new FileWatcherListener() {
                @Override
                public void onChange(FileWatcher watcher, FileWatcherEvent event) {
                    watcher.stop();
                    latch.countDown();
                }
            }
        );

        try {
            notifier.run();
        } catch (Exception e) {
            watcher.stop();
            throw UncheckedException.throwAsUncheckedException(e);
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }

        Throwable throwable = error.get();
        if (throwable != null) {
            throw UncheckedException.throwAsUncheckedException(throwable);
        }

        try {
            Thread.sleep(FIRING_DELAY_TIMEMILLIS);
        } catch (InterruptedException e) {
            // ignore
        }
    }
}

{'fraction_non_alphanumeric': 0.07536764705882353, 'fraction_numerical': 0.003860294117647059, 'mean_word_length': 2.4198617221873038, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6965913', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1349, 'n_words': 386}