[LIB-8] Add Event Rounds and Seedings functional
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonRounds.java
diff --git a/src/main/java/org/hedgecode/snooker/json/JsonRounds.java b/src/main/java/org/hedgecode/snooker/json/JsonRounds.java
new file mode 100644 (file)
index 0000000..eaa1a2d
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2017-2019. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.snooker.json;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hedgecode.snooker.api.Round;
+import org.hedgecode.snooker.api.Rounds;
+import org.hedgecode.snooker.compare.RoundComparators;
+
+/**
+ * Rounds Entity (set of {@link Round}) to JSON deserialize.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JsonRounds extends JsonCollectionEntity<Round> implements Rounds {
+
+    private static final JsonRounds EMPTY = new JsonRounds(new Round[0]);
+
+    protected JsonRounds(Round[] entities) {
+        super(entities);
+    }
+
+    protected JsonRounds(List<Round> entities) {
+        super(entities);
+    }
+
+    @Override
+    public Round byRound(int roundId) {
+        for (Round round : this)
+            if (round.round() == roundId)
+                return round;
+        return null;
+    }
+
+    @Override
+    public Rounds byRoundName(String roundName) {
+        if (roundName == null || roundName.isEmpty())
+            return EMPTY;
+        List<Round> rounds = new ArrayList<>();
+        for (Round round : this)
+            if (roundName.equals(round.roundName()))
+                rounds.add(round);
+        return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
+    }
+
+    @Override
+    public Rounds byEvent(int eventId) {
+        List<Round> rounds = new ArrayList<>();
+        for (Round round : this)
+            if (round.eventId() == eventId)
+                rounds.add(round);
+        return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
+    }
+
+    @Override
+    public Rounds byMainEvent(int mainEventId) {
+        List<Round> rounds = new ArrayList<>();
+        for (Round round : this)
+            if (round.mainEventId() == mainEventId)
+                rounds.add(round);
+        return rounds.isEmpty() ? EMPTY : new JsonRounds(rounds);
+    }
+
+    @Override
+    public void sortByRound() {
+        sort(
+                RoundComparators.ROUND.comparator()
+        );
+    }
+
+    @Override
+    public void sortByEvent() {
+        sort(
+                RoundComparators.EVENT.comparator()
+        );
+    }
+
+}