<commit_before>package org.wikiup.core.impl.translator;

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

import org.wikiup.core.Wikiup;
import org.wikiup.core.bean.WikiupDynamicSingleton;
import org.wikiup.core.inf.Document;
import org.wikiup.core.inf.Translator;
import org.wikiup.core.util.Documents;
import org.wikiup.core.util.Interfaces;

public class TypeCastTranslator extends WikiupDynamicSingleton<TypeCastTranslator> implements Translator<Object, Object> {
    private Map<Class<?>, Map<Class<?>, Translator<Object, Object>>> typeTranslators;

    public Object translate(Object object) {
        return cast(Object.class, object);
    }

    public <F, T> T cast(Class<T> toClass, F from) {
        Map<Class<?>, Translator<Object, Object>> toFilter = typeTranslators.get(Interfaces.box(from.getClass()));
        if(toFilter != null) {
            toClass = (Class<T>) Interfaces.box(toClass);
            Translator<Object, Object> translator = toFilter.get(Interfaces.box(toClass));
            return translator != null ? Interfaces.cast(toClass, translator.translate(from)) : null;
        }
        return null;
    }

    @Override
    public void aware(Document desc) {
        for(Document node : desc.getChildren()) {
            Class<?> clazz = Interfaces.getClass(Documents.getDocumentValue(node, "from-class", null));
            typeTranslators.put(clazz, loadTranslators(node));
        }
    }

    private Map<Class<?>, Translator<Object, Object>> loadTranslators(Document node) {
        Map<Class<?>, Translator<Object, Object>> filters = new HashMap<Class<?>, Translator<Object, Object>>();
        for(Document item : node.getChildren()) {
            Class<?> clazz = Interfaces.getClass(Documents.getDocumentValue(item, "to-class", null));
            filters.put(clazz, Wikiup.getInstance().getBean(Translator.class, item));
        }
        return filters;
    }

    public void firstBuilt() {
        typeTranslators = new HashMap<Class<?>, Map<Class<?>, Translator<Object, Object>>>();
    }
}<commit_msg>--------<commit_after>package org.wikiup.core.impl.translator;

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

import org.wikiup.core.Wikiup;
import org.wikiup.core.bean.WikiupDynamicSingleton;
import org.wikiup.core.inf.Document;
import org.wikiup.core.inf.Translator;
import org.wikiup.core.util.Documents;
import org.wikiup.core.util.Interfaces;

public class TypeCastTranslator extends WikiupDynamicSingleton<TypeCastTranslator> implements Translator<Object, Object> {
    private Map<Class<?>, Map<Class<?>, Translator<Object, Object>>> typeTranslators;

    public Object translate(Object object) {
        return cast(Object.class, object);
    }

    public <F, T> T cast(Class<T> toClass, F from) {
        Map<Class<?>, Translator<Object, Object>> toFilter = typeTranslators.get(Interfaces.box(toClass));
        if(toFilter != null) {
            Class<?> fromClass = Interfaces.box(from.getClass());
            Translator<Object, Object> translator = toFilter.get(Interfaces.box(fromClass));
            return translator != null ? Interfaces.cast(toClass, translator.translate(from)) : null;
        }
        return null;
    }

    @Override
    public void aware(Document desc) {
        for(Document node : desc.getChildren()) {
            Class<?> fromClass = Interfaces.getClass(Documents.getDocumentValue(node, "from-class", null));
            loadTranslators(fromClass, node);
        }
    }

    private void loadTranslators(Class<?> fromClass, Document node) {
        for(Document item : node.getChildren()) {
            Class<?> toClass = Interfaces.getClass(Documents.getDocumentValue(item, "to-class", null));
            Translator<Object, Object> translator = Wikiup.getInstance().getBean(Translator.class, item);
            addTranslator(fromClass, toClass, translator);
        }
    }
    
    private void addTranslator(Class<?> fromClass, Class<?> toClass, Translator<Object, Object> translator) {
        Map<Class<?>, Translator<Object, Object>> translators = typeTranslators.get(toClass);
        if(translators == null)
            typeTranslators.put(toClass, translators = new HashMap<Class<?>, Translator<Object, Object>>());
        translators.put(fromClass, translator);
    }

    public void firstBuilt() {
        typeTranslators = new HashMap<Class<?>, Map<Class<?>, Translator<Object, Object>>>();
    }
}
{'fraction_non_alphanumeric': 0.13336357450669087, 'fraction_numerical': 0.0, 'mean_word_length': 3.6081504702194356, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, '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': '5137099', 'n_tokens_mistral': 1315, 'n_tokens_neox': 1285, 'n_words': 259}