<commit_before>/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013-2017 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
package com.wegas.core.merge.patch;

import com.wegas.core.exception.client.WegasErrorMessage;
import com.wegas.core.exception.client.WegasRuntimeException;
import com.wegas.core.merge.utils.LifecycleCollector;
import com.wegas.core.merge.utils.WegasCallback;
import com.wegas.core.persistence.Mergeable;
import com.wegas.core.persistence.game.GameModel;
import com.wegas.core.persistence.variable.ModelScoped;
import com.wegas.core.persistence.variable.ModelScoped.ProtectionLevel;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Patch List or Map of AbstrctEntities
 *
 * @author maxence
 */
public final class WegasChildrenPatch extends WegasPatch {

    private Object from;
    private final Mergeable referenceEntity;
    private Boolean primitive = null;
    private Object to;

    private List<WegasPatch> patches;

    WegasChildrenPatch(Object identifier, int order,
            WegasCallback userCallback, Mergeable referenceEntity,
            Method getter, Method setter,
            Object from, Object to,
            boolean recursive, boolean ignoreNull, boolean sameEntityOnly, boolean initOnly,
            ProtectionLevel protectionLevel) {

        super(identifier, order, getter, setter, userCallback, ignoreNull, sameEntityOnly, initOnly, recursive, protectionLevel);
        this.patches = new ArrayList<>();
        this.from = from;
        this.to = to;
        this.referenceEntity = referenceEntity;

        Map<Object, Object> fromMap = asMap(from);
        Map<Object, Object> toMap = asMap(to);

        /*
         * Go through initial children
         */
        for (Entry<Object, Object> entry : fromMap.entrySet()) {
            Object key = entry.getKey();

            Object fromEntity = entry.getValue();
            Object toEntity = toMap.get(key);

            if (primitive != null) {
                // this patch handles delete and update cases
                if (primitive) {
                    patches.add(new WegasPrimitivePatch(key, 0, null, referenceEntity, null, null, fromEntity, toEntity, false, false, false, this.protectionLevel));
                } else {
                    patches.add(new WegasEntityPatch(key, 0, null, null, null,
                            (Mergeable) fromEntity,
                            (Mergeable) toEntity, // null -> DELETE ; not null -> UPDATE
                            recursive, false, false, false, this.protectionLevel));
                }
            } else {
                // not able yet (java8) to retrieve effective class as generic types does not exists in runtime anymore...
                logger.error("Unable to guess patch type: ignore {}", referenceEntity);
            }

            if (to != null) {
                // since the patch to update "to" has been created, remove "to" from the map
                toMap.remove(key);
            }
        }

        /* Remaining entities in toMap are new */
        for (Entry<Object, Object> entry : toMap.entrySet()) {
            Object key = entry.getKey();
            Object toEntity = entry.getValue();
            if (primitive) {
                patches.add(new WegasPrimitivePatch(key, 0, null, referenceEntity, null, null, null, toEntity, false, false, false, this.protectionLevel));
            } else {
                patches.add(new WegasEntityPatch(key, 0, userCallback, null, null,
                        null, (Mergeable) toEntity, // from null to no null  -> CREATE
                        recursive, false, false, false, this.protectionLevel));
            }
        }
    }

