<commit_before>package core;

import html.HistoryPage;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import launch.StatusManager.Status;
import logger.Logger.Module;

import util.FileTools;
import util.IChangedListener;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import data.LaunchHistory;

/**
 * the configuration of the application,- will be serialized
 */
public class History {

	public static final String OUTPUT_FILE = "History.xml";
	
	@SuppressWarnings("unused")
	private String version;
	
	private ArrayList<LaunchHistory> entries;
	private transient ArrayList<IChangedListener> listeners;
	private transient String path;
	private transient boolean dirty;

	public History(String path){
		
		version = Constants.APP_VERSION;
		entries = new ArrayList<LaunchHistory>();
		listeners = new ArrayList<IChangedListener>();
		this.path = path;
		dirty = true;
	}

	public void addListener(IChangedListener listener){ listeners.add(listener); }
	
	public void notifyListeners(){
		for(IChangedListener listener : listeners){
			listener.changed(this);
		}
	}
	
	public ArrayList<LaunchHistory> getEntries(){ return entries; }
	public String getPath(){ return path; }
	
	public void setDirty(boolean dirty){ this.dirty = dirty; }
	public boolean isDirty(){ return dirty; }
	
	public static History load(String path) throws Exception {
	
		Application.getInstance().getLogger().debug(Module.COMMON, "load: "+path);
		XStream xstream = new XStream(new DomDriver());
		String xml = FileTools.readFile(path);
		History history = (History)xstream.fromXML(xml);
		history.listeners = new ArrayList<IChangedListener>();
		history.path = path;
		history.dirty = false;
		return history;
	}
	
	public void save() throws Exception {
		
		if(isDirty()){
			Application.getInstance().getLogger().debug(Module.COMMON, "save: "+path);
			XStream xstream = new XStream(new DomDriver());
			String xml = xstream.toXML(this);
			FileTools.writeFile(path, xml, false);
			dirty = false;
			notifyListeners();
		}
	}
	
	public synchronized void add(LaunchHistory entry) throws Exception {
		
		entry.init();
		entries.add(0, entry);
		dirty = true;
		save();
		cleanup();
		createIndex();
	}
	
	public synchronized void delete(String id) {
		
		for(int i=0; i<entries.size(); i++){
			LaunchHistory entry = entries.get(i);
			if(entry.historyId.equals(id)){
				delete(entry);
			}
		}
	}
	
	/** clear old entries */
	public synchronized void clear() {
		
		for(int i = entries.size()-1; i>=0; i--){
			delete(entries.get(i));
		}
	}

	/** recreate the main index-page */
	public void createIndex(){
		
		HistoryPage page = new HistoryPage(this, getIndexPath());
		try{
			page.create();
		}catch(Exception e){
			Application.getInstance().getLogger().error(Module.COMMON, e);
		}
	}
	
	public String getIndexPath() {
		return 
			Application.getInstance().getFileManager().getHistoryFolderPath()+
			File.separator+HistoryPage.OUTPUT_FILE;
	}<commit_msg>also update history-index on delete<commit_after>package core;

import html.HistoryPage;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import launch.StatusManager.Status;
import logger.Logger.Module;

import util.FileTools;
import util.IChangedListener;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import data.LaunchHistory;

/**
 * the configuration of the application,- will be serialized
 */
public class History {

	public static final String OUTPUT_FILE = "History.xml";
	
	@SuppressWarnings("unused")
	private String version;
	
	private ArrayList<LaunchHistory> entries;
	private transient ArrayList<IChangedListener> listeners;
	private transient String path;
	private transient boolean dirty;

	public History(String path){
		
		version = Constants.APP_VERSION;
		entries = new ArrayList<LaunchHistory>();
		listeners = new ArrayList<IChangedListener>();
		this.path = path;
		dirty = true;
	}

	public void addListener(IChangedListener listener){ listeners.add(listener); }
	
	public void notifyListeners(){
		for(IChangedListener listener : listeners){
			listener.changed(this);
		}
	}
	
	public ArrayList<LaunchHistory> getEntries(){ return entries; }
	public String getPath(){ return path; }
	
	public void setDirty(boolean dirty){ this.dirty = dirty; }
	public boolean isDirty(){ return dirty; }
	
	public static History load(String path) throws Exception {
	
		Application.getInstance().getLogger().debug(Module.COMMON, "load: "+path);
		XStream xstream = new XStream(new DomDriver());
		String xml = FileTools.readFile(path);
		History history = (History)xstream.fromXML(xml);
		history.listeners = new ArrayList<IChangedListener>();
		history.path = path;
		history.dirty = false;
		return history;
	}
	
	public void save() throws Exception {
		
		if(isDirty()){
			Application.getInstance().getLogger().debug(Module.COMMON, "save: "+path);
			XStream xstream = new XStream(new DomDriver());
			String xml = xstream.toXML(this);
			FileTools.writeFile(path, xml, false);
			dirty = false;
			notifyListeners();
		}
	}
	
	public synchronized void add(LaunchHistory entry) throws Exception {
		
		entry.init();
		entries.add(0, entry);
		dirty = true;
		save();
		cleanup();
		createIndex();
	}
	
	public synchronized void delete(String id) {
		
		for(int i=0; i<entries.size(); i++){
			LaunchHistory entry = entries.get(i);
			if(entry.historyId.equals(id)){
				delete(entry);
			}
		}
		createIndex();
	}
	
	/** clear old entries */
	public synchronized void clear() {
		
		for(int i = entries.size()-1; i>=0; i--){
			delete(entries.get(i));
		}
		createIndex();
	}

	/** recreate the main index-page */
	public void createIndex(){
		
		HistoryPage page = new HistoryPage(this, getIndexPath());
		try{
			page.create();
		}catch(Exception e){
			Application.getInstance().getLogger().error(Module.COMMON, e);
		}
	}
	
	public String getIndexPath() {
		return 
			Application.getInstance().getFileManager().getHistoryFolderPath()+
			File.separator+HistoryPage.OUTPUT_FILE;
	}
{'fraction_non_alphanumeric': 0.12532851511169513, 'fraction_numerical': 0.001314060446780552, 'mean_word_length': 4.4366071428571425, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '1740090', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1781, 'n_words': 460}