diff --git a/Spiral/.gitignore b/Spiral/.gitignore
new file mode 100644
index 0000000..345e61a
--- /dev/null
+++ b/Spiral/.gitignore
@@ -0,0 +1,49 @@
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
+
+# User-specific stuff:
+.idea/**/workspace.xml
+.idea/**/tasks.xml
+.idea/dictionaries
+
+# Sensitive or high-churn files:
+.idea/**/dataSources/
+.idea/**/dataSources.ids
+.idea/**/dataSources.xml
+.idea/**/dataSources.local.xml
+.idea/**/sqlDataSources.xml
+.idea/**/dynamic.xml
+.idea/**/uiDesigner.xml
+
+# Gradle:
+.idea/**/gradle.xml
+.idea/**/libraries
+
+# CMake
+cmake-build-debug/
+
+# Mongo Explorer plugin:
+.idea/**/mongoSettings.xml
+
+## File-based project format:
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Cursive Clojure plugin
+.idea/replstate.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
+fabric.properties
diff --git a/Spiral/.idea/compiler.xml b/Spiral/.idea/compiler.xml
new file mode 100644
index 0000000..f76440d
--- /dev/null
+++ b/Spiral/.idea/compiler.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Spiral/.idea/misc.xml b/Spiral/.idea/misc.xml
new file mode 100644
index 0000000..56c64d0
--- /dev/null
+++ b/Spiral/.idea/misc.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Spiral/.idea/modules.xml b/Spiral/.idea/modules.xml
new file mode 100644
index 0000000..97c24d4
--- /dev/null
+++ b/Spiral/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Spiral/SpiralMatrix.iml b/Spiral/SpiralMatrix.iml
new file mode 100644
index 0000000..0c047ef
--- /dev/null
+++ b/Spiral/SpiralMatrix.iml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Spiral/pom.xml b/Spiral/pom.xml
new file mode 100644
index 0000000..3bde29e
--- /dev/null
+++ b/Spiral/pom.xml
@@ -0,0 +1,43 @@
+
+
+ 4.0.0
+
+ ru.spbau.mit.kazakov.SpiralMatrix
+ SpiralMatrix
+ 1.0-SNAPSHOT
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 1.8
+ 1.8
+
+
+
+
+ jar
+
+
+
+ junit
+ junit
+ 4.0
+ test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ RELEASE
+
+
+ org.jetbrains
+ annotations
+ 13.0
+
+
+
+
\ No newline at end of file
diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java
new file mode 100644
index 0000000..28e0816
--- /dev/null
+++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/EvenDimensionException.java
@@ -0,0 +1,4 @@
+package ru.spbau.mit.kazakov.SpiralMatrix;
+
+public class EvenDimensionException extends Exception {
+}
diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java
new file mode 100644
index 0000000..9a4dcbe
--- /dev/null
+++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NotSquareMatrixException.java
@@ -0,0 +1,4 @@
+package ru.spbau.mit.kazakov.SpiralMatrix;
+
+public class NotSquareMatrixException extends Exception {
+}
diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java
new file mode 100644
index 0000000..7d19334
--- /dev/null
+++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/NullRowException.java
@@ -0,0 +1,4 @@
+package ru.spbau.mit.kazakov.SpiralMatrix;
+
+public class NullRowException extends Exception {
+}
diff --git a/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java
new file mode 100644
index 0000000..2d1e48a
--- /dev/null
+++ b/Spiral/src/main/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrix.java
@@ -0,0 +1,130 @@
+package ru.spbau.mit.kazakov.SpiralMatrix;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+
+/**
+ * Stores transpose square matrix with odd dimensions, sorts matrix's columns by first elements, prints matrix spirally.
+ */
+public class SpiralMatrix {
+ private final int[][] matrix;
+
+ private enum Direction {RIGHT, UP, LEFT, DOWN}
+
+ /**
+ * Initialize matrix with given data.
+ *
+ * @param data initializing matrix
+ */
+ public SpiralMatrix(@NotNull int[][] data) throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ checkMatrix(data);
+ matrix = new int[data.length][data.length];
+
+ for (int i = 0; i < data.length; i++) {
+ for (int j = 0; j < data.length; j++) {
+ matrix[i][j] = data[j][i];
+ }
+ }
+ }
+
+ /**
+ * Checks if given matrix is correct.
+ *
+ * @param data a matrix to check
+ * @throws NotSquareMatrixException if matrix isn't square
+ * @throws NullRowException if matrix contains null row
+ * @throws EvenDimensionException if matrix's dimensions is even
+ */
+ private void checkMatrix(@NotNull int[][] data) throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int rows = data.length;
+
+ if (rows % 2 != 1) {
+ throw new EvenDimensionException();
+ }
+
+ for (int[] col : data) {
+ if (col == null) {
+ throw new NullRowException();
+ }
+ if (col.length != rows) {
+ throw new NotSquareMatrixException();
+ }
+ }
+ }
+
+
+ /**
+ * Prints matrix: each element separated by space symbol, each row separated by new line symbol.
+ */
+ public void printMatrix() {
+ for (int i = 0; i < matrix.length; i++) {
+ for (int j = 0; j < matrix.length; j++) {
+ System.out.print(matrix[j][i] + " ");
+ }
+ System.out.println();
+ }
+ }
+
+ /**
+ * Sorts columns by first elements.
+ */
+ public void sortMatrix() {
+ Arrays.sort(matrix, Comparator.comparingInt(col -> col[0]));
+ }
+
+ /**
+ * Prints matrix in spiral order. Elements separated by space symbol.
+ */
+ public void printSpiralMatrix() {
+ int steps = 0;
+ int iCurrent = matrix.length / 2;
+ int jCurrent = matrix.length / 2;
+
+ while (true) {
+ for (Direction direction : Direction.values()) {
+ switch (direction) {
+ case RIGHT:
+ steps++;
+ for (int step = 1; step <= steps; step++) {
+ System.out.print(matrix[iCurrent][jCurrent] + " ");
+ iCurrent++;
+ }
+
+ if (steps == matrix.length) {
+ return;
+ }
+ break;
+
+ case UP:
+ for (int step = 1; step <= steps; step++) {
+ System.out.print(matrix[iCurrent][jCurrent] + " ");
+ jCurrent--;
+ }
+ break;
+
+ case LEFT:
+ steps++;
+ for (int step = 1; step <= steps; step++) {
+ System.out.print(matrix[iCurrent][jCurrent] + " ");
+ iCurrent--;
+ }
+
+ if (steps == matrix.length) {
+ return;
+ }
+ break;
+
+ case DOWN:
+ for (int step = 1; step <= steps; step++) {
+ System.out.print(matrix[iCurrent][jCurrent] + " ");
+ jCurrent++;
+ }
+ break;
+ }
+ }
+ }
+ }
+}
diff --git a/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java
new file mode 100644
index 0000000..45a4369
--- /dev/null
+++ b/Spiral/src/test/java/ru/spbau/mit/kazakov/SpiralMatrix/SpiralMatrixTest.java
@@ -0,0 +1,187 @@
+package ru.spbau.mit.kazakov.SpiralMatrix;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class SpiralMatrixTest {
+ private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
+
+ @Before
+ public void setUpStreams() {
+ System.setOut(new PrintStream(outContent));
+ }
+
+ @After
+ public void cleanUpStreams() {
+ System.setOut(null);
+ }
+
+ @Test
+ public void testConstructor() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ {54, 43, 32},
+ {21, 53, 76}};
+ new SpiralMatrix(matrix);
+ }
+
+ @Test(expected = EvenDimensionException.class)
+ public void testThrowsEvenDimensionException() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1},
+ {54, 43}};
+ new SpiralMatrix(matrix);
+ }
+
+ @Test(expected = NotSquareMatrixException.class)
+ public void testRaggedMatrixThrowsNotSquareMatrixException() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ {54, 43},
+ {21, 53, 76}};
+ new SpiralMatrix(matrix);
+ }
+
+ @Test(expected = NotSquareMatrixException.class)
+ public void testRectangleMatrixThrowsNotSquareMatrixException() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ {21, 53, 76}};
+ new SpiralMatrix(matrix);
+ }
+
+ @Test(expected = NullRowException.class)
+ public void testThrowsNullRowException() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ null,
+ {21, 53, 76}};
+ new SpiralMatrix(matrix);
+ }
+
+ @Test
+ public void testPrintMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ {54, 43, 32},
+ {21, 53, 76}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.printMatrix();
+
+ assertEquals("3 1 5 \n54 43 32 \n21 53 76", outContent.toString().trim());
+ }
+
+ @Test
+ public void testPrintMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5, -1, 0},
+ {54, 43, 32, 3, 2},
+ {21, 53, 76, 7, 2},
+ {21, 87, 32, 98, 23},
+ {1, 4, 6, 4, 3}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.printMatrix();
+
+ assertEquals("3 1 5 -1 0 \n" +
+ "54 43 32 3 2 \n" +
+ "21 53 76 7 2 \n" +
+ "21 87 32 98 23 \n" +
+ "1 4 6 4 3", outContent.toString().trim());
+ }
+
+ @Test
+ public void testPrintMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.printMatrix();
+
+ assertEquals("3", outContent.toString().trim());
+ }
+
+ @Test
+ public void testSortMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ {54, 43, 32},
+ {21, 53, 76}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.sortMatrix();
+ spiral.printMatrix();
+
+ assertEquals("1 3 5 \n43 54 32 \n53 21 76", outContent.toString().trim());
+ }
+
+ @Test
+ public void testPrintSortMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5, -1, 0},
+ {54, 43, 32, 3, 2},
+ {21, 53, 76, 7, 2},
+ {21, 87, 32, 98, 23},
+ {1, 4, 6, 4, 3}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.sortMatrix();
+ spiral.printMatrix();
+
+ assertEquals("-1 0 1 3 5 \n" +
+ "3 2 43 54 32 \n" +
+ "7 2 53 21 76 \n" +
+ "98 23 87 21 32 \n" +
+ "4 3 4 1 6", outContent.toString().trim());
+ }
+
+ @Test
+ public void testSortMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.sortMatrix();
+ spiral.printMatrix();
+
+ assertEquals("3", outContent.toString().trim());
+ }
+
+ @Test
+ public void testPrintSpiralMatrix3x3() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5},
+ {54, 43, 32},
+ {21, 53, 76}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.printSpiralMatrix();
+
+ assertEquals("43 32 5 1 3 54 21 53 76", outContent.toString().trim());
+ }
+
+ @Test
+ public void testPrintSpiralMatrix5x5() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3, 1, 5, 6, 7},
+ {54, 43, 32, 3, 2},
+ {21, 53, 76, 7, 2},
+ {21, 87, 32, 98, 23},
+ {1, 4, 6, 4, 3}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.printSpiralMatrix();
+
+ assertEquals("76 7 3 32 43 53 87 32 98 23 2 2 7 6 5 1 3 54 21 21 1 4 6 4 3", outContent.toString().trim());
+ }
+
+ @Test
+ public void testPrintSpiralMatrix1x1() throws NotSquareMatrixException, NullRowException, EvenDimensionException {
+ int[][] matrix = new int[][]{{3}};
+
+ SpiralMatrix spiral = new SpiralMatrix(matrix);
+ spiral.printSpiralMatrix();
+
+ assertEquals("3", outContent.toString().trim());
+ }
+
+ @Test
+ public void test() {
+ int[][] matrix = new int[][]{{1, 2}, {4, 5, 6}};
+ assertEquals(3, matrix[1].length);
+ }
+}
\ No newline at end of file