Permalink
Browse files

Added LeaderData class and tests. Added leadersIn method and tests.

  • Loading branch information...
1 parent 6cd2931 commit 6d6b8c94e5dd96affcedc19bec5170cc416c48e0 @czarneckid czarneckid committed Jun 26, 2011
@@ -0,0 +1,38 @@
+package com.agoragames.leaderboard;
+
+public class LeaderData {
+
+ private String _member;
+ private Double _score;
+ private Long _rank;
+
+ public LeaderData(String member, double score, long rank) {
+ _member = member;
+ _score = score;
+ _rank = rank;
+ }
+
+ public void setMember(String member) {
+ _member = member;
+ }
+
+ public String getMember() {
+ return _member;
+ }
+
+ public void setScore(double score) {
+ _score = score;
+ }
+
+ public double getScore() {
+ return _score;
+ }
+
+ public void setRank(long rank) {
+ _rank = rank;
+ }
+
+ public long getRank() {
+ return _rank;
+ }
+}
@@ -1,8 +1,13 @@
package com.agoragames.leaderboard;
+import java.util.ArrayList;
import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
import redis.clients.jedis.Jedis;
+import redis.clients.jedis.Tuple;
public class Leaderboard {
@@ -30,6 +35,10 @@ public Leaderboard(String leaderboardName, String host, int port, int pageSize)
_jedis = new Jedis(host, port);
}
+ public String getLeaderboardName() {
+ return _leaderboardName;
+ }
+
public int getPageSize() {
return _pageSize;
}
@@ -138,5 +147,46 @@ public long removeMembersInScoreRangeIn(String leaderboardName, double minScore,
data.put("rank", rankForIn(_leaderboardName, member, useZeroIndexForRank));
return data;
- }
+ }
+
+ public List<LeaderData> leadersIn(int currentPage, boolean useZeroIndexForRank) {
+ return leadersIn(_leaderboardName, currentPage, useZeroIndexForRank, _pageSize);
+ }
+
+ public List<LeaderData> leadersIn(String leaderboardName, int currentPage, boolean useZeroIndexForRank, int pageSize) {
+ if (currentPage < 1) {
+ currentPage = 1;
+ }
+
+ if (pageSize < 1) {
+ pageSize = DEFAULT_PAGE_SIZE;
+ }
+
+ if (currentPage > totalPagesIn(leaderboardName, pageSize)) {
+ currentPage = totalPagesIn(leaderboardName, pageSize);
+ }
+
+ int indexForRedis = currentPage - 1;
+ int startingOffset = indexForRedis * pageSize;
+ if (startingOffset < 0) {
+ startingOffset = 0;
+ }
+ int endingOffset = (startingOffset + pageSize) - 1;
+
+ Set<Tuple> rawLeaderData = _jedis.zrevrangeWithScores(leaderboardName, startingOffset, endingOffset);
+ return massageLeaderData(leaderboardName, rawLeaderData, useZeroIndexForRank);
+ }
+
+ private List<LeaderData> massageLeaderData(String leaderboardName, Set<Tuple> memberData, boolean useZeroIndexForRank) {
+ List<LeaderData> leaderData = new ArrayList<LeaderData>();
+
+ Iterator<Tuple> memberDataIterator = memberData.iterator();
+ while (memberDataIterator.hasNext()) {
+ Tuple memberDataTuple = memberDataIterator.next();
+ LeaderData leaderDataItem = new LeaderData(memberDataTuple.getElement(), memberDataTuple.getScore(), rankForIn(leaderboardName, memberDataTuple.getElement(), useZeroIndexForRank));
+ leaderData.add(leaderDataItem);
+ }
+
+ return leaderData;
+ }
}
@@ -0,0 +1,31 @@
+package com.agoragames.leaderboard;
+
+import junit.framework.TestCase;
+
+public class LeaderDataTest extends TestCase {
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testLeaderDataMethods() {
+ LeaderData leader = new LeaderData("member", 1, 2);
+
+ assertEquals("member", leader.getMember());
+ assertEquals(1.0, leader.getScore());
+ assertEquals(2, leader.getRank());
+
+ leader.setMember("new_member");
+ assertEquals("new_member", leader.getMember());
+
+ leader.setScore(2);
+ assertEquals(2.0, leader.getScore());
+
+ leader.setRank(5);
+ assertEquals(5, leader.getRank());
+ }
+}
@@ -1,6 +1,7 @@
package com.agoragames.leaderboard;
import java.util.Hashtable;
+import java.util.List;
import junit.framework.TestCase;
import redis.clients.jedis.Jedis;
@@ -29,6 +30,10 @@ public void testVersion() {
assertEquals("1.0.0", Leaderboard.VERSION);
}
+ public void testGetLeaderboardName() {
+ assertEquals("name", _leaderboard.getLeaderboardName());
+ }
+
public void testSetPageSize() {
_leaderboard.setPageSize(10);
assertEquals(10, _leaderboard.getPageSize());
@@ -110,6 +115,38 @@ public void testScoreAndRankFor() {
assertEquals(5, ((Long) data.get("rank")).longValue());
}
+ public void testLeadersIn() {
+ addMembersToLeaderboard(25);
+
+ List<LeaderData> leaders = _leaderboard.leadersIn(1, false);
+ assertEquals(25, leaders.size());
+ assertEquals("member_25", leaders.get(0).getMember());
+ assertEquals("member_1", leaders.get(leaders.size() - 1).getMember());
+ assertEquals(1, (int) leaders.get(leaders.size() - 1).getScore());
+ assertEquals(25, leaders.get(leaders.size() - 1).getRank());
+ }
+
+ public void testLeadersWithMultiplePages() {
+ addMembersToLeaderboard(Leaderboard.DEFAULT_PAGE_SIZE * 3 + 1);
+
+ assertEquals(Leaderboard.DEFAULT_PAGE_SIZE * 3 + 1, _leaderboard.totalMembers());
+
+ List<LeaderData> leaders = _leaderboard.leadersIn(1, false);
+ assertEquals(_leaderboard.getPageSize(), leaders.size());
+
+ leaders = _leaderboard.leadersIn(2, false);
+ assertEquals(_leaderboard.getPageSize(), leaders.size());
+
+ leaders = _leaderboard.leadersIn(3, false);
+ assertEquals(_leaderboard.getPageSize(), leaders.size());
+
+ leaders = _leaderboard.leadersIn(4, false);
+ assertEquals(1, leaders.size());
+
+ leaders = _leaderboard.leadersIn(_leaderboard.getLeaderboardName(), 1, false, 10);
+ assertEquals(10, leaders.size());
+ }
+
private void addMembersToLeaderboard(int totalMembers) {
for (int i = 1; i <= totalMembers; i++) {
_leaderboard.addMember("member_" + i, i);

0 comments on commit 6d6b8c9

Please sign in to comment.