/*
 *  Copyright 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 therian.operator.convert;

import java.lang.reflect.Type;
import java.util.Collection;

import org.apache.commons.lang3.reflect.TypeUtils;

import therian.TherianContext;
import therian.Operator.DependsOn;
import therian.buildweaver.StandardOperator;
import therian.operation.Convert;
import therian.operation.GetElementType;
import therian.operator.getelementtype.GetArrayElementType;
import therian.position.Position;
import therian.util.Positions;

/**
 * Converts from source to {@link Collection} to target.
 */
@StandardOperator
@DependsOn({ GetArrayElementType.class, CollectionToArray.class })
public class DefaultToArrayConverter extends Converter.WithDynamicTarget<Object> {

    @Override
    public boolean perform(TherianContext context, final Convert<?, ?> convert) {
        @SuppressWarnings("rawtypes")
        final Position.ReadWrite<Collection> coll = Positions.readWrite(Collection.class);
        if (!context.evalSuccess(Convert.to(coll, convert.getSourcePosition()))) {
            return false;
        }
        return context.forwardTo(Convert.to(convert.getTargetPosition(), coll));
    }

    @Override
    public boolean supports(TherianContext context, Convert<?, ?> convert) {
        if (!(super.supports(context, convert) && TypeUtils.isArrayType(convert.getTargetPosition().getType()))) {
            return false;
        }
        if (!context.supports(Convert.to(Collection.class, convert.getSourcePosition()))) {
            return false;
        }
        final GetElementType<?> getTargetElementType = GetElementType.of(convert.getTargetPosition());
        if (!context.supports(getTargetElementType)) {
            return false;
        }
        final Type targetElementType = context.eval(getTargetElementType);
        final GetElementType<?> getSourceElementType = GetElementType.of(convert.getSourcePosition());
        if (!context.supports(getSourceElementType)) {
            return false;
        }
        final Type sourceElementType = context.eval(getSourceElementType);
        return TypeUtils.isAssignable(sourceElementType, targetElementType);
    }

}

@@ -696,26 +696,20 @@
 va.util.
-Collection
+List
 ;%0A%0Aimpor
@@ -759,39 +759,8 @@
 s;%0A%0A
-import therian.TherianContext;%0A
 impo
@@ -790,16 +790,47 @@
 endsOn;%0A
+import therian.TherianContext;%0A
 import t
@@ -1065,16 +1065,43 @@
 sitions;
+%0Aimport therian.util.Types;
 %0A%0A/**%0A *
@@ -1136,19 +1136,36 @@
 ink 
-Collection%7D
+List%7D of target element type
  to 
@@ -1453,37 +1453,101 @@
     
-@SuppressWarnings(%22rawtypes%22)
+final Type targetElementType = context.eval(GetElementType.of(convert.getTargetPosition()));%0A
 %0A   
@@ -1580,24 +1580,21 @@
 ite%3C
-Collection%3E coll
+List%3C?%3E%3E list
  = P
@@ -1616,35 +1616,69 @@
 ite(
-Collection.class
+Types.parameterize(List.class, targetElementType)
 );%0A
+%0A
         
 if (
@@ -1665,37 +1665,39 @@
 ype));%0A%0A        
-if (!
+return 
 context.evalSucc
@@ -1711,20 +1711,20 @@
 vert.to(
-coll
+list
 , conver
@@ -1738,35 +1738,32 @@
 urcePosition()))
-) %7B
 %0A            ret
@@ -1763,46 +1763,10 @@
     
-return false;%0A        %7D%0A        return
+&&
  con
@@ -1824,12 +1824,12 @@
 (), 
-coll
+list
 ));%0A
@@ -2081,136 +2081,8 @@
   %7D%0A
-        if (!context.supports(Convert.to(Collection.class, convert.getSourcePosition()))) %7B%0A            return false;%0A        %7D%0A
     
@@ -2346,24 +2346,25 @@
 tType);%0A
+%0A
         
 final Ge
@@ -2359,255 +2359,100 @@
     
-final GetElementType%3C?%3E getSourceElementType = GetElementType.of(convert.getSourcePosition());%0A        if (!context.supports(getSourceElementType)) %7B%0A            return false;%0A        %7D%0A        final Type sourceElementType = context.eval(getSource
+return context.supports(Convert.to(Positions.readWrite(Types.parameterize(List.class, target
 Elem
@@ -2455,25 +2455,26 @@
 ElementType)
-;
+),
 %0A        ret
@@ -2474,74 +2474,40 @@
     
-return TypeUtils.isAssignable(sourceElementType, targetElementType
+    convert.getSourcePosition())
 );%0A 

optimize and ensure the correct type of Collection (i.e. List) is used for intermediate