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

import therian.TherianContext;
import therian.Operator;
import therian.operation.Convert;
import therian.operation.Copy;
import therian.operation.ImmutableCheck;
import therian.position.Position;

/**
 * {@link Copy} {@link Operator} that attempts overwriting conversion for writable target positions storing immutable
 * values.
 */
public class ConvertingCopier implements Operator<Copy<?, ?>> {

    public void perform(Copy<?, ?> operation) {
        final Convert<?, ?> convert =
            Convert.to((Position.Writable<?>) operation.getTargetPosition(), operation.getSourcePosition());
        TherianContext.getRequiredInstance().forwardTo(convert);
    }

    public boolean supports(Copy<?, ?> operation) {
        return operation.getTargetPosition() instanceof Position.Writable
            && TherianContext.getRequiredInstance().eval(ImmutableCheck.of(operation.getTargetPosition())).booleanValue();
    }
}

@@ -655,30 +655,48 @@
 therian.
+Operator;%0Aimport therian.
 Therian
-Context
 ;%0Aimport
@@ -700,32 +700,38 @@
 ort therian.
-Operator
+TherianContext
 ;%0Aimport the
@@ -1365,33 +1365,28 @@
 (Copy%3C?, ?%3E 
+c
 op
-eration
+y
 ) %7B%0A        
@@ -1389,24 +1389,16 @@
     
-return operation
+if (copy
 .get
@@ -1442,16 +1442,19 @@
 Writable
+) %7B
 %0A       
@@ -1462,10 +1462,13 @@
     
-&&
+final
  The
@@ -1478,38 +1478,83 @@
 nContext
-.getRequiredInstance()
+ context = TherianContext.getInstance();%0A            return context
 .eval(Im
@@ -1569,25 +1569,20 @@
 heck.of(
+c
 op
-eration
+y
 .getTarg
@@ -1610,16 +1610,223 @@
 nValue()
+%0A                && context.getTypedContext(Therian.class).supports(%0A                    Convert.to((Position.Writable%3C?%3E) copy.getTargetPosition(), copy.getSourcePosition()));%0A        %7D%0A        return false
 ;%0A    %7D%0A

make convertingcopier pickier