use of com.nolanlawson.keepscore.util.CollectionUtil.Function in project KeepScore by nolanlawson.
the class SdcardHelper method saveSpreadsheet.
/**
* Write CSV file with games on the X axis and players (and basic game data) on the Y axis.
*
* @param filename
* @param games
* @return
*/
public static void saveSpreadsheet(String filename, List<Game> games, final Context context) {
// get all the unique player names so we can put them on the X axis
SortedSet<String> playerNames = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
// special value for players whose names weren't specified
for (Game game : games) {
for (PlayerScore playerScore : game.getPlayerScores()) {
playerNames.add(playerScore.toDisplayName(context).toString());
}
}
// sort by start date descending
List<Game> sortedGames = new ArrayList<Game>(games);
Collections.sort(sortedGames, new Comparator<Game>() {
@Override
public int compare(Game lhs, Game rhs) {
return Long.valueOf(rhs.getDateStarted()).compareTo(lhs.getDateStarted());
}
});
File file = new File(getDirectory(Location.Spreadsheets), filename);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
// write the column names
List<String> columnNames = new ArrayList<String>(Arrays.asList(context.getResources().getStringArray(R.array.share_spreadsheet_column_names)));
columnNames.addAll(playerNames);
writer.write(CSVUtil.convertToLine(columnNames));
// write each game as a line in the CSV
for (Game game : sortedGames) {
List<String> entries = new ArrayList<String>();
// date started
entries.add(CSV_DATE_FORMAT.format(new Date(game.getDateStarted())));
// date saved
entries.add(CSV_DATE_FORMAT.format(new Date(game.getDateSaved())));
// play time, using duration format HH:MM:SS
long duration = (game.getDateSaved() - game.getDateStarted()) / 1000;
entries.add(String.format("%02d:%02d:%02d", duration / 3600, (duration % 3600) / 60, duration % 60));
// num players
entries.add(Integer.toString(game.getPlayerScores().size()));
// num rounds
entries.add(Integer.toString(CollectionUtil.max(game.getPlayerScores(), Functions.PLAYER_SCORE_TO_HISTORY_SIZE)));
// game name
entries.add(game.getName());
Function<PlayerScore, String> playerScoreToName = new Function<PlayerScore, String>() {
@Override
public String apply(PlayerScore obj) {
return obj.toDisplayName(context).toString();
}
};
// player(s) with max
entries.add(TextUtils.join(", ", CollectionUtil.transform(CollectionUtil.maxWithTies(game.getPlayerScores(), Functions.PLAYER_SCORE_TO_SCORE), playerScoreToName)));
// player(s) with min
entries.add(TextUtils.join(", ", CollectionUtil.transform(CollectionUtil.minWithTies(game.getPlayerScores(), Functions.PLAYER_SCORE_TO_SCORE), playerScoreToName)));
// rest of columns are just all the player names, so add blank for irrelevant players
// or the score for the actual players
Map<String, Long> playerScoreLookup = new HashMap<String, Long>();
for (PlayerScore playerScore : game.getPlayerScores()) {
playerScoreLookup.put(playerScore.toDisplayName(context).toString(), playerScore.getScore());
}
for (String playerName : playerNames) {
Long score = playerScoreLookup.get(playerName);
entries.add(score != null ? Long.toString(score) : null);
}
writer.write(CSVUtil.convertToLine(entries));
}
} catch (IOException e) {
log.e(e, "unexpected error");
throw new RuntimeException(e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
log.e(e, "unexpected error");
throw new RuntimeException(e);
}
}
}
}
use of com.nolanlawson.keepscore.util.CollectionUtil.Function in project KeepScore by nolanlawson.
the class LineChartView method updatePaints.
private void updatePaints() {
linePaints = CollectionUtil.transform(lineColors, new Function<Integer, Paint>() {
@Override
public Paint apply(Integer color) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(lineWidth);
paint.setStyle(Style.FILL_AND_STROKE);
paint.setTextSize(fontSize);
return paint;
}
});
lineLabelPaints = CollectionUtil.transform(lineColors, new Function<Integer, Paint>() {
@Override
public Paint apply(Integer color) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(fontSize);
return paint;
}
});
}
Aggregations