<commit_before>package org.strangeforest.tcb.stats.model;

import java.util.*;

import static org.strangeforest.tcb.stats.model.StatsCategory.Type.*;

public final class StatsCategory {

	public enum Type {COUNT, PERCENTAGE, RATIO}


	// Factory

	private static final Map<String, StatsCategory> CATEGORIES = new HashMap<>();
	private static final Map<String, List<StatsCategory>> CATEGORY_CLASSES = new LinkedHashMap<>();

	private static final String BREAK_POINTS_CONVERTED_PCT = "(o_bp_fc - o_bp_sv)::REAL / o_bp_fc";
	private static final String RETURN_POINTS_WON_PCT = "(o_sv_pt - o_1st_won - o_2nd_won)::REAL / o_sv_pt";
	private static final String RETURN_GAMES_WON_PCT = "CASE WHEN o_sv_gms > 0 THEN (o_bp_fc - o_bp_sv)::REAL / o_sv_gms ELSE NULL END";
	private static final String TOTAL_POINTS_WON = "p_1st_won + p_2nd_won + o_sv_pt - o_1st_won - o_2nd_won";
	private static final String TOTAL_POINTS_WON_PCT = "(" + TOTAL_POINTS_WON + ")::REAL / (p_sv_pt + o_sv_pt)";
	private static final String MATCHES_WON_PCT = "p_matches::REAL / (p_matches + o_matches)";
	private static final String POINTS_DOMINANCE_RATIO = "(" + RETURN_POINTS_WON_PCT + ") / ((p_sv_pt - p_1st_won - p_2nd_won)::REAL / p_sv_pt)";
	private static final String GAMES_DOMINANCE_RATIO = "CASE WHEN p_sv_gms > 0 THEN (" + RETURN_GAMES_WON_PCT + ") / ((p_bp_fc - p_bp_sv)::REAL / p_sv_gms) ELSE NULL END";
	private static final String BREAK_POINTS_RATIO = "CASE WHEN p_bp_fc > 0 AND o_bp_fc > 0 THEN (" + BREAK_POINTS_CONVERTED_PCT + ") / ((p_bp_fc - p_bp_sv)::REAL / p_bp_fc) ELSE NULL END";
	private static final String OVER_PERFORMING_RATIO = "(" + MATCHES_WON_PCT + ") / (" + TOTAL_POINTS_WON_PCT + ")";

	private static final String SERVE = "Serve";
	private static final String RETURN = "Return";
	private static final String TOTAL = "Total";

