/*
 * Copyright 2008-2011 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.mozilla.javascript;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.nominanuda.code.Nullable;
import com.nominanuda.lang.ObjectConvertor;
import com.nominanuda.lang.Tuple2;

public class PluggableMethodArgCoercer implements MethodArgCoercer {
	private final Map<Class<?>, Tuple2<ObjectConvertor<Object, Object, Exception>,Integer>> convertors = new HashMap<Class<?>, Tuple2<ObjectConvertor<Object,Object,Exception>,Integer>>();

	public int getConversionWeight(Object value, Class<?> type) {
		Tuple2<ObjectConvertor<Object, Object, Exception>,Integer> t = findConvertor(type);
		return t == null ? Integer.MIN_VALUE : t.get1();
	}

	private @Nullable Tuple2<ObjectConvertor<Object, Object, Exception>, Integer> findConvertor(
			Class<?> type) {
		for(Entry<Class<?>, Tuple2<ObjectConvertor<Object, Object, Exception>,Integer>> e : convertors.entrySet()) {
			if(e.getKey().isAssignableFrom(type)) {
				return e.getValue();
			}
		}
		return null;
	}

	public Object coerceTypeImpl(Class<?> type, Object value) {
		Tuple2<ObjectConvertor<Object, Object, Exception>,Integer> t = findConvertor(type);
		if(t != null) {
			try {
				return t.get0().apply(value);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		} else {
			return REGULAR_COERCE_OP;
		}
	}

	public void setConvertors(
			Map<Class<?>, Tuple2<ObjectConvertor<Object, Object, Exception>, Integer>> convertors) {
		this.convertors.clear();
		this.convertors.putAll(convertors);
	}

}

@@ -1099,16 +1099,173 @@
 r%3E%3E();%0A%0A
+%09public PluggableMethodArgCoercer(Map%3CClass%3C?%3E, Tuple2%3CObjectConvertor%3CObject, Object, Exception%3E, Integer%3E%3E convertors) %7B%0A%09%09setConvertors(convertors);%0A%09%7D%0A%09%0A
 %09public 
@@ -1398,24 +1398,31 @@
 nvertor(type
+, value
 );%0A%09%09return 
@@ -1560,20 +1560,16 @@
 nvertor(
-%0A%09%09%09
 Class%3C?%3E
@@ -1569,24 +1569,38 @@
 lass%3C?%3E type
+, Object value
 ) %7B%0A%09%09for(En
@@ -1596,16 +1596,17 @@
  %7B%0A%09%09for
+ 
 (Entry%3CC
@@ -1708,16 +1708,17 @@
  %7B%0A%09%09%09if
+ 
 (e.getKe
@@ -1725,28 +1725,59 @@
 y().
-isAssignableFrom(typ
+equals(type) && e.getValue().get0().canConvert(valu
 e)) 
@@ -1979,15 +1979,23 @@
 type
+, value
 );%0A%09%09if
+ 
 (t !
@@ -2189,20 +2189,16 @@
 vertors(
-%0A%09%09%09
 Map%3CClas

call ObjectConvertor.canConvert before accepting a convertor coercer class must be the same as the one in the method signature (allows better overloading)