Make TextRenderer and HTMLRenderer thread-safe (or so I think ...)
This commit is contained in:
parent
8774324b65
commit
cf96949f8e
5 changed files with 292 additions and 294 deletions
|
@ -9,6 +9,8 @@ import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
|
|
||||||
public class HTMLFileRenderer implements Renderer<Path> {
|
public class HTMLFileRenderer implements Renderer<Path> {
|
||||||
|
@NonNull
|
||||||
|
private static final HTMLRenderer HTML_RENDERER = HTMLRenderer.newInstance();
|
||||||
@Setter
|
@Setter
|
||||||
private Path targetFile;
|
private Path targetFile;
|
||||||
|
|
||||||
|
@ -35,7 +37,7 @@ public class HTMLFileRenderer implements Renderer<Path> {
|
||||||
if (!this.isTargetFileDefinedAndWritable()) {
|
if (!this.isTargetFileDefinedAndWritable()) {
|
||||||
throw new IllegalArgumentException("Cannot write to target file. See previous log messages for details.");
|
throw new IllegalArgumentException("Cannot write to target file. See previous log messages for details.");
|
||||||
}
|
}
|
||||||
final String html = HTMLRenderer.newInstance().render(labyrinth);
|
final String html = HTML_RENDERER.render(labyrinth);
|
||||||
try {
|
try {
|
||||||
Files.writeString(this.targetFile, html, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
Files.writeString(this.targetFile, html, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package ch.fritteli.labyrinth;
|
||||||
import io.vavr.collection.HashSet;
|
import io.vavr.collection.HashSet;
|
||||||
import io.vavr.collection.Set;
|
import io.vavr.collection.Set;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
public class HTMLRenderer implements Renderer<String> {
|
public class HTMLRenderer implements Renderer<String> {
|
||||||
private static final String PREAMBLE = "<!DOCTYPE html><html lang=\"en\">" +
|
private static final String PREAMBLE = "<!DOCTYPE html><html lang=\"en\">" +
|
||||||
|
@ -33,11 +34,6 @@ public class HTMLRenderer implements Renderer<String> {
|
||||||
"<body>" +
|
"<body>" +
|
||||||
"<input type=\"checkbox\" onclick=\"toggleSolution()\">show solution</input>";
|
"<input type=\"checkbox\" onclick=\"toggleSolution()\">show solution</input>";
|
||||||
private static final String POSTAMBLE = "</body></html>";
|
private static final String POSTAMBLE = "</body></html>";
|
||||||
private Labyrinth labyrinth;
|
|
||||||
private int width;
|
|
||||||
private int height;
|
|
||||||
// row counter
|
|
||||||
private int y = 0;
|
|
||||||
|
|
||||||
private HTMLRenderer() {
|
private HTMLRenderer() {
|
||||||
}
|
}
|
||||||
|
@ -49,29 +45,32 @@ public class HTMLRenderer implements Renderer<String> {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public String render(@NonNull final Labyrinth labyrinth) {
|
public String render(@NonNull final Labyrinth labyrinth) {
|
||||||
this.labyrinth = labyrinth;
|
if (labyrinth.getWidth() == 0 || labyrinth.getHeight() == 0) {
|
||||||
this.width = labyrinth.getWidth();
|
|
||||||
this.height = labyrinth.getHeight();
|
|
||||||
if (this.width == 0 || this.height == 0) {
|
|
||||||
return PREAMBLE + POSTAMBLE;
|
return PREAMBLE + POSTAMBLE;
|
||||||
}
|
}
|
||||||
|
final Generator generator = new Generator(labyrinth);
|
||||||
final StringBuilder sb = new StringBuilder(PREAMBLE);
|
final StringBuilder sb = new StringBuilder(PREAMBLE);
|
||||||
sb.append("<table>");
|
sb.append("<table>");
|
||||||
while (this.hasNext()) {
|
while (generator.hasNext()) {
|
||||||
sb.append(this.next());
|
sb.append(generator.next());
|
||||||
}
|
}
|
||||||
sb.append("</table>");
|
sb.append("</table>");
|
||||||
sb.append(POSTAMBLE);
|
sb.append(POSTAMBLE);
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
private static class Generator {
|
||||||
|
private final Labyrinth labyrinth;
|
||||||
|
private int y = 0;
|
||||||
|
|
||||||
private boolean hasNext() {
|
private boolean hasNext() {
|
||||||
return this.y < this.height;
|
return this.y < this.labyrinth.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String next() {
|
private String next() {
|
||||||
StringBuilder sb = new StringBuilder("<tr>");
|
StringBuilder sb = new StringBuilder("<tr>");
|
||||||
for (int x = 0; x < this.width; x++) {
|
for (int x = 0; x < this.labyrinth.getWidth(); x++) {
|
||||||
final Tile currentTile = this.labyrinth.getTileAt(x, this.y);
|
final Tile currentTile = this.labyrinth.getTileAt(x, this.y);
|
||||||
sb.append("<td class=\"");
|
sb.append("<td class=\"");
|
||||||
sb.append(this.getClasses(currentTile).mkString(" "));
|
sb.append(this.getClasses(currentTile).mkString(" "));
|
||||||
|
@ -101,4 +100,5 @@ public class HTMLRenderer implements Renderer<String> {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ public class Main {
|
||||||
final Labyrinth labyrinth = new Labyrinth(width, height);
|
final Labyrinth labyrinth = new Labyrinth(width, height);
|
||||||
final TextRenderer textRenderer = TextRenderer.newInstance();
|
final TextRenderer textRenderer = TextRenderer.newInstance();
|
||||||
System.out.println(textRenderer.render(labyrinth));
|
System.out.println(textRenderer.render(labyrinth));
|
||||||
System.out.println(textRenderer.setRenderingSolution(true).render(labyrinth));
|
System.out.println(textRenderer.setRenderSolution(true).render(labyrinth));
|
||||||
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
final HTMLRenderer htmlRenderer = HTMLRenderer.newInstance();
|
||||||
System.out.println(htmlRenderer.render(labyrinth));
|
System.out.println(htmlRenderer.render(labyrinth));
|
||||||
System.out.println(HTMLFileRenderer.newInstance().render(labyrinth));
|
System.out.println(HTMLFileRenderer.newInstance().render(labyrinth));
|
||||||
|
|
|
@ -4,20 +4,12 @@ import lombok.AccessLevel;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.experimental.FieldDefaults;
|
import lombok.experimental.FieldDefaults;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
public class TextRenderer implements Renderer<String> {
|
public class TextRenderer implements Renderer<String> {
|
||||||
private Labyrinth labyrinth;
|
|
||||||
private int width;
|
|
||||||
private int height;
|
|
||||||
private boolean renderSolution;
|
private boolean renderSolution;
|
||||||
// column counter
|
|
||||||
private int x;
|
|
||||||
// row counter
|
|
||||||
private int y;
|
|
||||||
// line counter (top-, center- or bottom line of a row)
|
|
||||||
private int line;
|
|
||||||
|
|
||||||
private TextRenderer() {
|
private TextRenderer() {
|
||||||
this.renderSolution = false;
|
this.renderSolution = false;
|
||||||
|
@ -29,31 +21,35 @@ public class TextRenderer implements Renderer<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public TextRenderer setRenderingSolution(final boolean renderSolution) {
|
public TextRenderer setRenderSolution(final boolean renderSolution) {
|
||||||
this.renderSolution = renderSolution;
|
this.renderSolution = renderSolution;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public String render(@NonNull final Labyrinth labyrinth) {
|
public String render(@NonNull final Labyrinth labyrinth) {
|
||||||
this.labyrinth = labyrinth;
|
if (labyrinth.getWidth() == 0 || labyrinth.getHeight() == 0) {
|
||||||
this.width = labyrinth.getWidth();
|
|
||||||
this.height = labyrinth.getHeight();
|
|
||||||
if (this.width == 0 || this.height == 0) {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
this.x = 0;
|
final Generator generator = new Generator(labyrinth, this.renderSolution);
|
||||||
this.y = 0;
|
|
||||||
this.line = 0;
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
while (this.hasNext()) {
|
while (generator.hasNext()) {
|
||||||
sb.append(this.next());
|
sb.append(generator.next());
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
static class Generator {
|
||||||
|
@NonNull
|
||||||
|
private final Labyrinth labyrinth;
|
||||||
|
private final boolean renderSolution;
|
||||||
|
private int x = 0;
|
||||||
|
private int y = 0;
|
||||||
|
private int line = 0;
|
||||||
|
|
||||||
private boolean hasNext() {
|
private boolean hasNext() {
|
||||||
return this.y < this.height;
|
return this.y < this.labyrinth.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String next() {
|
private String next() {
|
||||||
|
@ -82,13 +78,13 @@ public class TextRenderer implements Renderer<String> {
|
||||||
private void prepareNextStep() {
|
private void prepareNextStep() {
|
||||||
// do some magic ...
|
// do some magic ...
|
||||||
this.x++;
|
this.x++;
|
||||||
if (this.x == this.width) {
|
if (this.x == this.labyrinth.getWidth()) {
|
||||||
// Reached the end of the row?
|
// Reached the end of the row?
|
||||||
// On to the next line then!
|
// On to the next line then!
|
||||||
this.x = 0;
|
this.x = 0;
|
||||||
this.line++;
|
this.line++;
|
||||||
}
|
}
|
||||||
if (this.line == 2 && this.y < this.height - 1) {
|
if (this.line == 2 && this.y < this.labyrinth.getHeight() - 1) {
|
||||||
// Finished rendering the center line, and more rows available?
|
// Finished rendering the center line, and more rows available?
|
||||||
// On to the next row then!
|
// On to the next row then!
|
||||||
this.line = 0;
|
this.line = 0;
|
||||||
|
@ -103,7 +99,7 @@ public class TextRenderer implements Renderer<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tile getTileOrNull(final int x, final int y) {
|
private Tile getTileOrNull(final int x, final int y) {
|
||||||
if (x < 0 || y < 0 || x >= this.width || y >= this.height) {
|
if (x < 0 || y < 0 || x >= this.labyrinth.getWidth() || y >= this.labyrinth.getHeight()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return this.labyrinth.getTileAt(x, y);
|
return this.labyrinth.getTileAt(x, y);
|
||||||
|
@ -137,7 +133,7 @@ public class TextRenderer implements Renderer<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = charDef1.toString() + charDef2;
|
result = charDef1.toString() + charDef2;
|
||||||
if (this.x == this.width - 1) {
|
if (this.x == this.labyrinth.getWidth() - 1) {
|
||||||
result += charDef3 + "\n";
|
result += charDef3 + "\n";
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -156,7 +152,7 @@ public class TextRenderer implements Renderer<String> {
|
||||||
|
|
||||||
result = charDef1 + (currentTile.isSolution() && this.renderSolution ? "x" : " ");
|
result = charDef1 + (currentTile.isSolution() && this.renderSolution ? "x" : " ");
|
||||||
|
|
||||||
if (this.x == this.width - 1) {
|
if (this.x == this.labyrinth.getWidth() - 1) {
|
||||||
result += charDef2 + "\n";
|
result += charDef2 + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,7 +182,7 @@ public class TextRenderer implements Renderer<String> {
|
||||||
|
|
||||||
result = charDef1.toString() + charDef2;
|
result = charDef1.toString() + charDef2;
|
||||||
|
|
||||||
if (this.x == this.width - 1) {
|
if (this.x == this.labyrinth.getWidth() - 1) {
|
||||||
result += charDef3;
|
result += charDef3;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
@ -255,7 +251,6 @@ public class TextRenderer implements Renderer<String> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CharDefinition horizontal() {
|
CharDefinition horizontal() {
|
||||||
return this.left().right();
|
return this.left().right();
|
||||||
}
|
}
|
||||||
|
@ -324,4 +319,5 @@ public class TextRenderer implements Renderer<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package ch.fritteli.labyrinth;
|
package ch.fritteli.labyrinth;
|
||||||
|
|
||||||
import ch.fritteli.labyrinth.TextRenderer.CharDefinition;
|
import ch.fritteli.labyrinth.TextRenderer.Generator.CharDefinition;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue