/*
 * Copyright 2020 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.watch.vfs.impl;

import org.gradle.internal.snapshot.AtomicSnapshotHierarchyReference;
import org.gradle.internal.snapshot.SnapshotHierarchy;
import org.gradle.internal.vfs.impl.SnapshotCollectingDiffListener;

import javax.annotation.CheckReturnValue;
import java.util.function.Predicate;

public class DelegatingDiffCapturingUpdateFunctionDecorator implements SnapshotHierarchy.DiffCapturingUpdateFunctionDecorator {

    private final Predicate<String> watchFilter;
    private SnapshotHierarchy.SnapshotDiffListener snapshotDiffListener;
    private ErrorHandler errorHandler;

    public DelegatingDiffCapturingUpdateFunctionDecorator(Predicate<String> watchFilter) {
        this.watchFilter = watchFilter;
    }

    public void setSnapshotDiffListener(SnapshotHierarchy.SnapshotDiffListener snapshotDiffListener, ErrorHandler errorHandler) {
        this.snapshotDiffListener = snapshotDiffListener;
        this.errorHandler = errorHandler;
    }

    public void stopListening() {
        this.snapshotDiffListener = null;
        this.errorHandler = null;
    }

    @Override
    public AtomicSnapshotHierarchyReference.UpdateFunction decorate(SnapshotHierarchy.DiffCapturingUpdateFunction updateFunction) {
        SnapshotHierarchy.SnapshotDiffListener currentListener = snapshotDiffListener;
        if (currentListener == null) {
            return root -> updateFunction.update(root, SnapshotHierarchy.NodeDiffListener.NOOP);
        }

        SnapshotCollectingDiffListener diffListener = new SnapshotCollectingDiffListener(watchFilter);
        return root -> {
            SnapshotHierarchy newRoot = updateFunction.update(root, diffListener);
            return errorHandler.handleErrors(newRoot, () -> diffListener.publishSnapshotDiff(currentListener));
        };
    }

    public interface ErrorHandler {
        @CheckReturnValue
        SnapshotHierarchy handleErrors(SnapshotHierarchy currentRoot, Runnable runnable);
    }
}

@@ -1123,104 +1123,59 @@
 ate 
-SnapshotHierarchy.SnapshotDiffListener snapshotDiffListener;%0A    private ErrorHandler errorHandl
+ErrorHandlingDiffPublisher errorHandlingDiffPublish
 er;%0A
@@ -1460,201 +1460,178 @@
 his.
-snapshotDiffListener = snapshotDiffListener;%0A        this.errorHandler = errorHandler;%0A    %7D%0A%0A    public void stopListening() %7B%0A        this.snapshotDiffListener = null;%0A        this.errorHandl
+errorHandlingDiffPublisher = new ErrorHandlingDiffPublisher(snapshotDiffListener, errorHandler);%0A    %7D%0A%0A    public void stopListening() %7B%0A        errorHandlingDiffPublish
 er =
@@ -1802,112 +1802,142 @@
     
-SnapshotHierarchy.SnapshotDiffListener currentListener = snapshotDiffListener;%0A        if (currentListen
+ErrorHandlingDiffPublisher currentErrorHandlingDiffPublisher = errorHandlingDiffPublisher;%0A        if (currentErrorHandlingDiffPublish
 er =
@@ -2284,17 +2284,24 @@
  return 
-e
+currentE
 rrorHand
@@ -2305,49 +2305,22 @@
 andl
-er.handleErrors(newRoot, () -%3E diffListen
+ingDiffPublish
 er.p
@@ -2342,23 +2342,20 @@
 iff(
-current
+diff
 Listener
 ));%0A
@@ -2350,17 +2350,25 @@
 Listener
-)
+, newRoot
 );%0A     
@@ -2537,11 +2537,672 @@
 );%0A    %7D
+%0A%0A    private static class ErrorHandlingDiffPublisher %7B%0A        private final SnapshotHierarchy.SnapshotDiffListener diffListener;%0A        private final ErrorHandler errorHandler;%0A%0A        public ErrorHandlingDiffPublisher(SnapshotHierarchy.SnapshotDiffListener diffListener, ErrorHandler errorHandler) %7B%0A            this.diffListener = diffListener;%0A            this.errorHandler = errorHandler;%0A        %7D%0A%0A        public SnapshotHierarchy publishSnapshotDiff(SnapshotCollectingDiffListener collectedDiff, SnapshotHierarchy newRoot) %7B%0A            return errorHandler.handleErrors(newRoot, () -%3E collectedDiff.publishSnapshotDiff(diffListener));%0A        %7D%0A    %7D
 %0A%7D%0A

Fix race condition in DelegatingDiffCapturingUpdateFunctionDecorator