za.co.knowles.pokewhat.HandComparator Maven / Gradle / Ivy
package za.co.knowles.pokewhat;
import za.co.knowles.pokewhat.domain.lookup.EHand;
import za.co.knowles.pokewhat.domain.Hand;
import za.co.knowles.pokewhat.domain.HandResult;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class HandComparator {
public static List compare(Collection hands) {
ArrayList handResults = new ArrayList<>();
for (Hand hand : hands) {
handResults.add(new HandResult(hand, HandResolver.getHand(hand)));
}
handResults.sort(Comparator.comparingInt(o -> o.getHandResult().getRank()));
checkForTies(handResults);
return handResults;
}
private static void checkForTies(ArrayList handResults) {
int best = handResults.get(0).getHandResult().getRank();
List ties = handResults.stream().filter(r -> r.getHandResult().getRank() == best).collect(Collectors.toList());
if (ties.size() > 1) {
handResults.removeAll(ties);
handResults.addAll(0, breakTies(ties));
}
}
private static List breakTies(List ties) {
EHand theHand = ties.get(0).getHandResult();
List handResults = theHand.sortTies(ties);
handResults.get(0).setTieBreakWinner(true);
return handResults;
}
}