package nl.connectedit.swiss.domain; import lombok.Getter; import lombok.Setter; import lombok.ToString; import nl.connectedit.swiss.domain.entity.Team; @Getter @Setter @ToString public class StandingsEntry { public StandingsEntry(Team team) { this.team = team; } private Long position; private Team team; private Long played = 0L; private Long won = 0L; private Long lost = 0L; private Long points = 0L; private Long gamesWon = 0L; private Long gamesLost = 0L; private Long pointsWon = 0L; private Long pointsLost = 0L; public static int compare(StandingsEntry s1, StandingsEntry s2) { var pointsPerMatchS1 = safeDivide(s1.points, s1.played); var pointsPerMatchS2 = safeDivide(s2.points, s2.played); if (pointsPerMatchS1 > pointsPerMatchS2) { return -1; } else if (pointsPerMatchS1 < pointsPerMatchS2) { return 1; } else { if (s1.played < s2.played) { return -1; } else if (s1.played > s2.played) { return 1; } else { var gamesPerMatchS1 = safeDivide(s1.gamesWon - s1.gamesLost, s1.played); var gamesPerMatchS2 = safeDivide(s2.gamesWon - s2.gamesLost, s2.played); if (gamesPerMatchS1 > gamesPerMatchS2) { return -1; } else if (gamesPerMatchS1 < gamesPerMatchS2) { return 1; } else { var ptsPerMatchS1 = safeDivide(s1.pointsWon - s1.pointsLost, s1.played); var ptsPerMatchS2 = safeDivide(s2.pointsWon - s2.pointsLost, s2.played); return Double.compare(ptsPerMatchS2, ptsPerMatchS1); } } } } private static double safeDivide(Long a, Long b) { return (a == 0 && b == 0) ? 0 : (double) a / b; } }