[LIB-8] Add Event Rounds and Seedings functional
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonSeedings.java
1 /*
2  * Copyright (c) 2017-2019. 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.Seeding;
23 import org.hedgecode.snooker.api.Seedings;
24 import org.hedgecode.snooker.compare.SeedingComparators;
25
26 /**
27  * Seedings Entity (set of {@link Seeding}) to JSON deserialize.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public class JsonSeedings extends JsonCollectionEntity<Seeding> implements Seedings {
32
33     private static final JsonSeedings EMPTY = new JsonSeedings(new Seeding[0]);
34
35     protected JsonSeedings(Seeding[] entities) {
36         super(entities);
37     }
38
39     protected JsonSeedings(List<Seeding> entities) {
40         super(entities);
41     }
42
43     @Override
44     public Seedings byEvent(int eventId) {
45         List<Seeding> seedings = new ArrayList<>();
46         for (Seeding seeding : this)
47             if (seeding.eventId() == eventId)
48                 seedings.add(seeding);
49         return seedings.isEmpty() ? EMPTY : new JsonSeedings(seedings);
50     }
51
52     @Override
53     public Seeding byPlayer(int playerId) {
54         for (Seeding seeding : this)
55             if (seeding.playerId() == playerId)
56                 return seeding;
57         return null;
58     }
59
60     @Override
61     public Seeding bySeeding(int seedingNum) {
62         for (Seeding seeding : this)
63             if (seeding.seeding() == seedingNum)
64                 return seeding;
65         return null;
66     }
67
68     @Override
69     public void sortByEvent() {
70         sort(
71                 SeedingComparators.EVENT.comparator()
72         );
73     }
74
75     @Override
76     public void sortByPlayer() {
77         sort(
78                 SeedingComparators.PLAYER.comparator()
79         );
80     }
81
82     @Override
83     public void sortBySeeding() {
84         sort(
85                 SeedingComparators.SEEDING.comparator()
86         );
87     }
88
89 }