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.util.ArrayList;
20 import java.util.List;
22 import org.hedgecode.snooker.api.Match;
23 import org.hedgecode.snooker.api.Matches;
24 import org.hedgecode.snooker.compare.MatchComparators;
27 * Matches Entity (set of {@link Match}) to JSON deserialize.
29 * @author Dmitry Samoshin aka gotty
31 public class JsonMatches extends JsonCollectionEntity<Match> implements Matches {
33 private static final JsonMatches EMPTY = new JsonMatches(new Match[0]);
35 protected JsonMatches(Match[] entities) {
39 protected JsonMatches(List<Match> entities) {
44 public Matches byEvent(int eventId) {
45 List<Match> matches = new ArrayList<>();
46 for (Match match : this)
47 if (match.eventId() == eventId)
49 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
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)
58 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
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)
67 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
71 public Matches ended() {
72 List<Match> matches = new ArrayList<>();
73 for (Match match : this)
74 if (match.endDate() != null)
76 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
80 public Matches current() {
81 List<Match> matches = new ArrayList<>();
82 for (Match match : this)
83 if (match.startDate() != null && match.endDate() == null)
85 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
89 public Matches upcoming() {
90 List<Match> matches = new ArrayList<>();
91 for (Match match : this)
92 if (match.startDate() == null)
94 return matches.isEmpty() ? EMPTY : new JsonMatches(matches);
98 public void sortByNumber() {
100 MatchComparators.ROUND_NUMBER.comparator()
105 public void sortByEvent() {
107 MatchComparators.EVENT.comparator()