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

org.gradle.model.internal.fixture.ModelActionBuilder 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.model.internal.fixture;

import org.gradle.api.Action;
import org.gradle.internal.BiAction;
import org.gradle.internal.TriAction;
import org.gradle.model.internal.core.*;
import org.gradle.model.internal.core.rule.describe.ModelRuleDescriptor;
import org.gradle.model.internal.core.rule.describe.SimpleModelRuleDescriptor;
import org.gradle.model.internal.type.ModelType;

import java.util.Collections;
import java.util.List;

public class ModelActionBuilder {

    private static final List> NO_REFS = Collections.emptyList();

    private ModelPath path;
    private ModelType type;
    private ModelRuleDescriptor descriptor;

    private ModelActionBuilder(ModelPath path, ModelType type, ModelRuleDescriptor descriptor) {
        this.path = path;
        this.type = type;
        this.descriptor = descriptor;
    }

    public static ModelActionBuilder of() {
        return new ModelActionBuilder(null, ModelType.UNTYPED, new SimpleModelRuleDescriptor("testrule"));
    }

    private  ModelActionBuilder copy(ModelType type) {
        return new ModelActionBuilder(path, type, descriptor);
    }

    public ModelActionBuilder path(String path) {
        return this.path(ModelPath.path(path));
    }

    public ModelActionBuilder path(ModelPath path) {
        this.path = path;
        return this;
    }

    public ModelActionBuilder descriptor(String descriptor) {
        return descriptor(new SimpleModelRuleDescriptor(descriptor));
    }

    public ModelActionBuilder descriptor(ModelRuleDescriptor descriptor) {
        this.descriptor = descriptor;
        return this;
    }

    public  ModelActionBuilder type(Class type) {
        return type(ModelType.of(type));
    }

    public  ModelActionBuilder type(ModelType type) {
        return copy(type);
    }

    public ModelAction action(final Action action) {
        return build(NO_REFS, new TriAction>>() {
            @Override
            public void execute(MutableModelNode mutableModelNode, T t, List> inputs) {
                action.execute(t);
            }
        });
    }

    public ModelAction node(final Action action) {
        return toAction(action, path, type, descriptor);
    }

    public  ModelAction action(ModelPath modelPath, Class inputType, BiAction action) {
        return action(modelPath, ModelType.of(inputType), action);
    }

    public  ModelAction action(String modelPath, Class inputType, BiAction action) {
        return action(ModelPath.path(modelPath), ModelType.of(inputType), action);
    }

    public  ModelAction action(ModelPath modelPath, ModelType inputType, BiAction action) {
        return action(modelPath, inputType, inputType.toString(), action);
    }

    public  ModelAction action(String modelPath, ModelType inputType, BiAction action) {
        return action(modelPath, inputType, modelPath, action);
    }

    public  ModelAction action(final ModelPath modelPath, final ModelType inputType, String referenceDescription, final BiAction action) {
        return action(ModelReference.of(modelPath, inputType, referenceDescription), action);
    }

    public  ModelAction action(final ModelReference inputReference, final BiAction action) {
        return build(Collections.>singletonList(inputReference), new TriAction>>() {
            @Override
            public void execute(MutableModelNode mutableModelNode, T t, List> inputs) {
                action.execute(t, ModelViews.assertType(inputs.get(0), inputReference.getType()).getInstance());
            }
        });
    }

    public  ModelAction action(final String modelPath, final ModelType inputType, String referenceDescription, final BiAction action) {
        return action(ModelPath.path(modelPath), inputType, referenceDescription, action);
    }

    public  ModelAction action(final ModelType inputType, final BiAction action) {
        return action((ModelPath) null, inputType, action);
    }

    public  ModelAction action(final Class inputType, final BiAction action) {
        return action(ModelType.of(inputType), action);
    }

    private ModelAction build(List> references, TriAction>> action) {
        return toAction(references, action, path, type, descriptor);
    }

    private static  ModelAction toAction(final List> references, final TriAction>> action, final ModelPath path, final ModelType type, final ModelRuleDescriptor descriptor) {
        return DirectNodeInputUsingModelAction.of(subject(path, type), descriptor, references, new TriAction>>() {
            @Override
            public void execute(MutableModelNode modelNode, T t, List> inputs) {
                action.execute(modelNode, t, inputs);
            }
        });
    }

    private static  ModelAction toAction(Action action, final ModelPath path, final ModelType type, final ModelRuleDescriptor descriptor) {
        return DirectNodeNoInputsModelAction.of(subject(path, type), descriptor, action);
    }

    private static  ModelReference subject(ModelPath path, ModelType type) {
        return path != null ? ModelReference.of(path, type) : ModelReference.of(type).inScope(ModelPath.ROOT);
    }
}