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