//
// Copyright 2011 The Apache Software Foundation
// 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.apache.tapestry5.internal.transform;

import org.apache.tapestry5.ioc.Invokable;
import org.apache.tapestry5.ioc.OperationTracker;
import org.apache.tapestry5.runtime.ComponentEvent;

/**
 * Used  to encapsulate the list of {@link EventHandlerMethodParameterProvider}s for a particular
 * method of a particular component, providing {@link OperationTracker} behavior as parameter values
 * are obtained/computed/coerced.
 *
 * @since 5.3
 */
public class EventHandlerMethodParameterSource
{
    private final String methodIdentifier;

    private final OperationTracker operationTracker;

    private final EventHandlerMethodParameterProvider[] providers;

    public EventHandlerMethodParameterSource(String methodIdentifier, OperationTracker operationTracker, EventHandlerMethodParameterProvider[] providers)
    {

        this.methodIdentifier = methodIdentifier;
        this.operationTracker = operationTracker;
        this.providers = providers;
    }

    public Object get(final ComponentEvent event, final int index)
    {
        // Hopefully this will not be too much overhead; it's really nice to be able to track what parameter
        // caused a failure.

        return operationTracker.invoke(String.format("Obtaining value for parameter #%d of %s", index + 1, methodIdentifier),
                new Invokable<Object>()
                {
                    public Object invoke()
                    {
                        return providers[index].valueForEventHandlerMethodParameter(event);
                    }
                });
    }
}

@@ -1100,16 +1100,618 @@
 ource%0A%7B%0A
+    private static final class ParameterExtractor implements Invokable%3CObject%3E %7B%0A        private final EventHandlerMethodParameterProvider%5B%5D providers;%0A        private final int index;%0A        private final ComponentEvent event;%0A%0A        private ParameterExtractor(EventHandlerMethodParameterProvider%5B%5D providers, int index, ComponentEvent event) %7B%0A            this.providers = providers;%0A            this.index = index;%0A            this.event = event;%0A        %7D%0A%0A        public Object invoke()%0A        %7B%0A            return providers%5Bindex%5D.valueForEventHandlerMethodParameter(event);%0A        %7D%0A    %7D%0A%0A
     priv
@@ -1741,24 +1741,24 @@
 Identifier;%0A
-
 %0A    private
@@ -2427,22 +2427,8 @@
 oke(
-String.format(
 %22Obt
@@ -2459,29 +2459,33 @@
 er #
-%25d of %25s%22, index + 1,
+%22 + (index + 1) + %22 of %22+
  met
@@ -2497,17 +2497,16 @@
 entifier
-)
 ,%0A      
@@ -2523,142 +2523,27 @@
 new 
-Invokable%3CObject%3E()%0A                %7B%0A                    public Object invoke()%0A                    %7B%0A                        return 
+ParameterExtractor(
 prov
@@ -2551,99 +2551,23 @@
 ders
-%5B
+, 
 index
-%5D.valueForEventHandlerMethodParameter(event);%0A                    %7D%0A                %7D
+, event)
 );%0A 

Optimize org.apache.tapestry5.internal.transform.EventHandlerMethodParameterSource.get(ComponentEvent, int) (~+30% reqs/second)