[LIB-5] Collection empty objects,reporting and serializable
[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     private static final JsonPlayers EMPTY = new JsonPlayers(new Player[0]);
34
35     protected JsonPlayers(Player[] entities) {
36         super(entities);
37     }
38
39     protected JsonPlayers(List<Player> entities) {
40         super(entities);
41     }
42
43     @Override
44     public Players byType(int type) {
45         List<Player> players = new ArrayList<>();
46         for (Player player : this)
47             if (player.type() == type)
48                 players.add(player);
49         return players.isEmpty() ? EMPTY : new JsonPlayers(players);
50     }
51
52     @Override
53     public Players byName(String name) {
54         List<Player> players = new ArrayList<>();
55         for (Player player : this)
56             if (player.shortName().contains(name))
57                 players.add(player);
58         return players.isEmpty() ? EMPTY : new JsonPlayers(players);
59     }
60
61     @Override
62     public Players byNationality(String nationality) {
63         List<Player> players = new ArrayList<>();
64         for (Player player : this)
65             if (player.nationality().equals(nationality))
66                 players.add(player);
67         return players.isEmpty() ? EMPTY : new JsonPlayers(players);
68     }
69
70     @Override
71     public Players bySex(String sex) {
72         List<Player> players = new ArrayList<>();
73         for (Player player : this)
74             if (player.nationality().equals(sex))
75                 players.add(player);
76         return players.isEmpty() ? EMPTY : new JsonPlayers(players);
77     }
78
79     @Override
80     public void sortByName() {
81         sort(
82                 PlayerComparators.NAME.comparator()
83         );
84     }
85
86     @Override
87     public void sortByAge() {
88         sort(
89                 PlayerComparators.AGE.comparator()
90         );
91     }
92
93     @Override
94     public void sortByAgeDesc() {
95         sort(
96                 PlayerComparators.AGE.comparator().reversed()
97         );
98     }
99
100 }