Skip to content

Instantly share code, notes, and snippets.

@xfyre
Last active August 29, 2015 14:13
Show Gist options
  • Save xfyre/9d72eae57a460dba440b to your computer and use it in GitHub Desktop.
Save xfyre/9d72eae57a460dba440b to your computer and use it in GitHub Desktop.
retrieveRankedList example
public List<ContestEntrance> retrieveRankedList ( Contest contest, ContestSignupRole role ) {
if ( !contest.isRandomPartnered () )
throw new IllegalArgumentException ( "only applicable for random-partnered contests" );
final List<ContestEntrance> list = new ArrayList<ContestEntrance> ();
final List<ContestRound> reverseRoundsList = new ArrayList<ContestRound> ( contest.getRounds () );
Collections.reverse ( reverseRoundsList );
for ( final ContestRound round : reverseRoundsList ) {
if ( !( round.isScored () || round.isFinished () ) )
throw new IllegalArgumentException ( "all rounds must be scored" );
if ( round.isFinal () ) {
if ( role == round.getDrawRole () ) {
list.addAll ( round.getRankedEntrances () );
} else {
CollectionUtils.collect ( round.getRankedEntrances (), new Transformer () {
@Override
public Object transform ( Object input ) {
ContestEntrance entrance = (ContestEntrance) input;
return entrance.getDraw ().getPartnerEntrance ();
}
}, list );
}
} else {
List<ContestEntrance> roundEntrances = round.getEntrancesSortedByCallbackScore ( role );
CollectionUtils.select ( roundEntrances, new Predicate () {
@Override
public boolean evaluate ( Object object ) {
ContestEntrance entrance = (ContestEntrance) object;
return entrance.getRank () != null;
}
}, list );
}
}
return list;
}
def retrieveRankedList(contest: Contest, role: ContestSignupRole) = {
if (!contest.isRandomPartnered)
throw new IllegalArgumentException("only applicable for random-partnered contests");
if (contest.getRounds.asScala.count(!_.isScored) > 0)
throw new IllegalArgumentException("all rounds must be scored");
contest.getRounds.asScala.toList.reverse.flatMap(_.getContestEntrances.asScala)
.filter(_.getRank != null)
.filter(_.getContestSignup.getRole == role)
.sortBy(_.getRank)
.asJava
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment