Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #457

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions FileTextSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* This class is thread safe.
*/
public final class FileTextSource implements TextSource {
private final File file;

public FileTextSource(final File source) {
this.file = source;
}

public FileTextSource(final String path) {
this(new File(path));
}

public String getContent() throws IOException {
final FileInputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
output += (char) data;
}
return output;
}

@Deprecated
public synchronized void setFile(File f) {
throw new UnsupportedOperationException();
}

@Deprecated
public synchronized File getFile() {
throw new UnsupportedOperationException();
}
}
30 changes: 30 additions & 0 deletions FileTextStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileTextStorage implements Storage {
private final TextSource source;
private final File destination;

public FileTextStorage(final File destination, final TextSource source) {
this.destination = destination;
this.source = source;
}

public FileTextStorage(final File destination, final String content) {
this(destination, () -> content);
}

@Override
public void save() throws IOException {
final FileOutputStream o = new FileOutputStream(destination);
final String content = this.source.getContent();
try {
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
46 changes: 0 additions & 46 deletions Parser.java

This file was deleted.

45 changes: 45 additions & 0 deletions Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Program implements Runnable {
private final TextSource source;
private final Storage storage;

public Program(final TextSource source, final Storage storage) {
this.source = source;
this.storage = storage;
}

@Override
public void run() {
try {
this.storage.save();
final String result = source.getContent();
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
final File destination = new File("test.txt");
new Program(
new UnicodeFreeTextSource(
new FileTextSource(
destination
)
),
new FileTextStorage(
destination,
new UnicodeFreeTextSource(
new ScannerTextSource(
new Scanner(
System.in
)
)
)
)
).run();
}
}
15 changes: 15 additions & 0 deletions ScannerTextSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.io.IOException;
import java.util.Scanner;

public class ScannerTextSource implements TextSource {
private final Scanner source;

public ScannerTextSource(final Scanner source) {
this.source = source;
}

@Override
public String getContent() throws IOException {
return this.source.nextLine();
}
}
6 changes: 6 additions & 0 deletions Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.io.IOException;

public interface Storage {

void save() throws IOException;
}
6 changes: 6 additions & 0 deletions TextSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import java.io.IOException;

public interface TextSource {

String getContent() throws IOException;
}
22 changes: 22 additions & 0 deletions UnicodeFreeTextSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.io.IOException;

public final class UnicodeFreeTextSource implements TextSource {
private final TextSource origin;

public UnicodeFreeTextSource(final TextSource origin) {
this.origin = origin;
}

@Override
public String getContent() throws IOException {
final String source = origin.getContent();
String result = "";
for (final char character : source.toCharArray()) {
if (character > 0x80) {
continue;
}
result += character;
}
return result;
}
}