All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.internal.tasks.DefaultRealizableTaskCollection Maven / Gradle / Ivy

There is a newer version: 8.11.1
Show newest version
/*
 * 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.api.internal.tasks;

import groovy.lang.Closure;
import org.gradle.api.Action;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Task;
import org.gradle.api.UnknownTaskException;
import org.gradle.api.internal.DelegatingNamedDomainObjectSet;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.TaskCollection;
import org.gradle.api.tasks.TaskProvider;
import org.gradle.internal.Cast;
import org.gradle.internal.reflect.Instantiator;
import org.gradle.model.internal.core.ModelNode;
import org.gradle.model.internal.core.MutableModelNode;
import org.gradle.model.internal.type.ModelType;

import java.util.concurrent.atomic.AtomicBoolean;

public class DefaultRealizableTaskCollection extends DelegatingNamedDomainObjectSet implements TaskCollection, TaskDependencyContainer {

    private final Class type;
    private final AtomicBoolean realized = new AtomicBoolean();
    private final MutableModelNode modelNode;
    private final Instantiator instantiator;

    public DefaultRealizableTaskCollection(Class type, TaskCollection delegate, MutableModelNode modelNode, Instantiator instantiator) {
        super(delegate);
        assert !(delegate instanceof DefaultRealizableTaskCollection) : "Attempt to wrap already realizable task collection in realizable wrapper: " + delegate;

        this.type = type;
        this.modelNode = modelNode;
        this.instantiator = instantiator;
    }

    @Override
    public void visitDependencies(TaskDependencyResolveContext context) {
        // Task dependencies may be calculated more than once.
        // This guard is purely an optimisation.
        if (modelNode != null && realized.compareAndSet(false, true)) {
            modelNode.ensureAtLeast(ModelNode.State.SelfClosed);
            for (MutableModelNode node : modelNode.getLinks(ModelType.of(type))) {
                node.ensureAtLeast(ModelNode.State.GraphClosed);
            }
        }
        for (T t : this) {
            context.add(t);
        }
    }

    @Override
    protected TaskCollection getDelegate() {
        return (TaskCollection) super.getDelegate();
    }

    private  TaskCollection realizable(Class type, TaskCollection collection) {
        return Cast.uncheckedCast(instantiator.newInstance(DefaultRealizableTaskCollection.class, type, collection, modelNode, instantiator));
    }

    @Override
    public TaskCollection named(Spec nameFilter) {
        return realizable(type, getDelegate().named(nameFilter));
    }

    @Override
    public TaskCollection matching(Spec spec) {
        return realizable(type, getDelegate().matching(spec));
    }

    @Override
    public TaskCollection matching(Closure closure) {
        return realizable(type, getDelegate().matching(closure));
    }

    @Override
    public  TaskCollection withType(Class type) {
        return realizable(type, getDelegate().withType(type));
    }

    @Override
    public Action whenTaskAdded(Action action) {
        return getDelegate().whenTaskAdded(action);
    }

    @Override
    public void whenTaskAdded(Closure closure) {
        getDelegate().whenTaskAdded(closure);
    }

    @Override
    public TaskProvider named(String name) throws InvalidUserDataException {
        return getDelegate().named(name);
    }

    @Override
    public TaskProvider named(String name, Action configurationAction) throws UnknownTaskException {
        return getDelegate().named(name, configurationAction);
    }

    @Override
    public  TaskProvider named(String name, Class type) throws UnknownTaskException {
        return getDelegate().named(name, type);
    }

    @Override
    public  TaskProvider named(String name, Class type, Action configurationAction) throws UnknownTaskException {
        return getDelegate().named(name, type, configurationAction);
    }
}