All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.alloyggp.tournament.spec.RoundSpec Maven / Gradle / Ivy

There is a newer version: 0.1.0
Show newest version
package net.alloyggp.tournament.spec;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.annotation.concurrent.Immutable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

import net.alloyggp.tournament.api.Game;
import net.alloyggp.tournament.impl.YamlUtils;

@Immutable
public class RoundSpec {
    private final Optional startTime;
    private final ImmutableList matches;

    private RoundSpec(Optional startTime, ImmutableList matches) {
        this.startTime = startTime;
        this.matches = matches;
    }

    private static final ImmutableSet ALLOWED_KEYS = ImmutableSet.of(
            "startTime",
            "matches"
            );

    @SuppressWarnings("unchecked")
    public static RoundSpec parseYaml(Object yamlRound, Map games) {
        Map roundMap = (Map) yamlRound;
        YamlUtils.validateKeys(roundMap, "round", ALLOWED_KEYS);

        final Optional startTime;
        if (roundMap.containsKey("startTime")) {
            String timeString = (String) roundMap.get("startTime");
            startTime = Optional.of(ZonedDateTime.parse(timeString, DateTimeFormatter.RFC_1123_DATE_TIME));
//            java.util.Date date = (Date) roundMap.get("startTime");
//            startTime = date.
        } else {
            startTime = Optional.empty();
        }

        List matches = Lists.newArrayList();
        for (Object yamlMatch : (List) roundMap.get("matches")) {
            matches.add(MatchSpec.parseYaml(yamlMatch, games));
        }
        return new RoundSpec(startTime, ImmutableList.copyOf(matches));
    }

    public Optional getStartTime() {
        return startTime;
    }

    public ImmutableList getMatches() {
        return matches;
    }

    public static Set getAllGames(ImmutableList rounds) {
        Set results = Sets.newHashSet();
        for (RoundSpec round : rounds) {
            for (MatchSpec match : round.getMatches()) {
                results.add(match.getGame());
            }
        }
        return results;
    }
}