2 * Copyright (c) 2017. 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 protected JsonMatches(Match[] entities) {
37 protected JsonMatches(List<Match> entities) {
42 public Matches byEvent(int eventId) {
43 List<Match> matches = new ArrayList<>();
44 for (Match match : this)
45 if (match.eventId() == eventId)
47 return matches.isEmpty() ? null : new JsonMatches(matches);
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)
56 return matches.isEmpty() ? null : new JsonMatches(matches);
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)
65 return matches.isEmpty() ? null : new JsonMatches(matches);
69 public Matches ended() {
70 List<Match> matches = new ArrayList<>();
71 for (Match match : this)
72 if (match.endDate() != null)
74 return matches.isEmpty() ? null : new JsonMatches(matches);
78 public Matches current() {
79 List<Match> matches = new ArrayList<>();
80 for (Match match : this)
81 if (match.startDate() != null && match.endDate() == null)
83 return matches.isEmpty() ? null : new JsonMatches(matches);
87 public Matches upcoming() {
88 List<Match> matches = new ArrayList<>();
89 for (Match match : this)
90 if (match.startDate() == null)
92 return matches.isEmpty() ? null : new JsonMatches(matches);
96 public void sortByNumber() {
98 MatchComparators.ROUND_NUMBER.comparator()
103 public void sortByEvent() {
105 MatchComparators.EVENT.comparator()