From b02641a13c88baac10eb2a9e297532851c5056de Mon Sep 17 00:00:00 2001 From: Marcin Kordus Date: Sun, 24 Sep 2017 00:22:11 +0200 Subject: [PATCH] Iterable{Input,Output}Provider --- src/main/java/pega/MergeSortFile.java | 5 +- src/main/java/pega/command/MergeCommand.java | 29 ++++++++ .../pega/io/FileIterableInputProvider.java | 2 +- .../pega/io/FileIterableOutputProvider.java | 32 +++++++++ src/main/java/pega/io/FileOutputProvider.java | 10 +-- .../java/pega/io/IterableInputProvider.java | 4 +- .../java/pega/io/IterableOutputProvider.java | 8 +++ src/main/java/pega/io/OutputProvider.java | 1 - .../io/FileIterableOutputProviderTest.java | 70 +++++++++++++++++++ .../java/pega/io/FileOutputProviderTest.java | 18 +---- 10 files changed, 147 insertions(+), 32 deletions(-) create mode 100644 src/main/java/pega/command/MergeCommand.java create mode 100644 src/main/java/pega/io/FileIterableOutputProvider.java create mode 100644 src/main/java/pega/io/IterableOutputProvider.java create mode 100644 src/test/java/pega/io/FileIterableOutputProviderTest.java diff --git a/src/main/java/pega/MergeSortFile.java b/src/main/java/pega/MergeSortFile.java index b445164..bfaad59 100644 --- a/src/main/java/pega/MergeSortFile.java +++ b/src/main/java/pega/MergeSortFile.java @@ -43,10 +43,7 @@ public void sort() throws IOException { 0, inputSize ), - new FileOutputProvider( - outputFile, - false - ) + new FileOutputProvider(outputFile) ); SortCommandExecutor executor = new SortCommandExecutor(); diff --git a/src/main/java/pega/command/MergeCommand.java b/src/main/java/pega/command/MergeCommand.java new file mode 100644 index 0000000..7d71a89 --- /dev/null +++ b/src/main/java/pega/command/MergeCommand.java @@ -0,0 +1,29 @@ +package pega.command; + +import pega.io.IterableInputProvider; +import pega.io.OutputProvider; + +public class MergeCommand { + + private IterableInputProvider fistInputProvider; + private IterableInputProvider secondInputProvider; + private OutputProvider outputProvider; + + public MergeCommand(IterableInputProvider fistInputProvider, IterableInputProvider secondInputProvider, OutputProvider outputProvider) { + this.fistInputProvider = fistInputProvider; + this.secondInputProvider = secondInputProvider; + this.outputProvider = outputProvider; + } + + public IterableInputProvider getFistInputProvider() { + return fistInputProvider; + } + + public IterableInputProvider getSecondInputProvider() { + return secondInputProvider; + } + + public OutputProvider getOutputProvider() { + return outputProvider; + } +} diff --git a/src/main/java/pega/io/FileIterableInputProvider.java b/src/main/java/pega/io/FileIterableInputProvider.java index e7018f1..8618725 100644 --- a/src/main/java/pega/io/FileIterableInputProvider.java +++ b/src/main/java/pega/io/FileIterableInputProvider.java @@ -2,7 +2,7 @@ import java.io.*; -public class FileIterableInputProvider implements IterableInputProvider, AutoCloseable { +public class FileIterableInputProvider implements IterableInputProvider { private final File file; private final int startPosition; diff --git a/src/main/java/pega/io/FileIterableOutputProvider.java b/src/main/java/pega/io/FileIterableOutputProvider.java new file mode 100644 index 0000000..0d9cc06 --- /dev/null +++ b/src/main/java/pega/io/FileIterableOutputProvider.java @@ -0,0 +1,32 @@ +package pega.io; + +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; + +public class FileIterableOutputProvider implements IterableOutputProvider { + + private final File file; + private RandomAccessFile output; + + public FileIterableOutputProvider(File file) { + this.file = file; + } + + @Override + public void write(int element) throws IOException { + if (output == null) { + output = new RandomAccessFile(file, "rw"); + output.seek(output.length()); + } + + output.writeInt(element); + } + + @Override + public void close() throws IOException { + if (output != null) { + output.close(); + } + } +} diff --git a/src/main/java/pega/io/FileOutputProvider.java b/src/main/java/pega/io/FileOutputProvider.java index 0ea5e5c..e8266b8 100644 --- a/src/main/java/pega/io/FileOutputProvider.java +++ b/src/main/java/pega/io/FileOutputProvider.java @@ -7,11 +7,9 @@ public class FileOutputProvider implements OutputProvider { private final File file; - private final boolean append; - public FileOutputProvider(File file, boolean append) { + public FileOutputProvider(File file) { this.file = file; - this.append = append; } @Override @@ -20,11 +18,7 @@ public void write(int[] output) throws IOException { file, "rw" )) { - if (append) { - outputFile.seek(outputFile.length()); - } else { - outputFile.setLength(0); - } + outputFile.setLength(0); for (int element : output) { outputFile.writeInt(element); diff --git a/src/main/java/pega/io/IterableInputProvider.java b/src/main/java/pega/io/IterableInputProvider.java index d422b8e..24802af 100644 --- a/src/main/java/pega/io/IterableInputProvider.java +++ b/src/main/java/pega/io/IterableInputProvider.java @@ -1,9 +1,7 @@ package pega.io; -import java.io.EOFException; -import java.io.FileNotFoundException; import java.io.IOException; -public interface IterableInputProvider { +public interface IterableInputProvider extends AutoCloseable { int getNext() throws IOException; } diff --git a/src/main/java/pega/io/IterableOutputProvider.java b/src/main/java/pega/io/IterableOutputProvider.java new file mode 100644 index 0000000..b4048f2 --- /dev/null +++ b/src/main/java/pega/io/IterableOutputProvider.java @@ -0,0 +1,8 @@ +package pega.io; + +import java.io.IOException; + +public interface IterableOutputProvider { + void write(int output) throws IOException; + void close() throws IOException; +} diff --git a/src/main/java/pega/io/OutputProvider.java b/src/main/java/pega/io/OutputProvider.java index 5039b40..7215359 100644 --- a/src/main/java/pega/io/OutputProvider.java +++ b/src/main/java/pega/io/OutputProvider.java @@ -1,6 +1,5 @@ package pega.io; -import java.io.FileNotFoundException; import java.io.IOException; public interface OutputProvider { diff --git a/src/test/java/pega/io/FileIterableOutputProviderTest.java b/src/test/java/pega/io/FileIterableOutputProviderTest.java new file mode 100644 index 0000000..f8dfba3 --- /dev/null +++ b/src/test/java/pega/io/FileIterableOutputProviderTest.java @@ -0,0 +1,70 @@ +package pega.io; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import java.io.EOFException; +import java.io.File; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.Arrays; + +import static org.junit.Assert.assertArrayEquals; + +@RunWith(MockitoJUnitRunner.class) +public class FileIterableOutputProviderTest { + + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + private File file; + + @Before + public void setUp() throws IOException { + file = folder.newFile("test"); + } + + @Test + public void testWriteToExistingFile() throws IOException { + int[] input = {6, 7, 8}; + int[] expectedOutput = {1, 2, 3, 4, 5, 6, 7, 8}; + fillFile(); + + FileIterableOutputProvider provider = new FileIterableOutputProvider(file); + for (int element : input) { + provider.write(element); + } + + provider.close(); + + assertArrayEquals(expectedOutput, readFile()); + } + + private void fillFile() throws IOException { + try (RandomAccessFile outputFile = new RandomAccessFile( + file, + "rw" + )) { + for (int i = 1; i <= 5; i++) { + outputFile.writeInt(i); + } + } + } + + private int[] readFile() throws IOException { + int[] result = new int[20]; + int index = 0; + + try (RandomAccessFile input = new RandomAccessFile(file, "r")) { + while (true) { + result[index++] = input.readInt(); + } + } catch (EOFException ignored) { + } + + return Arrays.copyOf(result, index - 1); + } +} \ No newline at end of file diff --git a/src/test/java/pega/io/FileOutputProviderTest.java b/src/test/java/pega/io/FileOutputProviderTest.java index 310ccf4..7e2575e 100644 --- a/src/test/java/pega/io/FileOutputProviderTest.java +++ b/src/test/java/pega/io/FileOutputProviderTest.java @@ -31,35 +31,23 @@ public void setUp() throws IOException { public void testWriteToEmptyFileWithoutAppend() throws IOException { int[] input = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - new FileOutputProvider(file, false) + new FileOutputProvider(file) .write(input); assertArrayEquals(input, readFile()); } @Test - public void testWriteToExistingFileWithoutAppend() throws IOException { + public void testWriteToExistingFile() throws IOException { int[] input = {1, 2, 3}; fillFile(); - new FileOutputProvider(file, false) + new FileOutputProvider(file) .write(input); assertArrayEquals(input, readFile()); } - @Test - public void testWriteToExistingFileWithAppend() throws IOException { - int[] input = {6, 7, 8}; - int[] expectedOutput = {1, 2, 3, 4, 5, 6, 7, 8}; - fillFile(); - - new FileOutputProvider(file, true) - .write(input); - - assertArrayEquals(expectedOutput, readFile()); - } - private void fillFile() throws IOException { try (RandomAccessFile outputFile = new RandomAccessFile( file,