From 6149a22fa37f93c27259dc37fadafdb64840b554 Mon Sep 17 00:00:00 2001 From: Finn Huelsbusch Date: Mon, 31 Oct 2022 10:23:28 +0100 Subject: [PATCH] final changes --- .../domain/leaderboard/Leaderboard.java | 26 +++++----- .../impl/AbstractLeaderboardImpl.java | 49 +++++++++---------- .../impl/CompetitorAndRankComparable.java | 1 + ...aLeaderboardWithCompetitorElimination.java | 1 - .../sailing/domain/tracking/TrackedRace.java | 6 +-- .../domain/tracking/impl/TrackedRaceImpl.java | 14 ++++-- 6 files changed, 48 insertions(+), 49 deletions(-) diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/Leaderboard.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/Leaderboard.java index c552eb690a5..944f25574f3 100644 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/Leaderboard.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/Leaderboard.java @@ -96,7 +96,7 @@ public interface Leaderboard extends LeaderboardBase, HasRaceColumns { * @author I518097 * */ - public class RankComparableRank implements RankComparable{ + public class RankComparableRank implements RankComparable { private final Integer rank; public RankComparableRank(Integer rank) { @@ -106,25 +106,21 @@ public interface Leaderboard extends LeaderboardBase, HasRaceColumns { } this.rank = rank; } - @Override public int compareTo(RankComparable o) { - if (o instanceof RankComparableRank) { - RankComparableRank otherRankComparableRank = (RankComparableRank) o; - if (rank == 0) { - return 1; // the other RankComparable is better because this RankComparable has not started + RankComparableRank otherRankComparableRank = (RankComparableRank) o; + final int result; + if (rank == 0) { + result = 1; // the other RankComparable is better because this RankComparable has not started + } else { + if (otherRankComparableRank.rank == 0) { + result =-1; // this RankComparable is better (lower rank) than the other one because the other one + // has not started } else { - if (otherRankComparableRank.rank == 0) { - return -1; // this RankComparable is better (lower rank) than the other one because the other one - // has not started - } else { - return rank - otherRankComparableRank.rank; // normal integer compare - } + result = rank - otherRankComparableRank.rank; // normal integer compare } - }else { - // error handling needed - return 0; } + return result; } } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/AbstractLeaderboardImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/AbstractLeaderboardImpl.java index 64fcc6b2096..874947b1359 100644 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/AbstractLeaderboardImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/AbstractLeaderboardImpl.java @@ -1,9 +1,7 @@ package com.sap.sailing.domain.leaderboard.impl; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -154,20 +152,18 @@ public abstract class AbstractLeaderboardImpl extends AbstractSimpleLeaderboardI // if on the other hand the flag is set, we first need to order the competitors after their rankComparables. // After that we can assign scores. if (race.hasCrossFleetMergedRanking()) { - // group fleets by their ordering in order to check if the hasCrossFleetMergedRanking has an effect or - // is irrelevant. - HashMap> fleetsByRank = new HashMap<>(); + // get Fleets that are of the same order as the fleet of the competitor + List fleetsOfSameOrderAsCompetitorsFleet = new ArrayList<>(); + int rankOfCompetitorsFleet = race.getFleetOfCompetitor(competitor).getOrdering(); for (Fleet fleet : race.getFleets()) { - if (fleetsByRank.get(fleet.getOrdering()) == null) { - fleetsByRank.put(fleet.getOrdering(), Arrays.asList(fleet)); - } else { - fleetsByRank.get(fleet.getOrdering()).add(fleet); + if (fleet.getOrdering() == rankOfCompetitorsFleet) { + fleetsOfSameOrderAsCompetitorsFleet.add(fleet); } } - if (fleetsByRank.get(race.getFleetOfCompetitor(competitor).getOrdering()).size() > 1) { + if (fleetsOfSameOrderAsCompetitorsFleet.size() > 1) { // more than one fleet of the same rank -> need to merge these fleets List competitorsFromBestToWorstAndRankComparable = new ArrayList<>(); - for (Fleet fleet : fleetsByRank.get(race.getFleetOfCompetitor(competitor).getOrdering())) { + for (Fleet fleet : fleetsOfSameOrderAsCompetitorsFleet) { List currentCompetitorsAndRankcomparables = race .getTrackedRace(fleet).getCompetitorsFromBestToWorstAndRankComparable(timePoint); competitorsFromBestToWorstAndRankComparable.addAll(currentCompetitorsAndRankcomparables); @@ -177,20 +173,19 @@ public abstract class AbstractLeaderboardImpl extends AbstractSimpleLeaderboardI Collections.sort(competitorsFromBestToWorstAndRankComparable); Iterator competitorsIterator = competitorsFromBestToWorstAndRankComparable.stream() .map(v -> v.getCompetitor()).iterator(); - trackedRank = improveByDisqualificationsOfBetterRankedCompetitors(competitor, race, timePoint, - competitorsIterator); + trackedRank = getRankImprovedByDisqualificationsOfBetterRankedCompetitors(competitor, race, + timePoint, competitorsIterator); } else { // just one fleet for the given rank -> no merge needed just use the normal behavior - trackedRank = improveByDisqualificationsOfBetterRankedCompetitors(competitor, race, timePoint, - trackedRace.getCompetitorsFromBestToWorst(timePoint, cache).iterator()); + trackedRank = getRankImprovedByDisqualificationsOfBetterRankedCompetitors(competitor, race, + timePoint, trackedRace.getCompetitorsFromBestToWorst(timePoint, cache).iterator()); } } else { - trackedRank = improveByDisqualificationsOfBetterRankedCompetitors(competitor, race, timePoint, + trackedRank = getRankImprovedByDisqualificationsOfBetterRankedCompetitors(competitor, race, timePoint, trackedRace.getCompetitorsFromBestToWorst(timePoint, cache).iterator()); } } return trackedRank; - } /** @@ -201,25 +196,25 @@ public abstract class AbstractLeaderboardImpl extends AbstractSimpleLeaderboardI * * @param competitor * the competitor whose rank is to be improved - * @param race + * @param race * raceColumn that is used to check if a competitor is disqualified * @param timePoint * time point at which to consider disqualifications (not used yet because currently we don't remember * when a competitor was disqualified) * @param competitorsFromBestToWorst - * An iterator that contains all competitors through which the rank of the given competitor can be improved. + * An iterator that contains all competitors through which the rank of the given competitor can be improved. * * @return the unmodified Pair(Rank, {@link RankComparable} if no disqualifications for better-ranked competitors exist for * race, or otherwise a Pair(Rank, {@link RankComparable} where the Rank is improved (lowered) by the number of disqualifications of * competitors whose tracked rank is better (lower) than rank while the {@link RankComparable} is consistent with the new Rank. */ - private int improveByDisqualificationsOfBetterRankedCompetitors( Competitor competitor, RaceColumn race, + private int getRankImprovedByDisqualificationsOfBetterRankedCompetitors(Competitor competitor, RaceColumn race, TimePoint timePoint, Iterator competitorsFromBestToWorst) { - int rank = 1; - int numberOfDisqualificationsOfBetterRankedCompetitors = 0; - while(competitorsFromBestToWorst.hasNext()) { + int rank = 1; + int numberOfDisqualificationsOfBetterRankedCompetitors = 0; + while (competitorsFromBestToWorst.hasNext()) { Competitor currentCompetitor = competitorsFromBestToWorst.next(); - if(competitor.equals(currentCompetitor)) { + if (competitor.equals(currentCompetitor)) { break; } MaxPointsReason maxPointsReasonForBetterCompetitor = getScoreCorrection() @@ -227,11 +222,11 @@ public abstract class AbstractLeaderboardImpl extends AbstractSimpleLeaderboardI if (isSuppressed(currentCompetitor) || (maxPointsReasonForBetterCompetitor != null && maxPointsReasonForBetterCompetitor != MaxPointsReason.NONE && maxPointsReasonForBetterCompetitor.isAdvanceCompetitorsTrackedWorse())) { - numberOfDisqualificationsOfBetterRankedCompetitors ++; + numberOfDisqualificationsOfBetterRankedCompetitors++; } - rank++; + rank++; } - return rank - numberOfDisqualificationsOfBetterRankedCompetitors; + return rank - numberOfDisqualificationsOfBetterRankedCompetitors; } // Note: no need to redefine isMedalRaceChanged because that doesn't affect the competitorsCache diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/CompetitorAndRankComparable.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/CompetitorAndRankComparable.java index 48f9cb224a8..729ba356da1 100644 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/CompetitorAndRankComparable.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/leaderboard/impl/CompetitorAndRankComparable.java @@ -22,6 +22,7 @@ public class CompetitorAndRankComparable implements Comparable getCompetitorsFromBestToWorstAndRankAndRankComparable(TimePoint timePoint); /** - * Same as {@link #getCompetitorsFromBestToWorstAndRankComparable(TimePoint)}, using a cache for wind, leg type and leg + * Same as {@link #getCompetitorsFromBestToWorstAndRankAndRankComparable(TimePoint)}, using a cache for wind, leg type and leg * bearing values. * * @return If no ranking can be computed for the given {@link TimePoint} an empty {@link LinkedHashMap} is returned not null. @@ -819,7 +819,7 @@ public interface TrackedRace LinkedHashMap getCompetitorsFromBestToWorstAndRankAndRankComparable(TimePoint timePoint, WindLegTypeAndLegBearingAndORCPerformanceCurveCache cache); /** - * Returns the competitors of this tracked race, according to their ranking (iteration order) and their RankComparables. + * Returns the competitors and their RankComparables of this tracked race, according to their ranking within in this Race (iteration order). * * @return If no ranking can be computed for the given {@link TimePoint} an empty {@link List} is returned not null. */ diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/tracking/impl/TrackedRaceImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/tracking/impl/TrackedRaceImpl.java index fd854c391b8..dce1fd7576e 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/tracking/impl/TrackedRaceImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/tracking/impl/TrackedRaceImpl.java @@ -1601,7 +1601,12 @@ public abstract class TrackedRaceImpl extends TrackedRaceWithWindEssentials impl // no mark passing at or before timePoint; competitor has not started / participated yet result = 0; } else { - result = getCompetitorsFromBestToWorstAndRankAndRankComparable(timePoint, cache).get(competitor).getRank(); + RankAndRankComparable rankAndRankComparable = getCompetitorsFromBestToWorstAndRankAndRankComparable(timePoint, cache).get(competitor); + if(competitor == null) { + result = 0; + }else { + result = rankAndRankComparable.getRank(); + } } } return result; @@ -1630,6 +1635,8 @@ public abstract class TrackedRaceImpl extends TrackedRaceWithWindEssentials impl return getCompetitorsFromBestToWorst(timePoint, new LeaderboardDTOCalculationReuseCache(timePoint)); } + // defaults im interface anschauen + @Override public Iterable getCompetitorsFromBestToWorst(TimePoint timePoint, WindLegTypeAndLegBearingAndORCPerformanceCurveCache cache) { @@ -1689,6 +1696,7 @@ public abstract class TrackedRaceImpl extends TrackedRaceWithWindEssentials impl getRace().getCourse().lockForRead(); try { // here the rankingmetrics need to return the RankComparables so that they could be ranked accordingly. + // encapsulate sorting providing etc. in a method of the Ranking metric. To Update this cache Comparator comparator = getRankingMetric().getRaceRankingComparator(timePoint, cache); List tempList = new ArrayList(); for (Competitor c : getRace().getCompetitors()) { @@ -1696,9 +1704,9 @@ public abstract class TrackedRaceImpl extends TrackedRaceWithWindEssentials impl } Collections.sort(tempList, comparator); final Iterator it = tempList.iterator(); - rankedCompetitors = new LinkedHashMap(); + rankedCompetitors = new LinkedHashMap<>(); for (int i = 1; it.hasNext(); i++) { - rankedCompetitors.put(it.next(), new RankAndRankComparable(i, new RankComparableRank(i))); + rankedCompetitors.put(it.next(), new RankAndRankComparable(i, /* TODO*/ new RankComparableRank(i))); } } finally { getRace().getCourse().unlockAfterRead();