2 * Copyright (c) 2017-2020. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker.json;
19 import java.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.hedgecode.snooker.api.Match;
24 import org.hedgecode.snooker.api.Matches;
25 import org.hedgecode.snooker.compare.MatchComparators;
28 * Matches Entity (set of {@link Match}) to JSON deserialize.
30 * @author Dmitry Samoshin aka gotty
32 public class JsonMatches extends JsonCollectionEntity<Match> implements Matches, Serializable {
34 private static final long serialVersionUID = 7399435317464241002L;
36 private static final JsonMatches EMPTY = new JsonMatches(new Match[0]);
38 JsonMatches(Match[] entities) {
42 JsonMatches(List<Match> entities) {
47 public Matches byEvent(int eventId) {
48 List<Match> matches = new ArrayList<>();
49 for (Match match : this)
50 if (match.eventId() == eventId)
52 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
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)
61 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
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)
70 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
74 public Matches ended() {
75 List<Match> matches = new ArrayList<>();
76 for (Match match : this)
77 if (match.endDate() != null)
79 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
83 public Matches current() {
84 List<Match> matches = new ArrayList<>();
85 for (Match match : this)
86 if (match.startDate() != null && match.endDate() == null)
88 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
92 public Matches upcoming() {
93 List<Match> matches = new ArrayList<>();
94 for (Match match : this)
95 if (match.startDate() == null)
97 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
101 public void sortByNumber() {
103 MatchComparators.ROUND_NUMBER.comparator()
108 public void sortByEvent() {
110 MatchComparators.EVENT.comparator()