/* * Copyright (c) 2017. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.snooker.json; import java.util.ArrayList; import java.util.List; import org.hedgecode.snooker.api.Match; import org.hedgecode.snooker.api.Matches; import org.hedgecode.snooker.compare.MatchComparators; /** * Matches Entity (set of {@link Match}) to JSON deserialize. * * @author Dmitry Samoshin aka gotty */ public class JsonMatches extends JsonCollectionEntity implements Matches { private static final JsonMatches EMPTY = new JsonMatches(new Match[0]); protected JsonMatches(Match[] entities) { super(entities); } protected JsonMatches(List entities) { super(entities); } @Override public Matches byEvent(int eventId) { List matches = new ArrayList<>(); for (Match match : this) if (match.eventId() == eventId) matches.add(match); return matches.isEmpty() ? EMPTY : new JsonMatches(matches); } @Override public Matches byRound(int eventId, int round) { List matches = new ArrayList<>(); for (Match match : this) if (match.eventId() == eventId && match.round() == round) matches.add(match); return matches.isEmpty() ? EMPTY : new JsonMatches(matches); } @Override public Matches byPlayer(int playerId) { List matches = new ArrayList<>(); for (Match match : this) if (match.player1Id() == playerId || match.player2Id() == playerId) matches.add(match); return matches.isEmpty() ? EMPTY : new JsonMatches(matches); } @Override public Matches ended() { List matches = new ArrayList<>(); for (Match match : this) if (match.endDate() != null) matches.add(match); return matches.isEmpty() ? EMPTY : new JsonMatches(matches); } @Override public Matches current() { List matches = new ArrayList<>(); for (Match match : this) if (match.startDate() != null && match.endDate() == null) matches.add(match); return matches.isEmpty() ? EMPTY : new JsonMatches(matches); } @Override public Matches upcoming() { List matches = new ArrayList<>(); for (Match match : this) if (match.startDate() == null) matches.add(match); return matches.isEmpty() ? EMPTY : new JsonMatches(matches); } @Override public void sortByNumber() { sort( MatchComparators.ROUND_NUMBER.comparator() ); } @Override public void sortByEvent() { sort( MatchComparators.EVENT.comparator() ); } }