	static {
		// Serve
		addCategory(SERVE, "aces", "p_ace", COUNT, true, "Aces");
		addCategory(SERVE, "acePct", "p_ace::REAL / p_sv_pt", PERCENTAGE, true, "Ace %");
		addCategory(SERVE, "acesPerMatch", "p_ace::REAL / (p_matches + o_matches)", RATIO, true, "Aces per Match");
		addCategory(SERVE, "acesPerSvcGame", "CASE WHEN p_sv_gms > 0 THEN p_ace::REAL / p_sv_gms ELSE NULL END", RATIO, true, "Aces per Svc. Game");
		addCategory(SERVE, "doubleFault", "p_df", COUNT, true, "Double Faults");
		addCategory(SERVE, "doubleFaultPct", "p_df::REAL / p_sv_pt", PERCENTAGE, true, "Double Fault %");
		addCategory(SERVE, "dfsPerMatch", "p_df::REAL / (p_matches + o_matches)", RATIO, true, "DFs per Match");
		addCategory(SERVE, "dfsPerSvcGame", "CASE WHEN p_sv_gms > 0 THEN p_df::REAL / p_sv_gms ELSE NULL END", RATIO, true, "DFs per Svc. Game");
		addCategory(SERVE, "acesDfsRatio", "CASE WHEN p_df > 0 THEN p_ace::REAL / p_df ELSE NULL END", RATIO, true, "Aces / DFs Ratio");
		addCategory(SERVE, "firstServePct", "p_1st_in::REAL / p_sv_pt", PERCENTAGE, true, "1st Serve %");
		addCategory(SERVE, "firstServeWonPct", "p_1st_won::REAL / p_1st_in", PERCENTAGE, true, "1st Serve Won %");
		addCategory(SERVE, "secondServeWonPct", "p_2nd_won::REAL / (p_sv_pt - p_1st_in)", PERCENTAGE, true, "2nd Serve Won %");
		addCategory(SERVE, "breakPointsSavedPct", "CASE WHEN p_bp_fc > 0 THEN p_bp_sv::REAL / p_bp_fc ELSE NULL END", PERCENTAGE, true, "Break Points Saved %");
		addCategory(SERVE, "bpsPerSvcGame", "CASE WHEN p_sv_gms > 0 THEN p_bp_fc::REAL / p_sv_gms ELSE NULL END", RATIO, true, "BPs per Svc. Game");
		addCategory(SERVE, "servicePointsWonPct", "(p_1st_won + p_2nd_won)::REAL / p_sv_pt", PERCENTAGE, true, "Service Pts. Won %");
		addCategory(SERVE, "serviceGamesWonPct", "CASE WHEN p_sv_gms > 0 THEN (p_sv_gms - (p_bp_fc - p_bp_sv))::REAL / p_sv_gms ELSE NULL END", PERCENTAGE, true, "Service Games Won %");
		// Return
		addCategory(RETURN, "aceAgainst", "o_ace", COUNT, true, "Ace Against");
		addCategory(RETURN, "aceAgainstPct", "o_ace::REAL / o_sv_pt", PERCENTAGE, true, "Ace Against %");
		addCategory(RETURN, "doubleFaultAgainst", "o_df", COUNT, true, "Dbl. Flt. Against");
		addCategory(RETURN, "doubleFaultAgainstPct", "o_df::REAL / o_sv_pt", PERCENTAGE, true, "Dbl. Flt. Against %");
		addCategory(RETURN, "firstServeReturnWonPct", "(o_1st_in - o_1st_won)::REAL / o_1st_in", PERCENTAGE, true, "1st Srv. Rtn. Won %");
		addCategory(RETURN, "secondServeReturnWonPct", "(o_sv_pt - o_1st_in - o_2nd_won)::REAL / (o_sv_pt - o_1st_in)", PERCENTAGE, true, "2nd Srv. Rtn. Won %");
		addCategory(RETURN, "breakPointsPct", "CASE WHEN o_bp_fc > 0 THEN " + BREAK_POINTS_CONVERTED_PCT + " ELSE NULL END", PERCENTAGE, true, "Break Points Won %");
		addCategory(RETURN, "bpsPerRtnGame", "CASE WHEN o_sv_gms > 0 THEN o_bp_fc::REAL / o_sv_gms ELSE NULL END", RATIO, true, "BPs per Rtn. Game");
		addCategory(RETURN, "returnPointsWonPct", RETURN_POINTS_WON_PCT, PERCENTAGE, true, "Return Pts. Won %");
		addCategory(RETURN, "returnGamesWonPct", RETURN_GAMES_WON_PCT, PERCENTAGE, true, "Return Games Won %");
		// Total
		addCategory(TOTAL, "pointsDominanceRatio", POINTS_DOMINANCE_RATIO, RATIO, true, "Points Dominance", "stats.pointsDominanceRatio.title");
		addCategory(TOTAL, "gamesDominanceRatio", GAMES_DOMINANCE_RATIO, RATIO, true, "Games Dominance", "stats.gamesDominanceRatio.title");
		addCategory(TOTAL, "breakPointsRatio", BREAK_POINTS_RATIO, RATIO, true, "Brk. Pts. Ratio", "stats.breakPointsRatio.title");
		addCategory(TOTAL, "overPerformingRatio", OVER_PERFORMING_RATIO, RATIO, true, "Over-Performing", "stats.overPerformingRatio.title");
		addCategory(TOTAL, "totalPoints", "p_sv_pt + o_sv_pt", COUNT, true, "Total Pts. Played");
		addCategory(TOTAL, "totalPointsWon", TOTAL_POINTS_WON, COUNT, true, "Total Pts. Won");
		addCategory(TOTAL, "totalPointsWonPct", TOTAL_POINTS_WON_PCT, PERCENTAGE, true, "Total Pts. Won %");
		addCategory(TOTAL, "totalGames", "p_games + o_games", COUNT, false, "Total Games Played");
		addCategory(TOTAL, "totalGamesWon", "p_games", COUNT, false, "Total Games Won");
		addCategory(TOTAL, "totalGamesWonPct", "p_games::REAL / (p_games+o_games)", PERCENTAGE, false, "Total Games Won %");
		addCategory(TOTAL, "sets", "p_sets + o_sets", COUNT, false, "Sets Played");
		addCategory(TOTAL, "setsWon", "p_sets", COUNT, false, "Sets Won");
		addCategory(TOTAL, "setsWonPct", "p_sets::REAL / (p_sets+o_sets)", PERCENTAGE, false, "Sets Won %");
		addCategory(TOTAL, "matches", "p_matches + o_matches", COUNT, false, "Matches Played");
		addCategory(TOTAL, "matchesWon", "p_matches", COUNT, false, "Matches Won");
		addCategory(TOTAL, "matchesWonPct", MATCHES_WON_PCT, PERCENTAGE, false, "Matches Won %");
	}

