package sizzle.functions;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import sizzle.types.Code.Revision;

/**
 * Boa domain-specific functions.
 * 
 * @author rdyer
 */
public class BoaIntrinsics {
	private final static String[] fixingRegex = {
		"\\s+(fix|fixes|fixing|fixed)\\s+",
		"(bug|issue)(s)?[\\s]+(#)?\\s*[0-9]+",
		"(bug|issue)\\s+id(s)?\\s*=\\s*[0-9]+"
	};

	private final static List<Matcher> fixingMatchers = new ArrayList<Matcher>();

	static {
		for (final String s : BoaIntrinsics.fixingRegex)
			fixingMatchers.add(Pattern.compile(s).matcher(""));
	}

	/**
	 * Is a log message indicating it is a fixing revision?
	 * 
	 * @param log the revision's log message to mine
	 * @return true if the log indicates a fixing revision
	 */
	@FunctionSpec(name = "isfixingrevision", returnType = "bool", formalParameters = { "string" })
	public static boolean isfixingrevision(final String log) {
		for (final Matcher m : fixingMatchers)
			if (m.reset(log).matches())
				return true;

		return false;
	}

	/**
	 * Does a Revision contain a file of the specified type? This only compares based on file extension.
	 * 
	 * @param rev the Revision to examine
	 * @param ext the file extension to look for
	 * @return true if the Revision contains at least 1 file with the specified extension
	 */
	@FunctionSpec(name = "hasfiletype", returnType = "bool", formalParameters = { "Revision", "string" })
	public static boolean hasfile(final Revision rev, final String ext) {
		for (int i = 0; i < rev.getFilesCount(); i++)
			if (rev.getFiles(i).getName().toLowerCase().endsWith("." + ext.toLowerCase()))
				return true;
		return false;
	}
}

@@ -170,16 +170,54 @@
 evision;
+%0Aimport sizzle.types.Diff.ChangedFile;
 %0A%0A/**%0A *
@@ -1746,11 +1746,527 @@
 alse;%0A%09%7D
+%0A%0A%09/**%0A%09 * Matches a *Kind enum to the given string.%0A%09 * %0A%09 * @param s the string to match against%0A%09 * @param kind the *Kind to match%0A%09 * @return true if the string matches the given *Kind%0A%09 */%0A%09@FunctionSpec(name = %22iskind%22, returnType = %22bool%22, formalParameters = %7B %22string%22, %22int%22 %7D)%0A%09public static boolean iskind(final String s, final Object kind) %7B%0A%09%09if (kind instanceof ChangedFile.FileKind)%0A%09%09%09return ((ChangedFile.FileKind)kind).name().startsWith(s);%0A%09%09throw new RuntimeException(%22Invalid argument type%22);%0A%09%7D
 %0A%7D%0A

add an iskind intrinsic to match enums using a string