Search in sources :

Example 1 with Function

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);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Function(com.nolanlawson.keepscore.util.CollectionUtil.Function) Game(com.nolanlawson.keepscore.db.Game) TreeSet(java.util.TreeSet) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PlayerScore(com.nolanlawson.keepscore.db.PlayerScore)

Example 2 with Function

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;
        }
    });
}
Also used : Function(com.nolanlawson.keepscore.util.CollectionUtil.Function) Paint(android.graphics.Paint)

Aggregations

Function (com.nolanlawson.keepscore.util.CollectionUtil.Function)2 Paint (android.graphics.Paint)1 Game (com.nolanlawson.keepscore.db.Game)1 PlayerScore (com.nolanlawson.keepscore.db.PlayerScore)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1