Skip to content

Commit

Permalink
Iterable{Input,Output}Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mkordus committed Sep 23, 2017
1 parent 6f92e53 commit b02641a
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 32 deletions.
5 changes: 1 addition & 4 deletions src/main/java/pega/MergeSortFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ public void sort() throws IOException {
0,
inputSize
),
new FileOutputProvider(
outputFile,
false
)
new FileOutputProvider(outputFile)
);

SortCommandExecutor executor = new SortCommandExecutor();
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/pega/command/MergeCommand.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/pega/io/FileIterableInputProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/pega/io/FileIterableOutputProvider.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
10 changes: 2 additions & 8 deletions src/main/java/pega/io/FileOutputProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/pega/io/IterableInputProvider.java
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/main/java/pega/io/IterableOutputProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pega.io;

import java.io.IOException;

public interface IterableOutputProvider {
void write(int output) throws IOException;
void close() throws IOException;
}
1 change: 0 additions & 1 deletion src/main/java/pega/io/OutputProvider.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pega.io;

import java.io.FileNotFoundException;
import java.io.IOException;

public interface OutputProvider {
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/pega/io/FileIterableOutputProviderTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
18 changes: 3 additions & 15 deletions src/test/java/pega/io/FileOutputProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b02641a

Please sign in to comment.