[LIB-8] Add Event Rounds and Seedings functional
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonRounds.java
1 /*
2  * Copyright (c) 2017-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.hedgecode.snooker.api.Round;
23 import org.hedgecode.snooker.api.Rounds;
24 import org.hedgecode.snooker.compare.RoundComparators;
25
26 /**
27  * Rounds Entity (set of {@link Round}) to JSON deserialize.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public class JsonRounds extends JsonCollectionEntity<Round> implements Rounds {
32
33     private static final JsonRounds EMPTY = new JsonRounds(new Round[0]);
34
35     protected JsonRounds(Round[] entities) {
36         super(entities);
37     }
38
39     protected JsonRounds(List<Round> entities) {
40         super(entities);
41     }
42
43     @Override
44     public Round byRound(int roundId) {
45         for (Round round : this)
46             if (round.round() == roundId)
47                 return round;
48         return null;
49     }
50
51     @Override
52     public Rounds byRoundName(String roundName) {
53         if (roundName == null || roundName.isEmpty())
54             return EMPTY;
55         List<Round> rounds = new ArrayList<>();
56         for (Round round : this)
57             if (roundName.equals(round.roundName()))
58                 rounds.add(round);
59         return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
60     }
61
62     @Override
63     public Rounds byEvent(int eventId) {
64         List<Round> rounds = new ArrayList<>();
65         for (Round round : this)
66             if (round.eventId() == eventId)
67                 rounds.add(round);
68         return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
69     }
70
71     @Override
72     public Rounds byMainEvent(int mainEventId) {
73         List<Round> rounds = new ArrayList<>();
74         for (Round round : this)
75             if (round.mainEventId() == mainEventId)
76                 rounds.add(round);
77         return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
78     }
79
80     @Override
81     public void sortByRound() {
82         sort(
83                 RoundComparators.ROUND.comparator()
84         );
85     }
86
87     @Override
88     public void sortByEvent() {
89         sort(
90                 RoundComparators.EVENT.comparator()
91         );
92     }
93
94 }