Last active
July 30, 2026 12:05
-
-
Save yegor256/6335539 to your computer and use it in GitHub Desktop.
quiz.java
This file contains hidden or 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
| /** | |
| * Please review the class below and suggest improvements. How would | |
| * you refactor this class if it would be in a real-life project? | |
| * There are many problems here, both high-level design mistakes, | |
| * and low-level implementation bugs. We're interested to see high-level | |
| * problems first, since they are most critical. The more mistakes | |
| * you can spot, the better programmer you are. | |
| */ | |
| /** | |
| * This class is thread safe. | |
| */ | |
| public class Parser { | |
| private File file; | |
| public synchronized void setFile(File f) { | |
| file = f; | |
| } | |
| public synchronized File getFile() { | |
| return file; | |
| } | |
| public String getContent() throws IOException { | |
| InputStream i = new FileInputStream(file); | |
| String output = ""; | |
| int data; | |
| while ((data = i.read()) > 0) { | |
| output += (char) data; | |
| } | |
| return output; | |
| } | |
| public String getContentWithoutUnicode() throws IOException { | |
| InputStream i = new FileInputStream(file); | |
| String output = ""; | |
| int data; | |
| while ((data = i.read()) > 0) { | |
| if (data < 0x80) { | |
| output += (char) data; | |
| } | |
| } | |
| return output; | |
| } | |
| public void saveContent(String content) throws IOException { | |
| OutputStream o = new FileOutputStream(file); | |
| for (int i = 0; i < content.length(); i += 1) { | |
| o.write(content.charAt(i)); | |
| } | |
| } | |
| } |
pysoftware
commented
Nov 18, 2021
I prefer to redesign this peace of code just like that
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.UnaryOperator;
interface Content {
String asString() throws IOException;
void replaceWith(final String s) throws IOException;
}
final class ContentOf implements Content {
private final File file;
public ContentOf(final File f) {
this.file = f;
}
@Override
public synchronized String asString() throws IOException {
try (final var f = new FileInputStream(this.file)) {
return new String(f.readAllBytes(), StandardCharsets.UTF_8);
}
}
@Override
public synchronized void replaceWith(final String s) throws IOException {
try (final var f = new FileOutputStream(this.file)) {
f.write(s.getBytes(StandardCharsets.UTF_8));
}
}
}
final class AsciiOnly implements Content {
private final Content origin;
private final UnaryOperator<String> filter;
public AsciiOnly(final Content c) {
this(
c,
s -> s
.codePoints()
.filter(el -> el < 0x80)
.collect(
StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append
)
.toString()
);
}
public AsciiOnly(final Content c, final UnaryOperator<String> f) {
this.origin = c;
this.filter = f;
}
@Override
public synchronized String asString() throws IOException {
return this.filter.apply(this.origin.asString());
}
@Override
public synchronized void replaceWith(final String s) throws IOException {
this.origin.replaceWith(this.filter.apply(s));
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment