Created
November 29, 2012 22:36
-
-
Save angrycub/4172376 to your computer and use it in GitHub Desktop.
Resolver Informing Transient Object Variable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package basho; | |
import com.basho.riak.client.convert.RiakKey; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.TreeSet; | |
import org.codehaus.jackson.annotate.JsonIgnore; | |
public final class GameLeaderboard | |
{ | |
/* | |
* The @RiakKey annotation allows the StoreObject to extract the key you wish to use | |
* from your POJO. If you're using the default JSONConverter, this is excluded | |
* from serialization | |
*/ | |
@RiakKey private String gameName; | |
private TreeSet<NameScorePair> scoreList = new TreeSet<NameScorePair>(); | |
@JsonIgnore private boolean wasResolved = false; | |
// required by Jackson for JSON serialization | |
public GameLeaderboard() {} | |
public GameLeaderboard(String gameName) | |
{ | |
this.gameName = gameName; | |
} | |
public GameLeaderboard(GameLeaderboard other) | |
{ | |
this.gameName = other.getGameName(); | |
this.addScores(other.getScoreList()); | |
} | |
public void addScore(NameScorePair s) | |
{ | |
scoreList.add(s); | |
if (scoreList.size() > 5) | |
scoreList.pollFirst(); | |
} | |
public void addScores(Collection<NameScorePair> scores) | |
{ | |
scoreList.addAll(scores); | |
while (scoreList.size() > 5) | |
scoreList.pollFirst(); | |
} | |
public String getGameName() | |
{ | |
return gameName; | |
} | |
public ArrayList<NameScorePair> getScoreList() | |
{ | |
return new ArrayList<NameScorePair>(scoreList.descendingSet()); | |
} | |
public boolean wasResolved() { | |
return wasResolved; | |
} | |
public void wasResolved(boolean wasResolved) { | |
this.wasResolved = wasResolved; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package basho; | |
import com.basho.riak.client.cap.ConflictResolver; | |
import java.util.Collection; | |
import java.util.Iterator; | |
public class GameLeaderboardResolver implements ConflictResolver<GameLeaderboard> | |
{ | |
/* | |
* Riak hands us a list of GameLeaderboard objects. Our job is to reconcile | |
* those objects and return a single, resolved GameLeaderboard | |
* | |
* In this example, the logic is pretty straightforard. in our GameLeaderboard | |
* class we created a addScores(Collection<NameScorePair>) method that will do the | |
* heavy lifting for us. By adding all the lists into one GameLeaderboard | |
* via that method, we end up with the top 5 scores from all the siblings | |
* | |
* Worth noting is that your ConflictResolver is *always* called, even if | |
* there are no siblings, or even if there is no object in Riak | |
*/ | |
public GameLeaderboard resolve(Collection<GameLeaderboard> siblings) | |
{ | |
if (siblings.size() > 1) | |
{ | |
// We have siblings, need to resolve them | |
Iterator<GameLeaderboard> i = siblings.iterator(); | |
GameLeaderboard resolvedLeaderboard = new GameLeaderboard(i.next()); | |
while (i.hasNext()) | |
{ | |
resolvedLeaderboard.addScores(i.next().getScoreList()); | |
} | |
resolvedLeaderboard.wasResolved(true); | |
return resolvedLeaderboard; | |
} | |
else if (siblings.size() == 1) | |
{ | |
// Only one object - just return it | |
return siblings.iterator().next(); | |
} | |
else | |
{ | |
// No object returned - return null | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment