<commit_before>package com.github.philippecade.biketoworkstats;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Extracts data from the bike to work status file
 *
 * @author  xphc
 * @since May 4, 2016
 */
public class StatsGenerator {

	private static final String SEPARATOR = "--------";
	private static final int TEAM_NAME_COLUMN = 5;
	private static final int TEAM_KM = 49;
	private static final int TEAM_PERCENT_BY_BIKE = 46;
	private static final int MEMBER_1_EMAIL = 9;
	private static final int MEMBER_1_KM = 30;
	private static final int MEMBER_2_EMAIL = 19;
	private static final int MEMBER_2_KM = 34;
	private static final int MEMBER_3_EMAIL = 23;
	private static final int MEMBER_3_KM = 38;
	private static final int MEMBER_4_EMAIL = 27;
	private static final int MEMBER_4_KM = 42;
	
	/**
	 * Reads the status file and returns the data model
	 * @param file
	 * @return
	 * @throws IOException
	 */
	List<Team> readStatusFile(File file) throws IOException {
		List<Team> teams = new ArrayList<>();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
			// skip the first header line
			String line = reader.readLine();
			while ((line = reader.readLine()) != null) {
				String[] columns = line.split(";");
				if (columns.length < TEAM_NAME_COLUMN) {
					continue;
				}

				Team team = new Team(columns[TEAM_NAME_COLUMN]);
				team.setKm(Integer.parseInt(columns[TEAM_KM]));
				team.setByBike(Double.parseDouble(columns[TEAM_PERCENT_BY_BIKE]));

				team.addMember(getMember(columns, MEMBER_1_EMAIL, MEMBER_1_KM));
				team.addMember(getMember(columns, MEMBER_2_EMAIL, MEMBER_2_KM));
				team.addMember(getMember(columns, MEMBER_3_EMAIL, MEMBER_3_KM));
				team.addMember(getMember(columns, MEMBER_4_EMAIL, MEMBER_4_KM));
				
				teams.add(team);
			}
		}
		return teams;
	}
	
	/**
	 * Returns a member
	 * @param columns
	 * @param emailIndex
	 * @param kmIndex
	 * @return
	 */
	Member getMember(String[] columns, int emailIndex, int kmIndex) {
		Member member = new Member();
		member.setEmail(columns[emailIndex]);
		member.setName(columns[emailIndex-2]+" "+columns[emailIndex-1]);
		String km = columns[kmIndex];
		if (!km.isEmpty()) {
			member.setKm(Integer.parseInt(km));
		}
		String byBike = columns[kmIndex+3];
		if (!byBike.isEmpty()) {
			member.setByBike(Double.parseDouble(byBike));
		}
		return member;
	}
	
	/**
	 * Generates the reports for a given week number
	 * @param inputFile
	 * @return Result file
	 * @throws IOException <commit_msg>BTW: only add members if they have an e-mail address<commit_after>package com.github.philippecade.biketoworkstats;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Extracts data from the bike to work status file
 *
 * @author  xphc
 * @since May 4, 2016
 */
public class StatsGenerator {

	private static final String SEPARATOR = "--------";
	private static final int TEAM_NAME_COLUMN = 5;
	private static final int TEAM_KM = 49;
	private static final int TEAM_PERCENT_BY_BIKE = 46;
	private static final int MEMBER_1_EMAIL = 9;
	private static final int MEMBER_1_KM = 30;
	private static final int MEMBER_2_EMAIL = 19;
	private static final int MEMBER_2_KM = 34;
	private static final int MEMBER_3_EMAIL = 23;
	private static final int MEMBER_3_KM = 38;
	private static final int MEMBER_4_EMAIL = 27;
	private static final int MEMBER_4_KM = 42;
	
	/**
	 * Reads the status file and returns the data model
	 * @param file
	 * @return
	 * @throws IOException
	 */
	List<Team> readStatusFile(File file) throws IOException {
		List<Team> teams = new ArrayList<>();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
			// skip the first header line
			String line = reader.readLine();
			while ((line = reader.readLine()) != null) {
				String[] columns = line.split(";");
				if (columns.length < TEAM_NAME_COLUMN) {
					continue;
				}

				Team team = new Team(columns[TEAM_NAME_COLUMN]);
				team.setKm(Integer.parseInt(columns[TEAM_KM]));
				team.setByBike(Double.parseDouble(columns[TEAM_PERCENT_BY_BIKE]));

				addNonEmptyMember(team, getMember(columns, MEMBER_1_EMAIL, MEMBER_1_KM));
				addNonEmptyMember(team, getMember(columns, MEMBER_2_EMAIL, MEMBER_2_KM));
				addNonEmptyMember(team, getMember(columns, MEMBER_3_EMAIL, MEMBER_3_KM));
				addNonEmptyMember(team, getMember(columns, MEMBER_4_EMAIL, MEMBER_4_KM));
				
				teams.add(team);
			}
		}
		return teams;
	}
	
	private void addNonEmptyMember(Team team, Member member) {
		if (!member.getEmail().isEmpty()) {
			team.addMember(member);
		}
		
	}
	
	/**
	 * Returns a member
	 * @param columns
	 * @param emailIndex
	 * @param kmIndex
	 * @return
	 */
	Member getMember(String[] columns, int emailIndex, int kmIndex) {
		Member member = new Member();
		member.setEmail(columns[emailIndex]);
		member.setName(columns[emailIndex-2]+" "+columns[emailIndex-1]);
		String km = columns[kmIndex];
		if (!km.isEmpty()) {
			member.setKm(Integer.parseInt(km));
		}
		String byBike = columns[kmIndex+3];
		if (!byBike.isEmpty()) {
			member.setByBike(Double.parseDouble(byBike));
		}
		return member;
	}
	
	/**
	 * Generates the reports for a given week number
	 * @param inputFile
	 * @return Result file
	 * @throws IOException 
{'fraction_non_alphanumeric': 0.11395428181508017, 'fraction_numerical': 0.015353121801432957, 'mean_word_length': 4.2772277227722775, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '422408', 'n_tokens_mistral': 2175, 'n_tokens_neox': 1922, 'n_words': 496}