/* * Copyright (c) 2017-2020. 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.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hedgecode.snooker.api.Seeding; import org.hedgecode.snooker.api.Seedings; import org.hedgecode.snooker.compare.SeedingComparators; /** * Seedings Entity (set of {@link Seeding}) to JSON deserialize. * * @author Dmitry Samoshin aka gotty */ public class JsonSeedings extends JsonCollectionEntity implements Seedings, Serializable { private static final long serialVersionUID = 8251290277292098461L; private static final JsonSeedings EMPTY = new JsonSeedings(new Seeding[0]); JsonSeedings(Seeding[] entities) { super(entities); } JsonSeedings(List entities) { super(entities); } @Override public Seedings byEvent(int eventId) { List seedings = new ArrayList<>(); for (Seeding seeding : this) if (seeding.eventId() == eventId) seedings.add(seeding); return seedings.isEmpty() ? EMPTY : new JsonSeedings(seedings); } @Override public Seeding byPlayer(int playerId) { for (Seeding seeding : this) if (seeding.playerId() == playerId) return seeding; return null; } @Override public Seeding bySeeding(int seedingNum) { for (Seeding seeding : this) if (seeding.seeding() == seedingNum) return seeding; return null; } @Override public void sortByEvent() { sort( SeedingComparators.EVENT.comparator() ); } @Override public void sortByPlayer() { sort( SeedingComparators.PLAYER.comparator() ); } @Override public void sortBySeeding() { sort( SeedingComparators.SEEDING.comparator() ); } }