net.alloyggp.tournament.impl.StandardNextMatchesResult Maven / Gradle / Ivy
Show all versions of ggp-tournament Show documentation
package net.alloyggp.tournament.impl;
import java.time.ZonedDateTime;
import java.util.Optional;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableSet;
import net.alloyggp.tournament.api.MatchSetup;
import net.alloyggp.tournament.api.NextMatchesResult;
/**
* Contains a set of matches that should be scheduled for the
* tournament if they are not already running. If the set of
* matches is empty, the tournament is over.
*
* Before scheduling the matches, the client should check if
* there is a restriction on the start time for matches. (This can
* be used by tournament organizers, for example, to spread
* tournaments across multiple non-consecutive blocks, such as on
* separate days.) This is most easily done with the
* {@link #getSecondsToWaitUntilAllowedStartTime()} method; if this returns
* a non-zero value, the client should wait instead of scheduling
* additional matches.
*/
//TODO: Make the public-facing part an interface
public class StandardNextMatchesResult implements NextMatchesResult {
private final ImmutableSet matchesToRun;
//Note: These may be moved into individual matches in the future.
private final Optional earliestAllowedStartTime;
private StandardNextMatchesResult(ImmutableSet matchesToRun, Optional earliestAllowedStartTime) {
this.matchesToRun = matchesToRun;
this.earliestAllowedStartTime = earliestAllowedStartTime;
}
public static NextMatchesResult createEmpty() {
return new StandardNextMatchesResult(ImmutableSet.of(),
Optional.empty());
}
public static NextMatchesResult create(Iterable matchesToRun,
@Nullable ZonedDateTime earliestAllowedStartTime) {
return new StandardNextMatchesResult(
ImmutableSet.copyOf(matchesToRun),
Optional.ofNullable(earliestAllowedStartTime));
}
@Override
public ImmutableSet getMatchesToRun() {
return matchesToRun;
}
@Override
public Optional getEarliestAllowedStartTime() {
return earliestAllowedStartTime;
}
/**
* If a restriction on the start time for the matches is defined and
* has not yet passed, returns the number of seconds left until
* that start time. Otherwise, returns zero.
*/
@Override
public long getSecondsToWaitUntilAllowedStartTime() {
return TimeUtils.getSecondsToWaitUntilStartTime(earliestAllowedStartTime);
}
}