[LIB-10] SerialVersionUID for Serializable classes
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonRounds.java
1 /*
2  * Copyright (c) 2017-2020. 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.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import org.hedgecode.snooker.api.Round;
24 import org.hedgecode.snooker.api.Rounds;
25 import org.hedgecode.snooker.compare.RoundComparators;
26
27 /**
28  * Rounds Entity (set of {@link Round}) to JSON deserialize.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class JsonRounds extends JsonCollectionEntity<Round> implements Rounds, Serializable {
33
34     private static final long serialVersionUID = -292582814222405882L;
35
36     private static final JsonRounds EMPTY = new JsonRounds(new Round[0]);
37
38     JsonRounds(Round[] entities) {
39         super(entities);
40     }
41
42     JsonRounds(List<Round> entities) {
43         super(entities);
44     }
45
46     @Override
47     public Round byRound(int roundId) {
48         for (Round round : this)
49             if (round.round() == roundId)
50                 return round;
51         return null;
52     }
53
54     @Override
55     public Rounds byRoundName(String roundName) {
56         if (roundName == null || roundName.isEmpty())
57             return EMPTY;
58         List<Round> rounds = new ArrayList<>();
59         for (Round round : this)
60             if (roundName.equals(round.roundName()))
61                 rounds.add(round);
62         return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
63     }
64
65     @Override
66     public Rounds byEvent(int eventId) {
67         List<Round> rounds = new ArrayList<>();
68         for (Round round : this)
69             if (round.eventId() == eventId)
70                 rounds.add(round);
71         return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
72     }
73
74     @Override
75     public Rounds byMainEvent(int mainEventId) {
76         List<Round> rounds = new ArrayList<>();
77         for (Round round : this)
78             if (round.mainEventId() == mainEventId)
79                 rounds.add(round);
80         return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
81     }
82
83     @Override
84     public void sortByRound() {
85         sort(
86                 RoundComparators.ROUND.comparator()
87         );
88     }
89
90     @Override
91     public void sortByEvent() {
92         sort(
93                 RoundComparators.EVENT.comparator()
94         );
95     }
96
97 }