[LIB-10] SerialVersionUID for Serializable classes
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonMatches.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.Match;
24 import org.hedgecode.snooker.api.Matches;
25 import org.hedgecode.snooker.compare.MatchComparators;
26
27 /**
28  * Matches Entity (set of {@link Match}) to JSON deserialize.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public class JsonMatches extends JsonCollectionEntity<Match> implements Matches, Serializable {
33
34     private static final long serialVersionUID = 7399435317464241002L;
35
36     private static final JsonMatches EMPTY = new JsonMatches(new Match[0]);
37
38     JsonMatches(Match[] entities) {
39         super(entities);
40     }
41
42     JsonMatches(List<Match> entities) {
43         super(entities);
44     }
45
46     @Override
47     public Matches byEvent(int eventId) {
48         List<Match> matches = new ArrayList<>();
49         for (Match match : this)
50             if (match.eventId() == eventId)
51                 matches.add(match);
52         return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
53     }
54
55     @Override
56     public Matches byRound(int eventId, int round) {
57         List<Match> matches = new ArrayList<>();
58         for (Match match : this)
59             if (match.eventId() == eventId && match.round() == round)
60                 matches.add(match);
61         return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
62     }
63
64     @Override
65     public Matches byPlayer(int playerId) {
66         List<Match> matches = new ArrayList<>();
67         for (Match match : this)
68             if (match.player1Id() == playerId || match.player2Id() == playerId)
69                 matches.add(match);
70         return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
71     }
72
73     @Override
74     public Matches ended() {
75         List<Match> matches = new ArrayList<>();
76         for (Match match : this)
77             if (match.endDate() != null)
78                 matches.add(match);
79         return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
80     }
81
82     @Override
83     public Matches current() {
84         List<Match> matches = new ArrayList<>();
85         for (Match match : this)
86             if (match.startDate() != null && match.endDate() == null)
87                 matches.add(match);
88         return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
89     }
90
91     @Override
92     public Matches upcoming() {
93         List<Match> matches = new ArrayList<>();
94         for (Match match : this)
95             if (match.startDate() == null)
96                 matches.add(match);
97         return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
98     }
99
100     @Override
101     public void sortByNumber() {
102         sort(
103                 MatchComparators.ROUND_NUMBER.comparator()
104         );
105     }
106
107     @Override
108     public void sortByEvent() {
109         sort(
110                 MatchComparators.EVENT.comparator()
111         );
112     }
113
114 }