<commit_before>package org.splevo.diffing.emfcompare.diff;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.FactoryException;
import org.eclipse.emf.compare.diff.engine.IMatchManager;
import org.eclipse.emf.compare.diff.engine.IMatchManager.MatchSide;
import org.eclipse.emf.compare.diff.engine.IMatchManager2;
import org.eclipse.emf.compare.diff.engine.check.ReferencesCheck;
import org.eclipse.emf.compare.diff.metamodel.ConflictingDiffElement;
import org.eclipse.emf.compare.diff.metamodel.DiffElement;
import org.eclipse.emf.compare.diff.metamodel.DiffFactory;
import org.eclipse.emf.compare.diff.metamodel.DiffGroup;
import org.eclipse.emf.compare.diff.metamodel.ReferenceChangeLeftTarget;
import org.eclipse.emf.compare.diff.metamodel.ReferenceChangeRightTarget;
import org.eclipse.emf.compare.diff.metamodel.UpdateReference;
import org.eclipse.emf.compare.match.internal.statistic.ResourceSimilarity;
import org.eclipse.emf.compare.match.metamodel.Match2Elements;
import org.eclipse.emf.compare.match.metamodel.Match3Elements;
import org.eclipse.emf.compare.util.EFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * A java model specific reference check to interpret model references which can be ignored.
 */
@SuppressWarnings("restriction")
public class JavaModelReferenceCheck extends ReferencesCheck {

    /** The package ignore visitor instance to be used. */
    @SuppressWarnings("unused")
    private PackageIgnoreVisitor packageIgnoreVisitor = null;

    /**
     * Constructor requiring a match manager to access required match objects.
     * 
     * @param manager
     *            The match manager to use.
     * @param ignorePackages
     *            The list of patterns to filter our packages that should be ignored.
     */
    public JavaModelReferenceCheck(IMatchManager manager, List<String> ignorePackages) {
        super(manager);
        packageIgnoreVisitor = new PackageIgnoreVisitor(ignorePackages);
    }
    
    /**
     * Check if a reference should be ignored. This method is an overloaded version of the
     * shouldBeIgnored(EReference) to also interpret the referencing element.
     * 
     * @param reference
     *            The reference to check.
     * @param referencingElement
     *            The referencing element to include in the check.
     * @return true/false whether to ignore the reference or not.
     */
    protected boolean shouldBeIgnored(EReference reference, EObject referencingElement) {

        // ************************************
        // Ignore Backlinks in bi-directional references
        // if a class is used in an import or by another type access
        // this is represented by a bi-directional reference.
        // this bi-derectional reference should be ignored for the "usagesIn.." side
        // ************************************
        if ("usagesInTypeAccess".equals(reference.getName())) {
            return true;
        } else if ("usagesInImports".equals(reference.getName())) {
            return true;
        } else if ("usagesInPackageAccess".equals(reference.getName())) {
            return true;
        } else if ("usages".equals(reference.getName())) {
            return true;
        }

        // ************************************
        // ignore references to comments while
        // changes to comments are ignored at all
        // ************************************
        if ("commentList".equals(reference.getName())) {
            return true;
        }

        return super.shouldBeIgnored(reference);
    }

    /* ************************************************************************************ */
    /* ****** METHODS OVERRIDDEN TO PASS ADDITIONAL ATTRIBUTES TO shouldBeIgnored() ******* */
    /* ************************************************************************************ */

