[LIB-2] Add snooker-score-api source files
[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     protected JsonMatches(Match[] entities) {
34         super(entities);
35     }
36
37     protected JsonMatches(List<Match> entities) {
38         super(entities);
39     }
40
41     @Override
42     public Matches byEvent(int eventId) {
43         List<Match> matches = new ArrayList<>();
44         for (Match match : this)
45             if (match.eventId() == eventId)
46                 matches.add(match);
47         return matches.isEmpty() ? null : new JsonMatches(matches);
48     }
49
50     @Override
51     public Matches byRound(int eventId, int round) {
52         List<Match> matches = new ArrayList<>();
53         for (Match match : this)
54             if (match.eventId() == eventId && match.round() == round)
55                 matches.add(match);
56         return matches.isEmpty() ? null : new JsonMatches(matches);
57     }
58
59     @Override
60     public Matches byPlayer(int playerId) {
61         List<Match> matches = new ArrayList<>();
62         for (Match match : this)
63             if (match.player1Id() == playerId || match.player2Id() == playerId)
64                 matches.add(match);
65         return matches.isEmpty() ? null : new JsonMatches(matches);
66     }
67
68     @Override
69     public Matches ended() {
70         List<Match> matches = new ArrayList<>();
71         for (Match match : this)
72             if (match.endDate() != null)
73                 matches.add(match);
74         return matches.isEmpty() ? null : new JsonMatches(matches);
75     }
76
77     @Override
78     public Matches current() {
79         List<Match> matches = new ArrayList<>();
80         for (Match match : this)
81             if (match.startDate() != null && match.endDate() == null)
82                 matches.add(match);
83         return matches.isEmpty() ? null : new JsonMatches(matches);
84     }
85
86     @Override
87     public Matches upcoming() {
88         List<Match> matches = new ArrayList<>();
89         for (Match match : this)
90             if (match.startDate() == null)
91                 matches.add(match);
92         return matches.isEmpty() ? null : new JsonMatches(matches);
93     }
94
95     @Override
96     public void sortByNumber() {
97         sort(
98                 MatchComparators.ROUND_NUMBER.comparator()
99         );
100     }
101
102     @Override
103     public void sortByEvent() {
104         sort(
105                 MatchComparators.EVENT.comparator()
106         );
107     }
108
109 }