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

net.alloyggp.tournament.impl.StandardRanking Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.concurrent.Immutable;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Lists;

import net.alloyggp.tournament.api.Player;
import net.alloyggp.tournament.api.PlayerScore;
import net.alloyggp.tournament.api.Ranking;
import net.alloyggp.tournament.api.Score;
import net.alloyggp.tournament.api.Seeding;

@Immutable
public class StandardRanking implements Ranking {
    //We may have multiple groups, which should be treated separately...
    //Let's ignore this case for now; we may not have to support group play
    private final ImmutableSortedSet scores;

    private StandardRanking(ImmutableSortedSet scores) {
        this.scores = scores;
    }

    public static StandardRanking create(Collection scores) {
        return new StandardRanking(ImmutableSortedSet.copyOf(scores));
    }

    /* (non-Javadoc)
     * @see net.alloyggp.swiss.api.Ranking#getScores()
     */
    @Override
    public ImmutableSortedSet getScores() {
        return scores;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((scores == null) ? 0 : scores.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        StandardRanking other = (StandardRanking) obj;
        if (scores == null) {
            if (other.scores != null) {
                return false;
            }
        } else if (!scores.equals(other.scores)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        int position = 1;
        for (PlayerScore score : scores) {
            sb.append(position).append(") ").append(score.getPlayer().getId())
              .append(" (").append(score.getScore()).append(")").append("\n");
            position++;
        }
        return sb.toString();
    }

    /* (non-Javadoc)
     * @see net.alloyggp.swiss.api.Ranking#getPlayersBestFirst()
     */
    @Override
    public ImmutableList getPlayersBestFirst() {
        return ImmutableList.copyOf(scores.stream()
                .map(PlayerScore::getPlayer)
                .collect(Collectors.toList()));
    }

    public static StandardRanking createForSeeding(Seeding initialSeeding) {
        List scores = Lists.newArrayList();
        ImmutableList players = initialSeeding.getPlayersBestFirst();
        for (int i = 0; i < players.size(); i++) {
            Player player = players.get(i);
            scores.add(PlayerScore.create(player, EmptyScore.create(), i));
        }
        return StandardRanking.create(scores);
    }

    public static class EmptyScore implements Score {
        private EmptyScore() {
            // Use create()
        }

        @Override
        public int compareTo(Score other) {
            if (!(other instanceof EmptyScore)) {
                throw new IllegalArgumentException("Incomparable scores being compared");
            }
            return 0;
        }

        public static Score create() {
            return new EmptyScore();
        }

        @Override
        public int hashCode() {
            return 1;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "initial seeding for stage";
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy