package com.rubenvandeven.emotionhero; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; /** * Created by ruben on 19/08/16. */ public class ScoreList extends ArrayList{ public boolean add(Score score) { boolean ret = super.add(score); int id = super.indexOf(score); score.id = id; return ret; } /** * Check if given highscore is highest hit in this set. * @param score * @return */ public boolean isHighest(Score score) { for(Score s: this) { if(s == score) // to allow comparison of items that are already in the set continue; // skip if it wants to compare with self. if(s.score > score.score) { return false; } } return true; } /** * Get the n highest hits */ public ScoreList getTopN(int n) { Collections.sort(this, new Comparator() { @Override public int compare(Score score1, Score score2) { // return highest first return score1.score > score2.score ? -1 : score1.score == score2.score ? 0 : 1; } }); if(n > this.size()) n = this.size(); ScoreList scores = new ScoreList(); for (int i = 0; i < n; i++) { scores.add(this.get(i)); } return scores; } /** * Get the n highest hits * @todo only for current level!! */ public ScoreList getLast(Integer n) { Collections.sort(this, new Comparator() { @Override public int compare(Score score1, Score score2) { // return newest first return score1.time.before(score2.time) ? -1 : score1.time == score2.time ? 0 : 1; } }); if(n == null) { // no limit return this; } if(n > this.size()) n = this.size(); ScoreList scores = new ScoreList(); for (int i = 0; i < n; i++) { scores.add(this.get(i)); } return scores; } }