	private static void addCategory(String categoryClass, String name, String expression, StatsCategory.Type type, boolean needsStats, String title) {
		addCategory(categoryClass, name, expression, type, needsStats, title, null);
	}
	private static void addCategory(String categoryClass, String name, String expression, StatsCategory.Type type, boolean needsStats, String title, String descriptionId) {
		StatsCategory category = new StatsCategory(name, expression, type, needsStats, title, descriptionId);
		CATEGORIES.put(name, category);
		CATEGORY_CLASSES.computeIfAbsent(categoryClass, catCls -> new ArrayList<>()).add(category);
	}

	public static StatsCategory get(String category) {
		StatsCategory statsCategory = CATEGORIES.get(category);
		if (statsCategory == null)
			throw new IllegalArgumentException("Unknown statistics category: " + category);
		return statsCategory;
	}

	public static Map<String, StatsCategory> getCategories() {
		return CATEGORIES;
	}

	public static Map<String, List<StatsCategory>> getCategoryClasses() {
		return CATEGORY_CLASSES;
	}


	// Instance<commit_msg>Tweaks<commit_after>package org.strangeforest.tcb.stats.model;

import java.util.*;

import static org.strangeforest.tcb.stats.model.StatsCategory.Type.*;

public final class StatsCategory {

	public enum Type {COUNT, PERCENTAGE, RATIO}


	// Factory

	private static final Map<String, StatsCategory> CATEGORIES = new HashMap<>();
	private static final Map<String, List<StatsCategory>> CATEGORY_CLASSES = new LinkedHashMap<>();

	private static final String TOTAL_POINTS = "p_sv_pt + o_sv_pt";
	private static final String TOTAL_GAMES = "p_games + o_games";
	private static final String TOTAL_SETS = "p_sets + o_sets";
	private static final String TOTAL_MATCHES = "p_matches + o_matches";
	private static final String BREAK_POINTS_CONVERTED_PCT = "(o_bp_fc - o_bp_sv)::REAL / o_bp_fc";
	private static final String RETURN_POINTS_WON_PCT = "(o_sv_pt - o_1st_won - o_2nd_won)::REAL / o_sv_pt";
	private static final String RETURN_GAMES_WON_PCT = "CASE WHEN o_sv_gms > 0 THEN (o_bp_fc - o_bp_sv)::REAL / o_sv_gms ELSE NULL END";
	private static final String TOTAL_POINTS_WON = "p_1st_won + p_2nd_won + o_sv_pt - o_1st_won - o_2nd_won";
	private static final String TOTAL_POINTS_WON_PCT = "(" + TOTAL_POINTS_WON + ")::REAL / (" + TOTAL_POINTS + ")";
	private static final String MATCHES_WON_PCT = "p_matches::REAL / (" + TOTAL_MATCHES + ")";
	private static final String POINTS_DOMINANCE_RATIO = "(" + RETURN_POINTS_WON_PCT + ") / ((p_sv_pt - p_1st_won - p_2nd_won)::REAL / p_sv_pt)";
	private static final String GAMES_DOMINANCE_RATIO = "CASE WHEN p_sv_gms > 0 THEN (" + RETURN_GAMES_WON_PCT + ") / ((p_bp_fc - p_bp_sv)::REAL / p_sv_gms) ELSE NULL END";
	private static final String BREAK_POINTS_RATIO = "CASE WHEN p_bp_fc > 0 AND o_bp_fc > 0 THEN (" + BREAK_POINTS_CONVERTED_PCT + ") / ((p_bp_fc - p_bp_sv)::REAL / p_bp_fc) ELSE NULL END";
	private static final String OVER_PERFORMING_RATIO = "(" + MATCHES_WON_PCT + ") / (" + TOTAL_POINTS_WON_PCT + ")";

