emotionhero/app/src/main/java/com/rubenvandeven/emotionhero/ScoreList.java

90 lines
2.1 KiB
Java
Raw Normal View History

2016-08-19 21:47:15 +02:00
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<Score>{
2016-08-19 21:47:15 +02:00
public boolean add(Score score) {
boolean ret = super.add(score);
int id = super.indexOf(score);
score.id = id;
return ret;
}
2016-08-20 18:22:01 +02:00
/**
* Check if given highscore is highest hit in this set.
2016-08-20 18:22:01 +02:00
* @param score
* @return
*/
public boolean isHighest(Score score) {
for(Score s: this) {
2016-08-20 18:22:01 +02:00
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;
}
2016-08-19 21:47:15 +02:00
/**
* Get the n highest hits
2016-08-19 21:47:15 +02:00
*/
public ScoreList getTopN(int n) {
Collections.sort(this, new Comparator<Score>() {
2016-08-19 21:47:15 +02:00
@Override
public int compare(Score score1, Score score2)
2016-08-19 21:47:15 +02:00
{
2016-08-20 18:22:01 +02:00
// return highest first
return score1.score > score2.score ? -1 : score1.score == score2.score ? 0 : 1;
2016-08-19 21:47:15 +02:00
}
});
2016-08-20 18:22:01 +02:00
if(n > this.size())
n = this.size();
ScoreList scores = new ScoreList();
2016-08-20 18:22:01 +02:00
for (int i = 0; i < n; i++) {
scores.add(this.get(i));
}
return scores;
2016-08-19 21:47:15 +02:00
}
2016-08-20 18:22:01 +02:00
/**
* Get the n highest hits
* @todo only for current level!!
2016-08-20 18:22:01 +02:00
*/
public ScoreList getLast(Integer n) {
Collections.sort(this, new Comparator<Score>() {
2016-08-20 18:22:01 +02:00
@Override
public int compare(Score score1, Score score2)
2016-08-20 18:22:01 +02:00
{
// 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();
2016-08-20 18:22:01 +02:00
for (int i = 0; i < n; i++) {
scores.add(this.get(i));
}
return scores;
}
2016-08-19 21:47:15 +02:00
}