[LIB-8] Add Event Rounds and Seedings functional
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonSeedings.java
diff --git a/src/main/java/org/hedgecode/snooker/json/JsonSeedings.java b/src/main/java/org/hedgecode/snooker/json/JsonSeedings.java
new file mode 100644 (file)
index 0000000..a504cff
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * 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.Seeding;
+import org.hedgecode.snooker.api.Seedings;
+import org.hedgecode.snooker.compare.SeedingComparators;
+
+/**
+ * Seedings Entity (set of {@link Seeding}) to JSON deserialize.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JsonSeedings extends JsonCollectionEntity<Seeding> implements Seedings {
+
+    private static final JsonSeedings EMPTY = new JsonSeedings(new Seeding[0]);
+
+    protected JsonSeedings(Seeding[] entities) {
+        super(entities);
+    }
+
+    protected JsonSeedings(List<Seeding> entities) {
+        super(entities);
+    }
+
+    @Override
+    public Seedings byEvent(int eventId) {
+        List<Seeding> seedings = new ArrayList<>();
+        for (Seeding seeding : this)
+            if (seeding.eventId() == eventId)
+                seedings.add(seeding);
+        return seedings.isEmpty() ? EMPTY : new JsonSeedings(seedings);
+    }
+
+    @Override
+    public Seeding byPlayer(int playerId) {
+        for (Seeding seeding : this)
+            if (seeding.playerId() == playerId)
+                return seeding;
+        return null;
+    }
+
+    @Override
+    public Seeding bySeeding(int seedingNum) {
+        for (Seeding seeding : this)
+            if (seeding.seeding() == seedingNum)
+                return seeding;
+        return null;
+    }
+
+    @Override
+    public void sortByEvent() {
+        sort(
+                SeedingComparators.EVENT.comparator()
+        );
+    }
+
+    @Override
+    public void sortByPlayer() {
+        sort(
+                SeedingComparators.PLAYER.comparator()
+        );
+    }
+
+    @Override
+    public void sortBySeeding() {
+        sort(
+                SeedingComparators.SEEDING.comparator()
+        );
+    }
+
+}