    /**
     * return a new Map which contains all children
     * If children is a map, return map is a copy of children.
     * If children is a list, returned map contains all children indexed by their refId
     *
     * @param children List or Map which contains children
     *
     * @return a brand new map which contains all children
     */
    private Map<Object, Object> asMap(Object children) {
        Map<Object, Object> theMap = new HashMap<>();
        if (children instanceof List) {
            List<Object> list = (List<Object>) children;

            for (int i = 0; i < list.size(); i++) {
                Object get = list.get(i);
                if (get != null) {
                    if (primitive == null) {
                        primitive = !Mergeable.class.isAssignableFrom(get.getClass());
                    }
                    if (primitive) {
                        theMap.put("" + i + ":" + get, get);
                        // theMap.put(i, get);
                    } else {
                        theMap.put(((Mergeable) get).getRefId(), get);
                    }
                }
            }<commit_msg>Minor ChildrenPatch fix<commit_after>/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013-2017 School of Business and Engineering Vaud, Comem
 * Licensed under the MIT License
 */
package com.wegas.core.merge.patch;

import com.wegas.core.exception.client.WegasErrorMessage;
import com.wegas.core.exception.client.WegasRuntimeException;
import com.wegas.core.merge.utils.LifecycleCollector;
import com.wegas.core.merge.utils.WegasCallback;
import com.wegas.core.persistence.Mergeable;
import com.wegas.core.persistence.game.GameModel;
import com.wegas.core.persistence.variable.ModelScoped;
import com.wegas.core.persistence.variable.ModelScoped.ProtectionLevel;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Patch List or Map of AbstrctEntities
 *
 * @author maxence
 */
public final class WegasChildrenPatch extends WegasPatch {

    private Object from;
    private final Mergeable referenceEntity;
    private Boolean primitive = null;
    private Object to;

    private List<WegasPatch> patches;

    WegasChildrenPatch(Object identifier, int order,
            WegasCallback userCallback, Mergeable referenceEntity,
            Method getter, Method setter,
            Object from, Object to,
            boolean recursive, boolean ignoreNull, boolean sameEntityOnly, boolean initOnly,
            ProtectionLevel protectionLevel) {

        super(identifier, order, getter, setter, userCallback, ignoreNull, sameEntityOnly, initOnly, recursive, protectionLevel);
        this.patches = new ArrayList<>();
        this.from = from;
        this.to = to;
        this.referenceEntity = referenceEntity;

        Map<Object, Object> fromMap = asMap(from);
        Map<Object, Object> toMap = asMap(to);

        /*
             * Go through initial children
         */
        for (Entry<Object, Object> entry : fromMap.entrySet()) {
            Object key = entry.getKey();

            Object fromEntity = entry.getValue();
            Object toEntity = toMap.get(key);

            // this patch handles delete and update cases
            if (primitive != null) {
                if (primitive) {
                    patches.add(new WegasPrimitivePatch(key, 0, null, referenceEntity, null, null, fromEntity, toEntity, false, false, false, this.protectionLevel));
                } else {
                    patches.add(new WegasEntityPatch(key, 0, null, null, null,
                            (Mergeable) fromEntity,
                            (Mergeable) toEntity, // null -> DELETE ; not null -> UPDATE
                            recursive, false, false, false, this.protectionLevel));
                }

                if (to != null) {
                    // since the patch to update "to" has been created, remove "to" from the map
                    toMap.remove(key);
                }

            } else {
                // not able yet (java8) to retrieve effective class as generic types does not exists in runtime anymore...
                logger.debug("Unable to guess patch type: ignore {}'s {}", referenceEntity, identifier);
            }
        }

        /* Remaining entities in toMap are new */
        for (Entry<Object, Object> entry : toMap.entrySet()) {
            Object key = entry.getKey();
            Object toEntity = entry.getValue();
            if (primitive != null) {
                if (primitive) {
                    patches.add(new WegasPrimitivePatch(key, 0, null, referenceEntity, null, null, null, toEntity, false, false, false, this.protectionLevel));
                } else {
                    patches.add(new WegasEntityPatch(key, 0, userCallback, null, null,
                            null, (Mergeable) toEntity, // from null to no null  -> CREATE
                            recursive, false, false, false, this.protectionLevel));
                }

            } else {
                // not able yet (java8) to retrieve effective class as generic types does not exists in runtime anymore...
                logger.debug("Unable to guess patch type: ignore {}'s {}", referenceEntity, identifier);
            }
        }

    }

    /**
     * return a new Map which contains all children
     * If children is a map, return map is a copy of children.
     * If children is a list, returned map contains all children indexed by their refId
     *
     * @param children List or Map which contains children
     *
     * @return a brand new map which contains all children
     */
    private Map<Object, Object> asMap(Object children) {
        Map<Object, Object> theMap = new HashMap<>();
        if (children instanceof List) {
            List<Object> list = (List<Object>) children;

            for (int i = 0; i < list.size(); i++) {
                Object get = list.get(i);
                if (get != null) {
                    if (primitive == null) {
                        primitive = !Mergeable.class.isAssignableFrom(get.getClass());
                    }
                    if (primitive) {
                        theMap.put("" + i + ":" + get, get);
                        // theMap.put(i, get);
                    } else {
                        theMap.put(((Mergeable) get).getRefId(), get);
                    }
                }
            }
{'fraction_non_alphanumeric': 0.0980336547551522, 'fraction_numerical': 0.00274153904329741, 'mean_word_length': 1.9875741316012425, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 31, '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': '19344', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2715, 'n_words': 828}