Commit cec5c367 authored by João Lino's avatar João Lino

remove time dependency

parent 58811b0e
package org.example.scoreboard.api; package org.example.scoreboard.api;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
public interface IMatch { public interface IMatch {
...@@ -13,5 +12,4 @@ public interface IMatch { ...@@ -13,5 +12,4 @@ public interface IMatch {
ITeam getAwayTeam(); ITeam getAwayTeam();
IScore getAwayTeamScore(); IScore getAwayTeamScore();
void setAwayTeamScore(IScore awayTeamScore); void setAwayTeamScore(IScore awayTeamScore);
Date getStartDate();
} }
...@@ -4,7 +4,6 @@ import org.example.scoreboard.api.IMatch; ...@@ -4,7 +4,6 @@ import org.example.scoreboard.api.IMatch;
import org.example.scoreboard.api.IScore; import org.example.scoreboard.api.IScore;
import org.example.scoreboard.api.ITeam; import org.example.scoreboard.api.ITeam;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
import java.util.function.Supplier; import java.util.function.Supplier;
...@@ -14,7 +13,6 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> { ...@@ -14,7 +13,6 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> {
private final ITeam awayTeam; private final ITeam awayTeam;
private IScore homeTeamScore; private IScore homeTeamScore;
private IScore awayTeamScore; private IScore awayTeamScore;
private final Date startDate;
protected AbstractMatch(Supplier<IScore> scoreSupplier, ITeam homeTeam, ITeam awayTeam) { protected AbstractMatch(Supplier<IScore> scoreSupplier, ITeam homeTeam, ITeam awayTeam) {
this.id = UUID.randomUUID(); this.id = UUID.randomUUID();
...@@ -22,7 +20,6 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> { ...@@ -22,7 +20,6 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> {
this.awayTeam = awayTeam; this.awayTeam = awayTeam;
this.homeTeamScore = scoreSupplier.get(); this.homeTeamScore = scoreSupplier.get();
this.awayTeamScore = scoreSupplier.get(); this.awayTeamScore = scoreSupplier.get();
this.startDate = new Date();
} }
public ITeam getHomeTeam() { public ITeam getHomeTeam() {
...@@ -53,10 +50,6 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> { ...@@ -53,10 +50,6 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> {
this.awayTeamScore = awayTeamScore; this.awayTeamScore = awayTeamScore;
} }
public Date getStartDate() {
return startDate;
}
@Override @Override
public String toString() { public String toString() {
return getHomeTeam() + " " + getHomeTeamScore() + " - " + getAwayTeam() + " " + getAwayTeamScore(); return getHomeTeam() + " " + getHomeTeamScore() + " - " + getAwayTeam() + " " + getAwayTeamScore();
...@@ -70,16 +63,8 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> { ...@@ -70,16 +63,8 @@ public abstract class AbstractMatch implements IMatch, Comparable<IMatch> {
return -1; return -1;
} else if (totalScoreMatch1 < totalScoreMatch2) { } else if (totalScoreMatch1 < totalScoreMatch2) {
return 1; return 1;
} else {
Date startMatch1 = getStartDate();
Date startMatch2 = otherMatch.getStartDate();
if (startMatch1.after(startMatch2)) {
return -1;
} else if(startMatch1.before(startMatch2)) {
return 1;
}
} }
return 0; return -1; // default return -1 to reverse the linked hash map insertion-order iterator on score collision
} }
} }
...@@ -8,6 +8,7 @@ import org.example.scoreboard.api.ITeam; ...@@ -8,6 +8,7 @@ import org.example.scoreboard.api.ITeam;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiFunction; import java.util.function.BiFunction;
...@@ -19,7 +20,7 @@ public class AbstractScoreboard implements IScoreboard { ...@@ -19,7 +20,7 @@ public class AbstractScoreboard implements IScoreboard {
BiFunction<ITeam, ITeam, IMatch> matchSupplier; BiFunction<ITeam, ITeam, IMatch> matchSupplier;
HashMap<UUID, ITeam> teamsOnTheBoard = new HashMap<>(); HashMap<UUID, ITeam> teamsOnTheBoard = new HashMap<>();
HashMap<UUID, IMatch> ongoingMatches = new HashMap<>(); LinkedHashMap<UUID, IMatch> ongoingMatches = new LinkedHashMap<>();
private AbstractScoreboard(){} private AbstractScoreboard(){}
public AbstractScoreboard(BiFunction<ITeam, ITeam, IMatch> matchSupplier) { public AbstractScoreboard(BiFunction<ITeam, ITeam, IMatch> matchSupplier) {
......
...@@ -10,7 +10,6 @@ import java.util.*; ...@@ -10,7 +10,6 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.lang.Thread.sleep;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
...@@ -123,24 +122,42 @@ public class TestScoreboard { ...@@ -123,24 +122,42 @@ public class TestScoreboard {
} }
} }
// should return ordered matches_whenSummaryIsRequested // should present matches ordered from newest to oldest when summary is requested
@Test @Test
public void shouldReturnOrderedMatches_whenSummaryIsRequested() throws InterruptedException { public void shouldPresentFromNewestToOldest_whenSummaryIsRequestedAndAllGamesWithTheSameScore() {
IMatch match1 = scoreboard.startMatch(Mexico, Canada);
match1 = scoreboard.updateScore(match1, new Score(5), new Score(5));
IMatch match2 = scoreboard.startMatch(Spain, Brazil);
match2 = scoreboard.updateScore(match2, new Score(5), new Score(5));
IMatch match3 = scoreboard.startMatch(Germany, France);
match3 = scoreboard.updateScore(match3, new Score(5), new Score(5));
IMatch match4 = scoreboard.startMatch(Uruguay, Italy);
match4 = scoreboard.updateScore(match4, new Score(5), new Score(5));
IMatch match5 = scoreboard.startMatch(Argentina, Australia);
match5 = scoreboard.updateScore(match5, new Score(5), new Score(5));
ArrayList<IMatch> expectedResult = Stream.of(match5, match4, match3, match2, match1).collect(Collectors.toCollection(ArrayList::new));
ArrayList<IMatch> gameSummary = scoreboard.getGameSummary();
if (!expectedResult.equals(gameSummary)) {
gameSummary.forEach(System.out::println);
fail("failed to return expected ordered summary");
}
}
// should present matches ordered from highest combined score to lowest, newest to oldest on collision, when summary is requested
@Test
public void shouldOrderByScoreFromHighToLowedByNewToOldOnCollision_whenSummaryIsRequested() {
IMatch match1 = scoreboard.startMatch(Mexico, Canada); IMatch match1 = scoreboard.startMatch(Mexico, Canada);
match1 = scoreboard.updateScore(match1, new Score(0), new Score(5)); match1 = scoreboard.updateScore(match1, new Score(0), new Score(5));
sleep(1);
IMatch match2 = scoreboard.startMatch(Spain, Brazil); IMatch match2 = scoreboard.startMatch(Spain, Brazil);
match2 = scoreboard.updateScore(match2, new Score(10), new Score(2)); match2 = scoreboard.updateScore(match2, new Score(10), new Score(2));
sleep(1);
IMatch match3 = scoreboard.startMatch(Germany, France); IMatch match3 = scoreboard.startMatch(Germany, France);
match3 = scoreboard.updateScore(match3, new Score(2), new Score(2)); match3 = scoreboard.updateScore(match3, new Score(2), new Score(2));
sleep(1);
IMatch match4 = scoreboard.startMatch(Uruguay, Italy); IMatch match4 = scoreboard.startMatch(Uruguay, Italy);
match4 = scoreboard.updateScore(match4, new Score(6), new Score(6)); match4 = scoreboard.updateScore(match4, new Score(6), new Score(6));
sleep(1);
IMatch match5 = scoreboard.startMatch(Argentina, Australia); IMatch match5 = scoreboard.startMatch(Argentina, Australia);
match5 = scoreboard.updateScore(match5, new Score(3), new Score(1)); match5 = scoreboard.updateScore(match5, new Score(3), new Score(1));
sleep(1);
ArrayList<IMatch> expectedResult = Stream.of(match4, match2, match1, match5, match3).collect(Collectors.toCollection(ArrayList::new)); ArrayList<IMatch> expectedResult = Stream.of(match4, match2, match1, match5, match3).collect(Collectors.toCollection(ArrayList::new));
ArrayList<IMatch> gameSummary = scoreboard.getGameSummary(); ArrayList<IMatch> gameSummary = scoreboard.getGameSummary();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment