package ca.ubc.ece.salt.sdjsb.analysis.learning.apis;

import ca.ubc.ece.salt.gumtree.ast.ClassifiedASTNode.ChangeType;

/**
 * Stores a keyword and the context under which it is used (which we call its type).
 */
public class Keyword {

	/** The type of the keyword (i.e., package, method, field, constant or event). **/
	public KeywordType type;

	/** The context under which the keyword is used. **/
	public KeywordContext context;

	/** The keyword text. **/
	public String keyword;

	/** How this keyword was modified from the source to the destination file. **/
	public ChangeType changeType;

	/** The AbstractAPI which contains this keyword. **/
	public AbstractAPI api;

	public Keyword(KeywordType type, KeywordContext context, String keyword, ChangeType changeType) {
		this.type = type;
		this.context = context;
		this.keyword = keyword;
		this.changeType = changeType;
		this.api = null;
	}

	public Keyword(KeywordType type, String keyword, AbstractAPI api) {
		this.type = type;
		this.keyword = keyword;
		this.api = api;
	}

	public Keyword(KeywordType type, String keyword) {
		this.type = type;
		this.keyword = keyword;
	}

	/**
	 * Return the package name of the API this Keywords belongs to. Just
	 * delegates the call method to the API.
	 *
	 * @return the package name, "global", or null
	 */
	public String getPackageName() {
		if (api != null)
			return api.getPackageName();

		return null;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Keyword) {
			Keyword that = (Keyword) obj;

			if(this.type == that.type && this.keyword.equals(that.keyword))
				return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return this.toString().hashCode();
	}

	@Override
	public String toString() {
		return this.type.toString() + "_" + this.keyword;
	}

	/**
	 * The possible types for a keyword. Reads like "The keyword is a ... ".
	 */
	public enum KeywordType {
		UNKNOWN,
		RESERVED,
		PACKAGE,
		CLASS,
		METHOD,
		FIELD,
		CONSTANT,
		VARIABLE,
		EXCEPTION,
		EVENT
	}

	/**
	 * The possible contexts for a keyword. Reads like "The keyword is being
	 * used in a ... ".
	 */
	public enum KeywordContext {
		UNKNOWN,
		CONDITION,
		REQUIRE,
		METHOD_CALL,
		FIELD_ACCESS,
		CONSTANT_ACCESS,
		ARGUMENT,
		PARAMETER,
		EXCEPTION,
		EVENT
	}

}
@@ -48,16 +48,120 @@
 .apis;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0A%0A
 import c
@@ -2406,9 +2406,786 @@
 ENT%0A%09%7D%0A%0A
+%09/**%0A%09 * Helper method to filter a list of keywords by ChangeType. It returns a%0A%09 * new list with the filtered elements. Original list is kept untouched.%0A%09 * TODO: Should this be on this class or should we create a%0A%09 * KeywordListHelper or something?%0A%09 *%0A%09 * @param keywords list of keywords%0A%09 * @param changeTypes types of keywords that should stay%0A%09 * @return a new list%0A%09 */%0A%09public static List%3CKeyword%3E filterListByChangeType(List%3CKeyword%3E keywords, ChangeType... changeTypes) %7B%0A%09%09List%3CKeyword%3E newList = new ArrayList%3CKeyword%3E(keywords);%0A%0A%09%09for (Iterator%3CKeyword%3E iterator = newList.iterator(); iterator.hasNext();) %7B%0A%09%09%09Keyword keyword = iterator.next();%0A%0A%09%09%09if (!Arrays.asList(changeTypes).contains(keyword.changeType))%0A%09%09%09%09iterator.remove();%0A%09%09%7D%0A%0A%09%09return newList;%0A%09%7D%0A%0A
 %7D

Add method on Keyword to filter Keyword lists by its ChangeType