<commit_before>/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2005, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs;

import edu.umd.cs.findbugs.config.CommandLine;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * Mine historical information from a BugCollection.
 * The BugCollection should be built using UpdateBugCollection
 * to record the history of analyzing all versions over time.
 * 
 * @author <NAME>
 */
public class MineBugHistory {
	static final int ADDED      = 0;
	static final int REMOVED    = 1;
	static final int RETAINED   = 2;
	static final int DEAD       = 3;
	static final int ACTIVE_NOW = 4;
	static final int TUPLE_SIZE = 5;
	
	static class Version {
		long sequence;
		int tuple[] = new int[TUPLE_SIZE];
		
		Version(long sequence) {
			this.sequence = sequence;
		}
		
		/**
		 * @return Returns the sequence.
		 */
		public long getSequence() {
			return sequence;
		}

		void increment(int key) {
			tuple[key]++;
			if (key == ADDED || key == RETAINED)
				tuple[ACTIVE_NOW]++;
		}
		
		int get(int key) {
			return tuple[key];
		}
	}
	
	BugCollection bugCollection;
	Version[] versionList;
	Map<Long, AppVersion> sequenceToAppVersionMap;
	int prio = Detector.LOW_PRIORITY;
	Set<String> categorySet;
	
	public MineBugHistory(BugCollection bugCollection) {
		this.bugCollection = bugCollection;
		this.sequenceToAppVersionMap = new HashMap<Long, AppVersion>();
		this.categorySet = new HashSet<String>();
	}

	public void setPrio(int prio) {
		this.prio = prio;
	}

	public void setCategories(String categories) {
		StringTokenizer t = new StringTokenizer(categories, ",");
		while (t.hasMoreTokens()) {
			String category = t.nextToken();
			categorySet.add(category);
		}
	}
	
	public MineBugHistory execute() {
		int maxSequence = (int)bugCollection.getSequenceNumber();
		versionList = new Version[maxSequence + 1];
		for (int i = 0; i <= maxSequence; ++i) {
			versionList[i] = new Version(i);
		}
		
		for (Iterator<AppVersion> i = bugCollection.appVersionIterator(); i.hasNext();) {
			AppVersion appVersion = i.next();
			sequenceToAppVersionMap.put(Long.valueOf(appVersion.getSequenceNumber()), appVersion);
		}
		sequenceToAppVersionMap.put(
			Long.valueOf(bugCollection.getSequenceNumber()),
			bugCollection.getCurrentAppVersion());
		
		for (Iterator<BugInstance> j = bugCollection.iterator(); j.hasNext();) {
			BugInstance bugInstance = j.next();

			if (ignore(bugInstance))
				continue;

			for (int i = 1; i <= maxSequence; ++i) {
				
				boolean activePrevious = bugInstance.getFirstVersion() < maxSequence;
				boolean activeCurrent = bugInstance.getLastVersion() == -1 || bugInstance.getLastVersion() >= maxSequence ;
				
				int key = getKey(activePrevious, activeCurrent);
				versionList[i].increment(key);
			}
		}
		
		return this;
	}

	private boolean ignore(BugInstance bugInstance) {
		if (bugInstance.getPriority() > this.prio)
			return true;
		if (!categorySet.isEmpty()
			&& !categorySet.contains(bugInstance.getBugPattern().getCategory()))
			return true;
		return false;<commit_msg>start at version 0 ignore warnings that haven't occurred yet<commit_after>/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2005, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs;

import edu.umd.cs.findbugs.config.CommandLine;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * Mine historical information from a BugCollection.
 * The BugCollection should be built using UpdateBugCollection
 * to record the history of analyzing all versions over time.
 * 
 * @author <NAME>
 */
public class MineBugHistory {
	static final int ADDED      = 0;
	static final int REMOVED    = 1;
	static final int RETAINED   = 2;
	static final int DEAD       = 3;
	static final int ACTIVE_NOW = 4;
	static final int TUPLE_SIZE = 5;
	
	static class Version {
		long sequence;
		int tuple[] = new int[TUPLE_SIZE];
		
		Version(long sequence) {
			this.sequence = sequence;
		}
		
		/**
		 * @return Returns the sequence.
		 */
		public long getSequence() {
			return sequence;
		}

		void increment(int key) {
			tuple[key]++;
			if (key == ADDED || key == RETAINED)
				tuple[ACTIVE_NOW]++;
		}
		
		int get(int key) {
			return tuple[key];
		}
	}
	
	BugCollection bugCollection;
	Version[] versionList;
	Map<Long, AppVersion> sequenceToAppVersionMap;
	int prio = Detector.LOW_PRIORITY;
	Set<String> categorySet;
	
	public MineBugHistory(BugCollection bugCollection) {
		this.bugCollection = bugCollection;
		this.sequenceToAppVersionMap = new HashMap<Long, AppVersion>();
		this.categorySet = new HashSet<String>();
	}

	public void setPrio(int prio) {
		this.prio = prio;
	}

	public void setCategories(String categories) {
		StringTokenizer t = new StringTokenizer(categories, ",");
		while (t.hasMoreTokens()) {
			String category = t.nextToken();
			categorySet.add(category);
		}
	}
	
	public MineBugHistory execute() {
		int maxSequence = (int)bugCollection.getSequenceNumber();
		versionList = new Version[maxSequence + 1];
		for (int i = 0; i <= maxSequence; ++i) {
			versionList[i] = new Version(i);
		}
		
		for (Iterator<AppVersion> i = bugCollection.appVersionIterator(); i.hasNext();) {
			AppVersion appVersion = i.next();
			sequenceToAppVersionMap.put(Long.valueOf(appVersion.getSequenceNumber()), appVersion);
		}
		sequenceToAppVersionMap.put(
			Long.valueOf(bugCollection.getSequenceNumber()),
			bugCollection.getCurrentAppVersion());
		
		for (Iterator<BugInstance> j = bugCollection.iterator(); j.hasNext();) {
			BugInstance bugInstance = j.next();

			if (ignore(bugInstance))
				continue;

			for (int i = 0; i <= maxSequence; ++i) {
				if (bugInstance.getFirstVersion() > i) continue;
				boolean activePrevious = bugInstance.getFirstVersion() < i;
				boolean activeCurrent = bugInstance.getLastVersion() == -1 || bugInstance.getLastVersion() >= maxSequence ;
				
				int key = getKey(activePrevious, activeCurrent);
				versionList[i].increment(key);
			}
		}
		
		return this;
	}

	private boolean ignore(BugInstance bugInstance) {
		if (bugInstance.getPriority() > this.prio)
			return true;
		if (!categorySet.isEmpty()
			&& !categorySet.contains(bugInstance.getBugPattern().getCategory()))
			return true;
		return false;
{'fraction_non_alphanumeric': 0.10446082718980382, 'fraction_numerical': 0.007622141696863676, 'mean_word_length': 3.931608133086876, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, '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': '3105654', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2341, 'n_words': 752}