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

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

package net.alloyggp.tournament.internal.spec;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.concurrent.Immutable;

import org.joda.time.DateTime;

import com.google.common.base.Optional;
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.internal.Game;
import net.alloyggp.tournament.internal.TimeUtils;
import net.alloyggp.tournament.internal.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(DateTime.parse(timeString, TimeUtils.RFC1123_DATE_TIME_FORMATTER));
//            java.util.Date date = (Date) roundMap.get("startTime");
//            startTime = date.
        } else {
            startTime = Optional.absent();
        }

        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;
    }
}