/*
* Copyright (C) 2011 Matt Van Der Westhuizen <mattpwest@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.chaoticengine.cgll.score;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
/**
*
* @author Matt Van Der Westhuizen <mattpwest@gmail.com>
*/
@Root(name="highscores")
public class HighScoreList {
protected ArrayList<Score> highScores = new ArrayList<Score>();
protected int positions = 10;
@ElementList(name="scores", entry="score", inline=true)
public ArrayList<Score> getHighScores() {
return highScores;
}
@ElementList(name="scores", entry="score", inline=true)
public void setHighScores(ArrayList<Score> highScores) {
this.highScores = highScores;
Collections.sort(highScores, new ScoreComparator());
if (highScores.size() > positions) {
while (highScores.size() > positions) {
highScores.remove(highScores.size() - 1);
}
}
}
public int getPositions() {
return positions;
}
public void setPositions(int positions) {
this.positions = positions;
}
/**
* @return True if sc was a highscore, otherwise false.
*/
public boolean addHighScore(Score sc) {
highScores.add(sc);
Collections.sort(highScores, new ScoreComparator());
while (highScores.size() > positions) {
highScores.remove(highScores.size() - 1);
}
if (highScores.contains(sc)) {
return(true);
} else {
return(false);
}
}
public class ScoreComparator implements Comparator<Score> {
public int compare(Score o1, Score o2) {
if (o1.getValue() > o2.getValue()) {
return(-1);
} else if (o1.getValue() < o2.getValue()) {
return(1);
} else {
return(0);
}
}
}
}
|