[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonMatches.java
diff --git a/src/main/java/org/hedgecode/snooker/json/JsonMatches.java b/src/main/java/org/hedgecode/snooker/json/JsonMatches.java
new file mode 100644 (file)
index 0000000..0d4dd02
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2017. 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.Match;
+import org.hedgecode.snooker.api.Matches;
+import org.hedgecode.snooker.compare.MatchComparators;
+
+/**
+ * Matches Entity (set of {@link Match}) to JSON deserialize.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JsonMatches extends JsonCollectionEntity<Match> implements Matches {
+
+    protected JsonMatches(Match[] entities) {
+        super(entities);
+    }
+
+    protected JsonMatches(List<Match> entities) {
+        super(entities);
+    }
+
+    @Override
+    public Matches byEvent(int eventId) {
+        List<Match> matches = new ArrayList<>();
+        for (Match match : this)
+            if (match.eventId() == eventId)
+                matches.add(match);
+        return matches.isEmpty() ? null : new JsonMatches(matches);
+    }
+
+    @Override
+    public Matches byRound(int eventId, int round) {
+        List<Match> matches = new ArrayList<>();
+        for (Match match : this)
+            if (match.eventId() == eventId && match.round() == round)
+                matches.add(match);
+        return matches.isEmpty() ? null : new JsonMatches(matches);
+    }
+
+    @Override
+    public Matches byPlayer(int playerId) {
+        List<Match> matches = new ArrayList<>();
+        for (Match match : this)
+            if (match.player1Id() == playerId || match.player2Id() == playerId)
+                matches.add(match);
+        return matches.isEmpty() ? null : new JsonMatches(matches);
+    }
+
+    @Override
+    public Matches ended() {
+        List<Match> matches = new ArrayList<>();
+        for (Match match : this)
+            if (match.endDate() != null)
+                matches.add(match);
+        return matches.isEmpty() ? null : new JsonMatches(matches);
+    }
+
+    @Override
+    public Matches current() {
+        List<Match> matches = new ArrayList<>();
+        for (Match match : this)
+            if (match.startDate() != null && match.endDate() == null)
+                matches.add(match);
+        return matches.isEmpty() ? null : new JsonMatches(matches);
+    }
+
+    @Override
+    public Matches upcoming() {
+        List<Match> matches = new ArrayList<>();
+        for (Match match : this)
+            if (match.startDate() == null)
+                matches.add(match);
+        return matches.isEmpty() ? null : new JsonMatches(matches);
+    }
+
+    @Override
+    public void sortByNumber() {
+        sort(
+                MatchComparators.ROUND_NUMBER.comparator()
+        );
+    }
+
+    @Override
+    public void sortByEvent() {
+        sort(
+                MatchComparators.EVENT.comparator()
+        );
+    }
+
+}