Skip to content

Instantly share code, notes, and snippets.

@pysoftware
Forked from yegor256/quiz.java
Last active November 18, 2021 20:47
Show Gist options
  • Save pysoftware/8fef355890eca482052142536c5f7884 to your computer and use it in GitHub Desktop.
Save pysoftware/8fef355890eca482052142536c5f7884 to your computer and use it in GitHub Desktop.
quiz.java
/**
* Made this class fully public for next extend cases
* For now it's unclear is this need to split FileContent class to different ones
*
* F.e. ParsedFile could have two methods returning SimpleContent and UnicodeFreeContent
* And each *Content class could have unique logic in it
*
* ParsedFile is thread safe because of immutability
*/
class FileContent {
private final String raw;
public FileContent(String rawContent) {
this.raw = rawContent;
}
public String text() {
return this.raw;
}
}
class ParsedFile {
private final InputStream fileInputStream;
public ParsedFile(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
public FileContent content() {
String content = "bytes converted to string";
return new FileContent(content);
}
public FileContent contentUnicodeFree() {
String content = "bytes converted to string without unicode";
return new FileContent(content);
}
}
class DefaultParsedFileUsage {
public static void main(String[] args) {
File file = new File("path");
try(InputStream is = new FileInputStream(file)) {
ParsedFile parsedFile = new ParsedFile(is);
FileContent fileContent = parsedFile.content();
String fileText = fileContent.text();
// next code
} catch (IOException ioException) {
// logging exception
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment