2 * Copyright (c) 2017. 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 protected JsonPlayers(Player[] entities) {
37 protected JsonPlayers(List<Player> entities) {
42 public Players byType(int type) {
43 List<Player> players = new ArrayList<>();
44 for (Player player : this)
45 if (player.type() == type)
47 return players.isEmpty() ? null : new JsonPlayers(players);
51 public Players byName(String name) {
52 List<Player> players = new ArrayList<>();
53 for (Player player : this)
54 if (player.shortName().contains(name))
56 return players.isEmpty() ? null : new JsonPlayers(players);
60 public Players byNationality(String nationality) {
61 List<Player> players = new ArrayList<>();
62 for (Player player : this)
63 if (player.nationality().equals(nationality))
65 return players.isEmpty() ? null : new JsonPlayers(players);
69 public Players bySex(String sex) {
70 List<Player> players = new ArrayList<>();
71 for (Player player : this)
72 if (player.nationality().equals(sex))
74 return players.isEmpty() ? null : new JsonPlayers(players);
78 public void sortByName() {
80 PlayerComparators.NAME.comparator()
85 public void sortByAge() {
87 PlayerComparators.AGE.comparator()
92 public void sortByAgeDesc() {
94 PlayerComparators.AGE.comparator().reversed()