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.Player;
23 import org.hedgecode.snooker.api.Players;
24 import org.hedgecode.snooker.compare.PlayerComparators;
27 * Players Entity (set of {@link Player}) to JSON deserialize.
29 * @author Dmitry Samoshin aka gotty
31 public class JsonPlayers extends JsonCollectionEntity<Player> implements Players {
33 private static final JsonPlayers EMPTY = new JsonPlayers(new Player[0]);
35 protected JsonPlayers(Player[] entities) {
39 protected JsonPlayers(List<Player> entities) {
44 public Players byType(int type) {
45 List<Player> players = new ArrayList<>();
46 for (Player player : this)
47 if (player.type() == type)
49 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
53 public Players byName(String name) {
54 List<Player> players = new ArrayList<>();
55 for (Player player : this)
56 if (player.shortName().contains(name))
58 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
62 public Players byNationality(String nationality) {
63 List<Player> players = new ArrayList<>();
64 for (Player player : this)
65 if (player.nationality().equals(nationality))
67 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
71 public Players bySex(String sex) {
72 List<Player> players = new ArrayList<>();
73 for (Player player : this)
74 if (player.nationality().equals(sex))
76 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
80 public void sortByName() {
82 PlayerComparators.NAME.comparator()
87 public void sortByAge() {
89 PlayerComparators.AGE.comparator()
94 public void sortByAgeDesc() {
96 PlayerComparators.AGE.comparator().reversed()