[LIB-2] Add snooker-score-api source files
[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     protected JsonRankings(Ranking[] entities) {
34         super(entities);
35     }
36
37     protected JsonRankings(List<Ranking> entities) {
38         super(entities);
39     }
40
41     @Override
42     public Ranking byPlayer(int playerId) {
43         for (Ranking ranking : this)
44             if (ranking.playerId() == playerId)
45                 return ranking;
46         return null;
47     }
48
49     @Override
50     public Rankings bySum(long minSum, long maxSum) {
51         List<Ranking> rankings = new ArrayList<>();
52         for (Ranking ranking : this)
53             if (ranking.sum() >= minSum && ranking.sum() <= maxSum)
54                 rankings.add(ranking);
55         return rankings.isEmpty() ? null : new JsonRankings(rankings);
56     }
57
58     @Override
59     public void sortByPosition() {
60         sort(
61                 RankingComparators.POSITION.comparator()
62         );
63     }
64
65     @Override
66     public void sortBySum() {
67         sort(
68                 RankingComparators.SUM.comparator()
69         );
70     }
71 }