<commit_before>package org.openmole.core.implementation.tools;

import java.io.IOException;

import com.rits.cloning.Cloner;
import com.rits.cloning.IFastCloner;
import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.MultipleException;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.core.implementation.data.Variable;
import org.openmole.core.implementation.internal.Activator;
import org.openmole.core.model.data.IVariable;


import static org.openmole.commons.tools.io.FileUtil.*;

public class ClonningService {

    
    public static IVariable clone(IVariable variable) throws InternalProcessingError, UserBadDataError {
        if (variable.getValue() == null || variable.getValue().getClass().isPrimitive()) {
            return variable;
        }
        
        Cloner cloner = new Cloner();
        final List<Throwable> exceptions = Collections.synchronizedList(new LinkedList<Throwable>());

        cloner.registerFastCloner(File.class, new IFastCloner() {

            @Override
            public Object clone(Object o, Cloner cloner, Map<Object, Object> map) throws IllegalAccessException {
                File toClone = (File) o;
                File cloned;
                try {
                    if (toClone.isDirectory()) {
                        cloned = Activator.getWorkspace().newTmpDir();
                    } else {
                        cloned = Activator.getWorkspace().newTmpFile();
                    }

                    copy(toClone, cloned);
                    return cloned;
                } catch (IOException ex) {
                    exceptions.add(ex);
                } catch (InternalProcessingError ex) {
                    exceptions.add(ex);
                }
                return null;
            }
        });

        Object val = cloner.deepClone(variable.getValue());

        if(!exceptions.isEmpty()) {
            throw new InternalProcessingError(new MultipleException(exceptions));
        }

        return new Variable(variable.getPrototype(), val);

       /* try {<commit_msg>Add big decimal and big integer as immutables.<commit_after>package org.openmole.core.implementation.tools;

import java.io.IOException;

import com.rits.cloning.Cloner;
import com.rits.cloning.IFastCloner;
import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.openmole.commons.exception.InternalProcessingError;
import org.openmole.commons.exception.MultipleException;
import org.openmole.commons.exception.UserBadDataError;
import org.openmole.core.implementation.data.Variable;
import org.openmole.core.implementation.internal.Activator;
import org.openmole.core.model.data.IVariable;


import static org.openmole.commons.tools.io.FileUtil.*;

public class ClonningService {

    
    public static IVariable clone(IVariable variable) throws InternalProcessingError, UserBadDataError {
        if (variable.getValue() == null || 
                variable.getValue().getClass().isPrimitive() ||
                variable.getValue().getClass() == BigDecimal.class ||
                variable.getValue().getClass() == BigInteger.class) {
            return variable;
        }
        
        Cloner cloner = new Cloner();
        final List<Throwable> exceptions = Collections.synchronizedList(new LinkedList<Throwable>());

        cloner.registerFastCloner(File.class, new IFastCloner() {

            @Override
            public Object clone(Object o, Cloner cloner, Map<Object, Object> map) throws IllegalAccessException {
                File toClone = (File) o;
                File cloned;
                try {
                    if (toClone.isDirectory()) {
                        cloned = Activator.getWorkspace().newTmpDir();
                    } else {
                        cloned = Activator.getWorkspace().newTmpFile();
                    }

                    copy(toClone, cloned);
                    return cloned;
                } catch (IOException ex) {
                    exceptions.add(ex);
                } catch (InternalProcessingError ex) {
                    exceptions.add(ex);
                }
                return null;
            }
        });

        cloner.registerImmutable(BigDecimal.class);
        cloner.registerImmutable(BigInteger.class);
        
        Object val = cloner.deepClone(variable.getValue());

        if(!exceptions.isEmpty()) {
            throw new InternalProcessingError(new MultipleException(exceptions));
        }

        return new Variable(variable.getPrototype(), val);

       /* try {
{'fraction_non_alphanumeric': 0.09886947584789312, 'fraction_numerical': 0.0, 'mean_word_length': 2.2375249500998002, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4633554', 'n_tokens_mistral': 1317, 'n_tokens_neox': 1242, 'n_words': 247}