/*
 * 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.core;

import org.gradle.internal.BiAction;
import org.gradle.internal.Cast;
import org.gradle.internal.TriAction;
import org.gradle.model.internal.core.rule.describe.ModelRuleDescriptor;

import java.util.Arrays;
import java.util.List;

public class DirectNodeInputUsingModelAction<T> extends AbstractModelActionWithView<T> {
    private final TriAction<? super MutableModelNode, ? super T, ? super List<ModelView<?>>> action;

    public DirectNodeInputUsingModelAction(ModelReference<T> subject, ModelRuleDescriptor descriptor, List<? extends ModelReference<?>> inputs,
                                           TriAction<? super MutableModelNode, ? super T, ? super List<ModelView<?>>> action) {
        super(subject, descriptor, inputs);
        this.action = action;
    }

    public static <T> DirectNodeInputUsingModelAction<T> of(ModelReference<T> modelReference, ModelRuleDescriptor descriptor, List<? extends ModelReference<?>> inputs,
                                                      TriAction<? super MutableModelNode, ? super T, ? super List<ModelView<?>>> action) {
        return new DirectNodeInputUsingModelAction<T>(modelReference, descriptor, inputs, action);
    }

    public static <T> ModelAction of(ModelReference<T> reference, ModelRuleDescriptor descriptor, List<? extends ModelReference<?>> input, final BiAction<? super MutableModelNode, ? super List<ModelView<?>>> action) {
        return new AbstractModelAction<T>(reference, descriptor, input) {
            @Override
            public void execute(MutableModelNode modelNode, List<ModelView<?>> inputs) {
                action.execute(modelNode, inputs);
            }
        };
    }

    public static <T, I> ModelAction of(ModelReference<T> reference, ModelRuleDescriptor descriptor, ModelReference<I> input, final BiAction<? super MutableModelNode, ? super I> action) {
        return new AbstractModelAction<T>(reference, descriptor, input) {
            @Override
            public void execute(MutableModelNode modelNode, List<ModelView<?>> inputs) {
                action.execute(modelNode, Cast.<I>uncheckedCast(inputs.get(0).getInstance()));
            }
        };
    }

    public static <T, I1, I2> ModelAction of(ModelReference<T> reference, ModelRuleDescriptor descriptor, ModelReference<I1> input1, ModelReference<I2> input2, final TriAction<? super MutableModelNode, ? super I1, ? super I2> action) {
        return new AbstractModelAction<T>(reference, descriptor, Arrays.<ModelReference<?>>asList(input1, input2)) {
            @Override
            public void execute(MutableModelNode modelNode, List<ModelView<?>> inputs) {
                action.execute(modelNode,
                    Cast.<I1>uncheckedCast(inputs.get(0).getInstance()),
                    Cast.<I2>uncheckedCast(inputs.get(1).getInstance())
                );
            }
        };
    }

    @Override
    public void execute(MutableModelNode modelNode, T view, List<ModelView<?>> inputs) {
        action.execute(modelNode, view, inputs);
    }
}

@@ -2852,13 +2852,11 @@
 T, I
-1
 , 
-I2
+J
 %3E Mo
@@ -2945,17 +2945,16 @@
 erence%3CI
-1
 %3E input1
@@ -2970,18 +2970,17 @@
 ference%3C
-I2
+J
 %3E input2
@@ -3032,17 +3032,16 @@
  super I
-1
 , ? supe
@@ -3042,18 +3042,17 @@
 ? super 
-I2
+J
 %3E action
@@ -3352,17 +3352,16 @@
  Cast.%3CI
-1
 %3Euncheck
@@ -3427,10 +3427,9 @@
 st.%3C
-I2
+J
 %3Eunc

Make Checkstyle happy