[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonPlayers.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.hedgecode.snooker.api.Player;
23 import org.hedgecode.snooker.api.Players;
24 import org.hedgecode.snooker.compare.PlayerComparators;
25
26 /**
27  * Players Entity (set of {@link Player}) to JSON deserialize.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public class JsonPlayers extends JsonCollectionEntity<Player> implements Players {
32
33     protected JsonPlayers(Player[] entities) {
34         super(entities);
35     }
36
37     protected JsonPlayers(List<Player> entities) {
38         super(entities);
39     }
40
41     @Override
42     public Players byType(int type) {
43         List<Player> players = new ArrayList<>();
44         for (Player player : this)
45             if (player.type() == type)
46                 players.add(player);
47         return players.isEmpty() ? null : new JsonPlayers(players);
48     }
49
50     @Override
51     public Players byName(String name) {
52         List<Player> players = new ArrayList<>();
53         for (Player player : this)
54             if (player.shortName().contains(name))
55                 players.add(player);
56         return players.isEmpty() ? null : new JsonPlayers(players);
57     }
58
59     @Override
60     public Players byNationality(String nationality) {
61         List<Player> players = new ArrayList<>();
62         for (Player player : this)
63             if (player.nationality().equals(nationality))
64                 players.add(player);
65         return players.isEmpty() ? null : new JsonPlayers(players);
66     }
67
68     @Override
69     public Players bySex(String sex) {
70         List<Player> players = new ArrayList<>();
71         for (Player player : this)
72             if (player.nationality().equals(sex))
73                 players.add(player);
74         return players.isEmpty() ? null : new JsonPlayers(players);
75     }
76
77     @Override
78     public void sortByName() {
79         sort(
80                 PlayerComparators.NAME.comparator()
81         );
82     }
83
84     @Override
85     public void sortByAge() {
86         sort(
87                 PlayerComparators.AGE.comparator()
88         );
89     }
90
91     @Override
92     public void sortByAgeDesc() {
93         sort(
94                 PlayerComparators.AGE.comparator().reversed()
95         );
96     }
97
98 }