1 | package geniusweb.protocol.tournament.allpermutations;
|
---|
2 |
|
---|
3 | import java.math.BigInteger;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
8 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
9 |
|
---|
10 | import geniusweb.protocol.session.SessionResult;
|
---|
11 | import geniusweb.protocol.session.SessionSettings;
|
---|
12 | import geniusweb.protocol.tournament.TournamentState;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * A non-parallel simple implementation of all-permutations
|
---|
16 | */
|
---|
17 | public class AllPermutationsState implements TournamentState {
|
---|
18 |
|
---|
19 | protected final AllPermutationsSettings toursettings; // original settings
|
---|
20 | // limited to MAX_INT elements. But we need to store results anyway
|
---|
21 | protected final List<SessionResult> results;
|
---|
22 |
|
---|
23 | @JsonCreator
|
---|
24 | public AllPermutationsState(
|
---|
25 | @JsonProperty("toursettings") AllPermutationsSettings toursettings,
|
---|
26 | @JsonProperty("results") List<SessionResult> results) {
|
---|
27 | this.toursettings = toursettings;
|
---|
28 | this.results = results;
|
---|
29 | }
|
---|
30 |
|
---|
31 | public List<SessionResult> getResults() {
|
---|
32 | return results;
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public boolean isFinal(long currentTimeMs) {
|
---|
37 | return toursettings.permutations().size().intValue() == results.size();
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * @return the settings for the next session. Null if no such session.
|
---|
42 | */
|
---|
43 | public SessionSettings getNextSettings() {
|
---|
44 | if (isFinal(0l))
|
---|
45 | return null;
|
---|
46 | return toursettings.permutations()
|
---|
47 | .get(BigInteger.valueOf(results.size()));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | *
|
---|
52 | * @param result the next {@link SessionResult}
|
---|
53 | * @return new state
|
---|
54 | * @throws IllegalArgumentException if the result participants are not
|
---|
55 | * matching the expected participants
|
---|
56 | */
|
---|
57 | public AllPermutationsState with(List<SessionResult> res) {
|
---|
58 | // FIXME do we really need this? Why is this not working?
|
---|
59 | // if (!new HashSet<PartyWithProfile>(getNextSettings().getAllParties())
|
---|
60 | // .equals(result.getParticipants().values())) {
|
---|
61 | // throw new IllegalArgumentException("Inconsistent session result");
|
---|
62 | // }
|
---|
63 | ArrayList<SessionResult> newresults = new ArrayList<>(results);
|
---|
64 | newresults.addAll(res);
|
---|
65 | return new AllPermutationsState(toursettings, newresults);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | *
|
---|
70 | * @return number of sessions in this tournament.
|
---|
71 | */
|
---|
72 | public BigInteger getSize() {
|
---|
73 | return toursettings.permutations().size();
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public AllPermutationsSettings getSettings() {
|
---|
78 | return toursettings;
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|