[LIB-9] Separate chesshog-core module
[chesshog.git] / chesshog-core / src / main / java / org / hedgecode / chess / position / SquareSort.java
diff --git a/chesshog-core/src/main/java/org/hedgecode/chess/position/SquareSort.java b/chesshog-core/src/main/java/org/hedgecode/chess/position/SquareSort.java
new file mode 100644 (file)
index 0000000..6c460b9
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2018. 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.chess.position;
+
+import java.util.Comparator;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public enum SquareSort {
+
+    A1H8 (
+            new A1H8Comparator()
+    ),
+
+    H8A1 (
+            new H8A1Comparator()
+    ),
+
+    A8H1 (
+            new A8H1Comparator()
+    );
+
+    Comparator<Square> comparator;
+
+    SquareSort(Comparator<Square> comparator) {
+        this.comparator = comparator;
+    }
+
+    public Comparator<Square> comparator() {
+        return comparator;
+    }
+
+    public static SquareSort defaultSort() {
+        return SquareSort.A1H8;
+    }
+
+
+    static class A1H8Comparator implements Comparator<Square> {
+
+        @Override
+        public int compare(Square square1, Square square2) {
+            return square1.getH() == square2.getH()
+                    ? square1.getV() == square2.getV() ? 0
+                    : square1.getV() > square2.getV() ? 1 : -1
+                    : square1.getH() > square2.getH() ? 1 : -1;
+        }
+    }
+
+    static class H8A1Comparator implements Comparator<Square> {
+
+        @Override
+        public int compare(Square square1, Square square2) {
+            return square1.getH() == square2.getH()
+                    ? square1.getV() == square2.getV() ? 0
+                    : square1.getV() < square2.getV() ? 1 : -1
+                    : square1.getH() < square2.getH() ? 1 : -1;
+        }
+    }
+
+    static class A8H1Comparator implements Comparator<Square> {
+
+        @Override
+        public int compare(Square square1, Square square2) {
+            return square1.getH() == square2.getH()
+                    ? square1.getV() == square2.getV() ? 0
+                    : square1.getV() > square2.getV() ? 1 : -1
+                    : square1.getH() < square2.getH() ? 1 : -1;
+        }
+    }
+
+}