<commit_before>package com.specmate.persistency.cdo.internal;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOObjectHistory;
import org.eclipse.emf.cdo.common.commit.CDOChangeSetData;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.osgi.service.component.annotations.Component;

import com.specmate.common.SpecmateException;
import com.specmate.model.history.Change;
import com.specmate.model.history.History;
import com.specmate.model.history.HistoryEntry;
import com.specmate.model.history.HistoryFactory;
import com.specmate.persistency.IHistoryProvider;
import com.specmate.persistency.event.EChangeKind;

@Component(immediate = true)
public class HistoryProviderImpl implements IHistoryProvider {

	@Override
	public History getHistory(EObject object) throws SpecmateException {
		CDOObject cdoObject = (CDOObject) object;
		History history = HistoryFactory.eINSTANCE.createHistory();
		return processHistory(cdoObject, history);
	}

	@Override
	public History getHistoryRecursive(EObject object) throws SpecmateException {
		History history = HistoryFactory.eINSTANCE.createHistory();

		history = getHistory(object);
		
		// get all contents recursively
		TreeIterator<EObject> it = object.eAllContents();
		while(it.hasNext()) {
			CDOObject cdoObject = (CDOObject) it.next();
			history = processHistory(cdoObject, history);
		}
		
		// sort by date (new to old)
		ECollections.sort(history.getEntries(), new Comparator<HistoryEntry>() {
			@Override
			public int compare(HistoryEntry o1, HistoryEntry o2) {
				return o2.getDate().compareTo(o1.getDate());
			}
		});
		
		return history;
	}
		
	private History processHistory(CDOObject cdoObject, History history) {
		CDOCommitInfo[] cdoHistoryElements = getCDOHistoryElements(cdoObject);
		for (int i = 0; i < cdoHistoryElements.length; i++) {
			CDOCommitInfo cdoHistoryElement = cdoHistoryElements[i];
			HistoryEntry historyEntry = HistoryFactory.eINSTANCE.createHistoryEntry();

			fillHistoryEntry(cdoObject, cdoHistoryElement, historyEntry);
			history.getEntries().add(historyEntry);
		}
		return history;
	}

	private void fillHistoryEntry(CDOObject cdoObject, CDOCommitInfo cdoHistoryElement, HistoryEntry historyEntry) {
		HistoryDeltaProcessor deltaProcessor = new HistoryDeltaProcessor(cdoHistoryElement, cdoObject.cdoID());<commit_msg>Only add a historyentry if it contains changes<commit_after>package com.specmate.persistency.cdo.internal;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOObjectHistory;
import org.eclipse.emf.cdo.common.commit.CDOChangeSetData;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.osgi.service.component.annotations.Component;

import com.specmate.common.SpecmateException;
import com.specmate.model.history.Change;
import com.specmate.model.history.History;
import com.specmate.model.history.HistoryEntry;
import com.specmate.model.history.HistoryFactory;
import com.specmate.persistency.IHistoryProvider;
import com.specmate.persistency.event.EChangeKind;

@Component(immediate = true)
public class HistoryProviderImpl implements IHistoryProvider {

	@Override
	public History getHistory(EObject object) throws SpecmateException {
		CDOObject cdoObject = (CDOObject) object;
		History history = HistoryFactory.eINSTANCE.createHistory();
		return processHistory(cdoObject, history);
	}

	@Override
	public History getHistoryRecursive(EObject object) throws SpecmateException {
		History history = HistoryFactory.eINSTANCE.createHistory();

		history = getHistory(object);
		
		// get all contents recursively
		TreeIterator<EObject> it = object.eAllContents();
		while(it.hasNext()) {
			CDOObject cdoObject = (CDOObject) it.next();
			history = processHistory(cdoObject, history);
		}
		
		// sort by date (new to old)
		ECollections.sort(history.getEntries(), new Comparator<HistoryEntry>() {
			@Override
			public int compare(HistoryEntry o1, HistoryEntry o2) {
				return o2.getDate().compareTo(o1.getDate());
			}
		});
		
		return history;
	}
		
	private History processHistory(CDOObject cdoObject, History history) {
		CDOCommitInfo[] cdoHistoryElements = getCDOHistoryElements(cdoObject);
		for (int i = 0; i < cdoHistoryElements.length; i++) {
			CDOCommitInfo cdoHistoryElement = cdoHistoryElements[i];
			HistoryEntry historyEntry = HistoryFactory.eINSTANCE.createHistoryEntry();

			fillHistoryEntry(cdoObject, cdoHistoryElement, historyEntry);
			if(!historyEntry.getChanges().isEmpty())
				history.getEntries().add(historyEntry);
		}
		return history;
	}

	private void fillHistoryEntry(CDOObject cdoObject, CDOCommitInfo cdoHistoryElement, HistoryEntry historyEntry) {
		HistoryDeltaProcessor deltaProcessor = new HistoryDeltaProcessor(cdoHistoryElement, cdoObject.cdoID());
{'fraction_non_alphanumeric': 0.10215439856373429, 'fraction_numerical': 0.0017953321364452424, 'mean_word_length': 5.785627283800244, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2503123', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1702, 'n_words': 327}