-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
147 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters