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.io.Serializable;
20 import java.util.ArrayList;
21 import java.util.List;
23 import org.hedgecode.snooker.api.Player;
24 import org.hedgecode.snooker.api.Players;
25 import org.hedgecode.snooker.compare.PlayerComparators;
28 * Players Entity (set of {@link Player}) to JSON deserialize.
30 * @author Dmitry Samoshin aka gotty
32 public class JsonPlayers extends JsonCollectionEntity<Player> implements Players, Serializable {
34 private static final long serialVersionUID = 7472642207778686468L;
36 private static final JsonPlayers EMPTY = new JsonPlayers(new Player[0]);
38 JsonPlayers(Player[] entities) {
42 JsonPlayers(List<Player> entities) {
47 public Players byType(int type) {
48 List<Player> players = new ArrayList<>();
49 for (Player player : this)
50 if (player.type() == type)
52 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
56 public Players byName(String name) {
57 List<Player> players = new ArrayList<>();
58 for (Player player : this)
59 if (player.shortName().contains(name))
61 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
65 public Players byNationality(String nationality) {
66 List<Player> players = new ArrayList<>();
67 for (Player player : this)
68 if (player.nationality().equals(nationality))
70 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
74 public Players bySex(String sex) {
75 List<Player> players = new ArrayList<>();
76 for (Player player : this)
77 if (player.nationality().equals(sex))
79 return players.isEmpty() ? EMPTY : new JsonPlayers(players);
83 public void sortByName() {
85 PlayerComparators.NAME.comparator()
90 public void sortByAge() {
92 PlayerComparators.AGE.comparator()
97 public void sortByAgeDesc() {
99 PlayerComparators.AGE.comparator().reversed()