Package edu.ntnu.idi.idatt.filehandler
Class PlayerFileHandlerCsv
java.lang.Object
edu.ntnu.idi.idatt.filehandler.PlayerFileHandlerCsv
- All Implemented Interfaces:
FileHandler<Player>
PlayerFileHandlerCsv.
Implements the FileHandler
interface for Player
objects, providing
functionality to read player data from and write player data to CSV (Comma Separated Values)
formatted files. This handler can distinguish between LadderGamePlayer
and
LudoPlayer
types based on the CSV header and structure.
When reading, it determines the player type from the header line and parses subsequent lines accordingly. For Ludo players, it expects "name, colorHex, isBot". For Ladder Game players, it expects "name, colorHex, playerTokenType, isBot".
When writing, it generates the appropriate header based on the type of the first player in the list and then formats each player object into a CSV line.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionParses a line from a CSV file, expectingLadderGamePlayer
data format.ludoPlayerFromCsvLine
(String line) Parses a line from a CSV file, expectingLudoPlayer
data format.Reads a list ofPlayer
objects from a CSV file at the specified path.Converts aPlayer
object to its CSV string representation.void
Writes a list ofPlayer
objects to a CSV file at the specified path.
-
Constructor Details
-
PlayerFileHandlerCsv
public PlayerFileHandlerCsv()
-
-
Method Details
-
readFile
Reads a list ofPlayer
objects from a CSV file at the specified path. The method automatically detects if the players areLudoPlayer
s orLadderGamePlayer
s based on the header row in the CSV file.- Specified by:
readFile
in interfaceFileHandler<Player>
- Parameters:
path
- The path to the CSV file.- Returns:
- A list of
Player
objects deserialized from the file. Returns an empty list if a player line is malformed or if an I/O error (other than file not found) occurs. - Throws:
IOException
- if the file cannot be found or if an I/O error occurs during reading that prevents processing (e.g., permissions issues).
-
writeFile
Writes a list ofPlayer
objects to a CSV file at the specified path. The CSV header and line format are determined by the type of the first player in the list (eitherLudoPlayer
orLadderGamePlayer
).- Specified by:
writeFile
in interfaceFileHandler<Player>
- Parameters:
path
- The path to the CSV file where players will be written.players
- The list ofPlayer
objects to serialize.- Throws:
IOException
- if an error occurs during file writing (e.g., path not found, permissions).
-
ladderGamePlayerFromCsvLine
Parses a line from a CSV file, expectingLadderGamePlayer
data format. The expected format is: "name,colorHex,playerTokenType,isBot".- Parameters:
line
- The CSV line string.- Returns:
- A
LadderGamePlayer
object, ornull
if the line is malformed.
-
ludoPlayerFromCsvLine
Parses a line from a CSV file, expectingLudoPlayer
data format. The expected format is: "name,colorHex,isBot".PlayerTokenType.CIRCLE
is assumed.- Parameters:
line
- The CSV line string.- Returns:
- A
LudoPlayer
object, ornull
if the line is malformed.
-
toCsvLine
Converts aPlayer
object to its CSV string representation. The format depends on whether the player is an instance ofLudoPlayer
(name,colorHex,isBot) or anotherPlayer
type, assumed to beLadderGamePlayer
(name,colorHex,playerTokenType,isBot).- Parameters:
player
- ThePlayer
object to convert.- Returns:
- A string representing the player in CSV format.
-