/*
 * Copyright 2013 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.execution.plan;


import com.google.common.collect.Maps;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.component.BuildIdentifier;
import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.api.internal.GradleInternal;
import org.gradle.api.internal.TaskInternal;
import org.gradle.api.internal.plugins.PluginManagerInternal;
import org.gradle.api.plugins.PluginContainer;
import org.gradle.composite.internal.IncludedBuildTaskGraph;
import org.gradle.internal.Cast;
import org.gradle.internal.build.BuildState;
import org.gradle.internal.execution.WorkValidationContext;
import org.gradle.internal.execution.impl.DefaultWorkValidationContext;
import org.gradle.plugin.use.PluginId;
import org.gradle.plugin.use.internal.DefaultPluginId;

import javax.annotation.Nullable;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class TaskNodeFactory {
    private final Map<Task, TaskNode> nodes = new HashMap<>();
    private final IncludedBuildTaskGraph taskGraph;
    private final GradleInternal thisBuild;
    private final BuildIdentifier currentBuildId;
    private final DocumentationRegistry documentationRegistry;
    private final DefaultTypeOriginInspectorFactory typeOriginInspectorFactory;

    public TaskNodeFactory(GradleInternal thisBuild, IncludedBuildTaskGraph taskGraph) {
        this.thisBuild = thisBuild;
        this.currentBuildId = thisBuild.getServices().get(BuildState.class).getBuildIdentifier();
        this.documentationRegistry = thisBuild.getServices().get(DocumentationRegistry.class);
        this.taskGraph = taskGraph;
        this.typeOriginInspectorFactory = new DefaultTypeOriginInspectorFactory();
    }

    public Set<Task> getTasks() {
        return nodes.keySet();
    }

    public TaskNode getOrCreateNode(Task task) {
        TaskNode node = nodes.get(task);
        if (node == null) {
            if (task.getProject().getGradle() == thisBuild) {
                node = new LocalTaskNode((TaskInternal) task, new DefaultWorkValidationContext(documentationRegistry, typeOriginInspectorFactory.forTask(task)));
            } else {
                node = TaskInAnotherBuild.of((TaskInternal) task, currentBuildId, taskGraph);
            }
            nodes.put(task, node);
        }
        return node;
    }

    public void clear() {
        nodes.clear();
    }

    private static class DefaultTypeOriginInspectorFactory {
        private final Map<Project, ProjectScopedTypeOriginInspector> projectToInspector = Maps.newConcurrentMap();
        private final Map<Class<?>, File> clazzToFile = Maps.newConcurrentMap();

        public ProjectScopedTypeOriginInspector forTask(Task task) {
            return projectToInspector.computeIfAbsent(task.getProject(), ProjectScopedTypeOriginInspector::new);
        }

        @Nullable
        private File jarFileFor(Class<?> pluginClass) {
            return clazzToFile.computeIfAbsent(pluginClass, clazz -> toFile(pluginClass.getProtectionDomain().getCodeSource().getLocation()));
        }

        @Nullable
        private static File toFile(@Nullable URL url) {
            if (url == null) {
                return null;
            }
            try {
                return new File(url.toURI());
            } catch (URISyntaxException e) {
                return null;
            }
        }

        private class ProjectScopedTypeOriginInspector implements WorkValidationContext.TypeOriginInspector {
            private final PluginContainer plugins;
            private final PluginManagerInternal pluginManager;
            private final Map<Class<?>, Optional<PluginId>> classToPlugin = Maps.newConcurrentMap();

            private ProjectScopedTypeOriginInspector(Project project) {
                this.plugins = project.getPlugins();
                this.pluginManager = (PluginManagerInternal) project.getPluginManager();
            }

            @Override
            public Optional<PluginId> findPluginDefining(Class<?> type) {
                return classToPlugin.computeIfAbsent(type, clazz -> {
                    File taskJar = jarFileFor(type);
                    Optional<Plugin<?>> pluginForTask = Cast.uncheckedCast(plugins.stream()
                        .filter(plugin -> Objects.equals(jarFileFor(plugin.getClass()), taskJar))
                        .findFirst());
                    if (pluginForTask.isPresent()) {
                        Optional<PluginId> pluginIdForClass = pluginManager.findPluginIdForClass(pluginForTask.get().getClass());
                        if (pluginIdForClass.isPresent()) {
                            return pluginIdForClass;
                        }
                        return Optional.of(new DefaultPluginId(pluginForTask.get().getClass().getName()));
                    }
                    return Optional.empty();
                });
            }
        }
    }
}

@@ -4979,49 +4979,94 @@
     
-Optional%3CPlugin%3C?%3E%3E pluginForTask = Cast.
+return plugins.stream()%0A                        .map(plugin -%3E Cast.%3CClass%3CPlugin%3C?%3E%3E%3E
 unch
@@ -5070,16 +5070,23 @@
 nchecked
+Nonnull
 Cast(plu
@@ -5084,33 +5084,36 @@
 lCast(plugin
-s.stream(
+.getClass())
 )%0A          
@@ -5136,24 +5136,28 @@
 ilter(plugin
+Type
  -%3E Objects.
@@ -5180,27 +5180,20 @@
 r(plugin
-.getClass()
+Type
 ), taskJ
@@ -5226,387 +5226,118 @@
    .
-findFirst());%0A                    if (pluginForTask.isPresent()) %7B%0A                        Optional%3CPluginId%3E pluginIdForClass = pluginManager.findPluginIdForClass(pluginForTask.get().getClass());%0A                        if (pluginIdForClass.isPresent()) %7B%0A                            return pluginIdForClass;%0A                        %7D%0A                        return Optional.of(
+map(pluginType -%3E pluginManager.findPluginIdForClass(pluginType)%0A                            .orElseGet(() -%3E 
 new 
@@ -5362,32 +5362,12 @@
 ugin
-ForTask.get().getClass()
+Type
 .get
@@ -5370,25 +5370,25 @@
 .getName()))
-;
+)
 %0A           
@@ -5400,51 +5400,22 @@
     
-%7D%0A                    return Optional.empty
+    .findFirst
 ();%0A

Simplify method body