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