	private static final String SERVE = "Serve";
	private static final String RETURN = "Return";
	private static final String TOTAL = "Total";

	static {
		// Serve
		addCategory(SERVE, "aces", "p_ace", COUNT, true, "Aces");
		addCategory(SERVE, "acePct", "p_ace::REAL / p_sv_pt", PERCENTAGE, true, "Ace %");
		addCategory(SERVE, "acesPerSvcGame", "CASE WHEN p_sv_gms > 0 THEN p_ace::REAL / p_sv_gms ELSE NULL END", RATIO, true, "Aces per Svc. Game");
		addCategory(SERVE, "acesPerMatch", "p_ace::REAL / (" + TOTAL_MATCHES + ")", RATIO, true, "Aces per Match");
		addCategory(SERVE, "doubleFault", "p_df", COUNT, true, "Double Faults");
		addCategory(SERVE, "doubleFaultPct", "p_df::REAL / p_sv_pt", PERCENTAGE, true, "Double Fault %");
		addCategory(SERVE, "dfsPerSvcGame", "CASE WHEN p_sv_gms > 0 THEN p_df::REAL / p_sv_gms ELSE NULL END", RATIO, true, "DFs per Svc. Game");
		addCategory(SERVE, "dfsPerMatch", "p_df::REAL / (" + TOTAL_MATCHES + ")", RATIO, true, "DFs per Match");
		addCategory(SERVE, "acesDfsRatio", "CASE WHEN p_df > 0 THEN p_ace::REAL / p_df ELSE NULL END", RATIO, true, "Aces / DFs Ratio");
		addCategory(SERVE, "firstServePct", "p_1st_in::REAL / p_sv_pt", PERCENTAGE, true, "1st Serve %");
		addCategory(SERVE, "firstServeWonPct", "p_1st_won::REAL / p_1st_in", PERCENTAGE, true, "1st Serve Won %");
		addCategory(SERVE, "secondServeWonPct", "p_2nd_won::REAL / (p_sv_pt - p_1st_in)", PERCENTAGE, true, "2nd Serve Won %");
		addCategory(SERVE, "breakPointsSavedPct", "CASE WHEN p_bp_fc > 0 THEN p_bp_sv::REAL / p_bp_fc ELSE NULL END", PERCENTAGE, true, "Break Points Saved %");
		addCategory(SERVE, "bpsPerSvcGame", "CASE WHEN p_sv_gms > 0 THEN p_bp_fc::REAL / p_sv_gms ELSE NULL END", RATIO, true, "BPs per Svc. Game");
		addCategory(SERVE, "bpsFacedPerMatch", "p_bp_fc::REAL / (" + TOTAL_MATCHES + ")", RATIO, true, "BPs Faced per Match");
		addCategory(SERVE, "servicePointsWonPct", "(p_1st_won + p_2nd_won)::REAL / p_sv_pt", PERCENTAGE, true, "Service Pts. Won %");
		addCategory(SERVE, "serviceGamesWonPct", "CASE WHEN p_sv_gms > 0 THEN (p_sv_gms - (p_bp_fc - p_bp_sv))::REAL / p_sv_gms ELSE NULL END", PERCENTAGE, true, "Service Games Won %");
		// Return
		addCategory(RETURN, "aceAgainst", "o_ace", COUNT, true, "Ace Against");
		addCategory(RETURN, "aceAgainstPct", "o_ace::REAL / o_sv_pt", PERCENTAGE, true, "Ace Against %");
		addCategory(RETURN, "doubleFaultAgainst", "o_df", COUNT, true, "Dbl. Flt. Against");
		addCategory(RETURN, "doubleFaultAgainstPct", "o_df::REAL / o_sv_pt", PERCENTAGE, true, "Dbl. Flt. Against %");
		addCategory(RETURN, "firstServeReturnWonPct", "(o_1st_in - o_1st_won)::REAL / o_1st_in", PERCENTAGE, true, "1st Srv. Rtn. Won %");
		addCategory(RETURN, "secondServeReturnWonPct", "(o_sv_pt - o_1st_in - o_2nd_won)::REAL / (o_sv_pt - o_1st_in)", PERCENTAGE, true, "2nd Srv. Rtn. Won %");
		addCategory(RETURN, "breakPointsPct", "CASE WHEN o_bp_fc > 0 THEN " + BREAK_POINTS_CONVERTED_PCT + " ELSE NULL END", PERCENTAGE, true, "Break Points Won %");
		addCategory(RETURN, "bpsPerRtnGame", "CASE WHEN o_sv_gms > 0 THEN o_bp_fc::REAL / o_sv_gms ELSE NULL END", RATIO, true, "BPs per Rtn. Game");
		addCategory(RETURN, "bpsPerMatch", "o_bp_fc::REAL / (" + TOTAL_MATCHES + ")", RATIO, true, "BPs per Match");
		addCategory(RETURN, "returnPointsWonPct", RETURN_POINTS_WON_PCT, PERCENTAGE, true, "Return Pts. Won %");
		addCategory(RETURN, "returnGamesWonPct", RETURN_GAMES_WON_PCT, PERCENTAGE, true, "Return Games Won %");
		// Total
		addCategory(TOTAL, "pointsDominanceRatio", POINTS_DOMINANCE_RATIO, RATIO, true, "Points Dominance", "stats.pointsDominanceRatio.title");
		addCategory(TOTAL, "gamesDominanceRatio", GAMES_DOMINANCE_RATIO, RATIO, true, "Games Dominance", "stats.gamesDominanceRatio.title");
		addCategory(TOTAL, "breakPointsRatio", BREAK_POINTS_RATIO, RATIO, true, "Brk. Pts. Ratio", "stats.breakPointsRatio.title");
		addCategory(TOTAL, "overPerformingRatio", OVER_PERFORMING_RATIO, RATIO, true, "Over-Performing", "stats.overPerformingRatio.title");
		addCategory(TOTAL, "totalPoints", TOTAL_POINTS, COUNT, true, "Total Pts. Played");
		addCategory(TOTAL, "totalPointsWon", TOTAL_POINTS_WON, COUNT, true, "Total Pts. Won");
		addCategory(TOTAL, "totalPointsWonPct", TOTAL_POINTS_WON_PCT, PERCENTAGE, true, "Total Pts. Won %");
		addCategory(TOTAL, "totalGames", TOTAL_GAMES, COUNT, false, "Total Games Played");
		addCategory(TOTAL, "totalGamesWon", "p_games", COUNT, false, "Total Games Won");
		addCategory(TOTAL, "totalGamesWonPct", "p_games::REAL / (" + TOTAL_GAMES + ")", PERCENTAGE, false, "Total Games Won %");
		addCategory(TOTAL, "sets", TOTAL_SETS, COUNT, false, "Sets Played");
		addCategory(TOTAL, "setsWon", "p_sets", COUNT, false, "Sets Won");
		addCategory(TOTAL, "setsWonPct", "p_sets::REAL / (" + TOTAL_SETS + ")", PERCENTAGE, false, "Sets Won %");
		addCategory(TOTAL, "matches", TOTAL_MATCHES, COUNT, false, "Matches Played");
		addCategory(TOTAL, "matchesWon", "p_matches", COUNT, false, "Matches Won");
		addCategory(TOTAL, "matchesWonPct", MATCHES_WON_PCT, PERCENTAGE, false, "Matches Won %");
	}

