[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonPlayers.java
diff --git a/src/main/java/org/hedgecode/snooker/json/JsonPlayers.java b/src/main/java/org/hedgecode/snooker/json/JsonPlayers.java
new file mode 100644 (file)
index 0000000..26c68e2
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * 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.Player;
+import org.hedgecode.snooker.api.Players;
+import org.hedgecode.snooker.compare.PlayerComparators;
+
+/**
+ * Players Entity (set of {@link Player}) to JSON deserialize.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class JsonPlayers extends JsonCollectionEntity<Player> implements Players {
+
+    protected JsonPlayers(Player[] entities) {
+        super(entities);
+    }
+
+    protected JsonPlayers(List<Player> entities) {
+        super(entities);
+    }
+
+    @Override
+    public Players byType(int type) {
+        List<Player> players = new ArrayList<>();
+        for (Player player : this)
+            if (player.type() == type)
+                players.add(player);
+        return players.isEmpty() ? null : new JsonPlayers(players);
+    }
+
+    @Override
+    public Players byName(String name) {
+        List<Player> players = new ArrayList<>();
+        for (Player player : this)
+            if (player.shortName().contains(name))
+                players.add(player);
+        return players.isEmpty() ? null : new JsonPlayers(players);
+    }
+
+    @Override
+    public Players byNationality(String nationality) {
+        List<Player> players = new ArrayList<>();
+        for (Player player : this)
+            if (player.nationality().equals(nationality))
+                players.add(player);
+        return players.isEmpty() ? null : new JsonPlayers(players);
+    }
+
+    @Override
+    public Players bySex(String sex) {
+        List<Player> players = new ArrayList<>();
+        for (Player player : this)
+            if (player.nationality().equals(sex))
+                players.add(player);
+        return players.isEmpty() ? null : new JsonPlayers(players);
+    }
+
+    @Override
+    public void sortByName() {
+        sort(
+                PlayerComparators.NAME.comparator()
+        );
+    }
+
+    @Override
+    public void sortByAge() {
+        sort(
+                PlayerComparators.AGE.comparator()
+        );
+    }
+
+    @Override
+    public void sortByAgeDesc() {
+        sort(
+                PlayerComparators.AGE.comparator().reversed()
+        );
+    }
+
+}