[LIB-5] Collection empty objects,reporting and serializable
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonRankings.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.Ranking;
23 import org.hedgecode.snooker.api.Rankings;
24 import org.hedgecode.snooker.compare.RankingComparators;
25
26 /**
27  * Rankings Entity (set of {@link Ranking}) to JSON deserialize.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public class JsonRankings extends JsonCollectionEntity<Ranking> implements Rankings {
32
33     private static final JsonRankings EMPTY = new JsonRankings(new Ranking[0]);
34
35     protected JsonRankings(Ranking[] entities) {
36         super(entities);
37     }
38
39     protected JsonRankings(List<Ranking> entities) {
40         super(entities);
41     }
42
43     @Override
44     public Ranking byPlayer(int playerId) {
45         for (Ranking ranking : this)
46             if (ranking.playerId() == playerId)
47                 return ranking;
48         return null;
49     }
50
51     @Override
52     public Rankings bySum(long minSum, long maxSum) {
53         List<Ranking> rankings = new ArrayList<>();
54         for (Ranking ranking : this)
55             if (ranking.sum() >= minSum && ranking.sum() <= maxSum)
56                 rankings.add(ranking);
57         return rankings.isEmpty() ? EMPTY : new JsonRankings(rankings);
58     }
59
60     @Override
61     public void sortByPosition() {
62         sort(
63                 RankingComparators.POSITION.comparator()
64         );
65     }
66
67     @Override
68     public void sortBySum() {
69         sort(
70                 RankingComparators.SUM.comparator()
71         );
72     }
73
74 }