	private static void addCategory(String categoryClass, String name, String expression, StatsCategory.Type type, boolean needsStats, String title) {
		addCategory(categoryClass, name, expression, type, needsStats, title, null);
	}
	private static void addCategory(String categoryClass, String name, String expression, StatsCategory.Type type, boolean needsStats, String title, String descriptionId) {
		StatsCategory category = new StatsCategory(name, expression, type, needsStats, title, descriptionId);
		CATEGORIES.put(name, category);
		CATEGORY_CLASSES.computeIfAbsent(categoryClass, catCls -> new ArrayList<>()).add(category);
	}

	public static StatsCategory get(String category) {
		StatsCategory statsCategory = CATEGORIES.get(category);
		if (statsCategory == null)
			throw new IllegalArgumentException("Unknown statistics category: " + category);
		return statsCategory;
	}

	public static Map<String, StatsCategory> getCategories() {
		return CATEGORIES;
	}

	public static Map<String, List<StatsCategory>> getCategoryClasses() {
		return CATEGORY_CLASSES;
	}


	// Instance
{'fraction_non_alphanumeric': 0.13803583866269867, 'fraction_numerical': 0.00481225859558032, 'mean_word_length': 5.86695652173913, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 47, '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': '1119401', 'n_tokens_mistral': 6409, 'n_tokens_neox': 6000, 'n_words': 1406}