Class TeamCsvService
java.lang.Object
edu.ucsb.cs156.frontiers.services.TeamCsvService
Builds the compact team tables that instructors traditionally produce by hand: every student's
name abbreviated as far as it can be while staying unique, laid out in several side-by-side
column pairs (e.g. "Name,Team,,Name,Team"). The name abbreviation, the column balancing and the
CSV writing are separate steps so that different tables (name to team, team to names) can share
them.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfillColumns(List<T> items, int size) Splits the items into columns of at most the given size, filling column by column, so that every column but the last is full.Returns the first word of the given name: everything before the first whitespace, once leading and trailing whitespace has been removed.groupByTeam(List<NameAndTeam> entries) Groups the entries by team, one list per team, with the teams in case-insensitive alphabetical order by name and the entries within each team in their original order.layoutColumns(List<T> items, int columns) Splits the items into the given number of columns, filling column by column, with the column lengths as balanced as possible: every column gets items.size() / columns items, and the first items.size() % columns columns get one extra.nameAndTeams(List<RosterStudent> students) Computes the abbreviated name and team for each student, preserving the order of the input list.Converts a name to Title Case: the first letter of each word is upper case and the rest lower case.voidWrites a CSV made of several side-by-side columns separated by a blank cell.voidwriteName2TeamCsv(Writer writer, List<NameAndTeam> entries, int columns) Writes the "Name,Team" table: the entries balanced across the given number of column pairs, filling column by column.voidwriteNameAndTeamTablesCsv(Writer writer, List<NameAndTeam> entries) Writes the "Name,Team" table under "by name", then the "Team,Name" table under "by team" line.voidwriteTeamTableCsv(Writer writer, List<NameAndTeam> entries, int columns) Writes the "Team,Name" table: the teams (seegroupByTeam(List)) balanced across the given number of column pairs, filling column by column, with one row per student.
-
Field Details
-
NAMES_PER_COLUMN
public static final int NAMES_PER_COLUMN- See Also:
-
TEAMS_PER_COLUMN
public static final int TEAMS_PER_COLUMN- See Also:
-
-
Constructor Details
-
TeamCsvService
public TeamCsvService()
-
-
Method Details
-
titleCase
Converts a name to Title Case: the first letter of each word is upper case and the rest lower case. A new word starts after any non-letter character, so hyphenated names and names with apostrophes are handled (e.g. "MARY-JANE O'BRIEN" becomes "Mary-Jane O'Brien"). Leading and trailing whitespace is removed; null becomes "".- Parameters:
name- the raw name (may be null)- Returns:
- the name in Title Case
-
firstWord
Returns the first word of the given name: everything before the first whitespace, once leading and trailing whitespace has been removed. The roster's first name field often holds a first and a middle name ("Chris Edward"); we assume the first space marks the end of the first name. Null becomes "".- Parameters:
name- the raw name (may be null)- Returns:
- the first word of the name
-
nameAndTeams
Computes the abbreviated name and team for each student, preserving the order of the input list. The first name is the first word of the roster's first name field (seefirstWord(String)), so a middle name in that field is ignored. Each name is abbreviated as far as possible while remaining unique among all students:- first name only, where no other student shares that first name;
- first name plus last initial, where that is enough to be unambiguous;
- first name plus full last name, otherwise;
- first name plus full last name followed by an asterisk, when another student has exactly the same first and last name (the instructor can then disambiguate by hand).
- Parameters:
students- the roster students, already in the desired order- Returns:
- one NameAndTeam per student, in the same order
-
layoutColumns
Splits the items into the given number of columns, filling column by column, with the column lengths as balanced as possible: every column gets items.size() / columns items, and the first items.size() % columns columns get one extra.- Type Parameters:
T- the type of item being laid out (e.g. a student, or a whole team)- Parameters:
items- the items, in ordercolumns- the number of columns, at least 1- Returns:
- a list of exactly
columnslists, the first of which is never shorter than any other - Throws:
IllegalArgumentException- if columns is less than 1
-
writeColumnsCsv
public void writeColumnsCsv(Writer writer, List<String> headerCells, List<List<List<String>>> columns) throws IOException Writes a CSV made of several side-by-side columns separated by a blank cell. The header line repeatsheaderCellsonce per column; then there is one line per row of the longest column. Each column is a list of rows, and each row a list of cells with the same length asheaderCells. Rows missing from shorter columns are written as blank cells.- Parameters:
writer- where to write the CSVheaderCells- the cells of one column's header, e.g. ["Name", "Team"]columns- the columns, each a list of rows of cells- Throws:
IOException- if writing fails
-
writeName2TeamCsv
public void writeName2TeamCsv(Writer writer, List<NameAndTeam> entries, int columns) throws IOException Writes the "Name,Team" table: the entries balanced across the given number of column pairs, filling column by column.- Parameters:
writer- where to write the CSVentries- the entries, in ordercolumns- the number of "Name,Team" column pairs, at least 1- Throws:
IOException- if writing failsIllegalArgumentException- if columns is less than 1
-
groupByTeam
Groups the entries by team, one list per team, with the teams in case-insensitive alphabetical order by name and the entries within each team in their original order. Entries with no team (a blank team name) are collected into one extra group placed last, so that unassigned students are still visible.- Parameters:
entries- the entries, in order- Returns:
- one list per team, teams sorted by name, unassigned students last
-
writeTeamTableCsv
public void writeTeamTableCsv(Writer writer, List<NameAndTeam> entries, int columns) throws IOException Writes the "Team,Name" table: the teams (seegroupByTeam(List)) balanced across the given number of column pairs, filling column by column, with one row per student. Columns are balanced by number of teams, not number of students, so that a team is never split across two columns.- Parameters:
writer- where to write the CSVentries- the entries, in ordercolumns- the number of "Team,Name" column pairs, at least 1- Throws:
IOException- if writing failsIllegalArgumentException- if columns is less than 1
-
fillColumns
Splits the items into columns of at most the given size, filling column by column, so that every column but the last is full. There is always at least one column.- Type Parameters:
T- the type of item being laid out (e.g. a student, or a whole team)- Parameters:
items- the items, in ordersize- the number of items per column, at least 1- Returns:
- the columns, all of exactly
sizeitems except possibly the last
-
writeNameAndTeamTablesCsv
Writes the "Name,Team" table under "by name", then the "Team,Name" table under "by team" line. The "Name,Team" table hasNAMES_PER_COLUMNrows per column, and as many columns as it takes. The "Team,Name" table hasTEAMS_PER_COLUMNteams per column, and as many columns as it takes.- Parameters:
writer- where to write the CSVentries- the entries, in order- Throws:
IOException- if writing fails
-