/* * 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.Player; import org.hedgecode.snooker.api.Players; import org.hedgecode.snooker.compare.PlayerComparators; /** * Players Entity (set of {@link Player}) to JSON deserialize. * * @author Dmitry Samoshin aka gotty */ public class JsonPlayers extends JsonCollectionEntity implements Players { private static final JsonPlayers EMPTY = new JsonPlayers(new Player[0]); protected JsonPlayers(Player[] entities) { super(entities); } protected JsonPlayers(List entities) { super(entities); } @Override public Players byType(int type) { List players = new ArrayList<>(); for (Player player : this) if (player.type() == type) players.add(player); return players.isEmpty() ? EMPTY : new JsonPlayers(players); } @Override public Players byName(String name) { List players = new ArrayList<>(); for (Player player : this) if (player.shortName().contains(name)) players.add(player); return players.isEmpty() ? EMPTY : new JsonPlayers(players); } @Override public Players byNationality(String nationality) { List players = new ArrayList<>(); for (Player player : this) if (player.nationality().equals(nationality)) players.add(player); return players.isEmpty() ? EMPTY : new JsonPlayers(players); } @Override public Players bySex(String sex) { List players = new ArrayList<>(); for (Player player : this) if (player.nationality().equals(sex)) players.add(player); return players.isEmpty() ? EMPTY : new JsonPlayers(players); } @Override public void sortByName() { sort( PlayerComparators.NAME.comparator() ); } @Override public void sortByAge() { sort( PlayerComparators.AGE.comparator() ); } @Override public void sortByAgeDesc() { sort( PlayerComparators.AGE.comparator().reversed() ); } }