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