    /**
     * Checks if a reference has been changed.<br/>
     * <p>
     * A reference is considered as changed if its value(s) are different (either removal or
     * addition of a value if the reference is multi-valued or change of a single-valued reference)
     * between the left and the right model.
     * </p>
     * 
     * Note: The method has been overriden to pass an additional element to the shouldBeIgnored
     * method. This also required to override this method to hand-over the origin element of the
     * mapping
     * 
     * @param root
     *            {@link DiffGroup root} of the {@link DiffElement} to create.
     * @param mapping
     *            Contains informations about the left and right model elements we have to compare.
     * @throws FactoryException
     *             Thrown if we cannot fetch the references' values.
     */
    public void checkReferencesUpdates(DiffGroup root, Match2Elements mapping) throws FactoryException {
        final EClass eClass = mapping.getLeftElement().eClass();<commit_msg>SPLEVO-20 Improve Diffing Performance Ignore all references outgoing from elements in ignored java packages<commit_after>package org.splevo.diffing.emfcompare.diff;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.FactoryException;
import org.eclipse.emf.compare.diff.engine.IMatchManager;
import org.eclipse.emf.compare.diff.engine.IMatchManager.MatchSide;
import org.eclipse.emf.compare.diff.engine.IMatchManager2;
import org.eclipse.emf.compare.diff.engine.check.ReferencesCheck;
import org.eclipse.emf.compare.diff.metamodel.ConflictingDiffElement;
import org.eclipse.emf.compare.diff.metamodel.DiffElement;
import org.eclipse.emf.compare.diff.metamodel.DiffFactory;
import org.eclipse.emf.compare.diff.metamodel.DiffGroup;
import org.eclipse.emf.compare.diff.metamodel.ReferenceChangeLeftTarget;
import org.eclipse.emf.compare.diff.metamodel.ReferenceChangeRightTarget;
import org.eclipse.emf.compare.diff.metamodel.UpdateReference;
import org.eclipse.emf.compare.match.internal.statistic.ResourceSimilarity;
import org.eclipse.emf.compare.match.metamodel.Match2Elements;
import org.eclipse.emf.compare.match.metamodel.Match3Elements;
import org.eclipse.emf.compare.util.EFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * A java model specific reference check to interpret model references which can be ignored.
 */
@SuppressWarnings("restriction")
public class JavaModelReferenceCheck extends ReferencesCheck {

    /** The package ignore visitor instance to be used. */
    @SuppressWarnings("unused")
    private PackageIgnoreVisitor packageIgnoreVisitor = null;

    /**
     * Constructor requiring a match manager to access required match objects.
     * 
     * @param manager
     *            The match manager to use.
     * @param ignorePackages
     *            The list of patterns to filter our packages that should be ignored.
     */
    public JavaModelReferenceCheck(IMatchManager manager, List<String> ignorePackages) {
        super(manager);
        packageIgnoreVisitor = new PackageIgnoreVisitor(ignorePackages);
    }
    
    /**
     * Check if a reference should be ignored. This method is an overloaded version of the
     * shouldBeIgnored(EReference) to also interpret the referencing element.
     * 
     * @param reference
     *            The reference to check.
     * @param referencingElement
     *            The referencing element to include in the check.
     * @return true/false whether to ignore the reference or not.
     */
    protected boolean shouldBeIgnored(EReference reference, EObject referencingElement) {

        // ************************************
        // Ignore Backlinks in bi-directional references
        // if a class is used in an import or by another type access
        // this is represented by a bi-directional reference.
        // this bi-derectional reference should be ignored for the "usagesIn.." side
        // ************************************
        if ("usagesInTypeAccess".equals(reference.getName())) {
            return true;
        } else if ("usagesInImports".equals(reference.getName())) {
            return true;
        } else if ("usagesInPackageAccess".equals(reference.getName())) {
            return true;
        } else if ("usages".equals(reference.getName())) {
            return true;
        }
        
        // ************************************
        // ignore references to comments while
        // changes to comments are ignored at all
        // ************************************
        if ("commentList".equals(reference.getName())) {
            return true;
        }

        
        // *******************************************************
        // Ignore references from elements in ignored packages
        // This is done after the reference name based filtering
        // because it requires more resources than simply checking 
        // the reference name
        // ********************************************************
        Boolean ignore = packageIgnoreVisitor.doSwitch(referencingElement);
        if (Boolean.TRUE.equals(ignore)) {
            return true;
        }

        return super.shouldBeIgnored(reference);
    }

    /* ************************************************************************************ */
    /* ****** METHODS OVERRIDDEN TO PASS ADDITIONAL ATTRIBUTES TO shouldBeIgnored() ******* */
    /* ************************************************************************************ */

    /**
     * Checks if a reference has been changed.<br/>
     * <p>
     * A reference is considered as changed if its value(s) are different (either removal or
     * addition of a value if the reference is multi-valued or change of a single-valued reference)
     * between the left and the right model.
     * </p>
     * 
     * Note: The method has been overriden to pass an additional element to the shouldBeIgnored
     * method. This also required to override this method to hand-over the origin element of the
     * mapping
     * 
     * @param root
     *            {@link DiffGroup root} of the {@link DiffElement} to create.
     * @param mapping
     *            Contains informations about the left and right model elements we have to compare.
     * @throws FactoryException
     *             Thrown if we cannot fetch the references' values.
     */
    public void checkReferencesUpdates(DiffGroup root, Match2Elements mapping) throws FactoryException {
        final EClass eClass = mapping.getLeftElement().eClass();
{'fraction_non_alphanumeric': 0.1488649724156643, 'fraction_numerical': 0.0009044044496698924, 'mean_word_length': 3.1759818731117826, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '836387', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2776, 'n_words': 818}