Another round of code suggestions.

This commit is contained in:
Rob Greene
2025-10-18 17:20:47 -07:00
parent 40b525ae08
commit 824f03f855
20 changed files with 35 additions and 37 deletions
@@ -33,7 +33,7 @@ public class Configuration {
private int maxLineLength = 255;
private PrintStream debugStream = new PrintStream(new OutputStream() {
@Override
public void write(int b) throws IOException {
public void write(int b) {
// Do nothing
}
});
@@ -22,8 +22,8 @@ import io.github.applecommander.bastools.api.utils.Converters;
public abstract class Directive {
private final String directiveName;
protected Configuration config;
protected OutputStream outputStream;
protected final Configuration config;
protected final OutputStream outputStream;
private final List<Token> paramTokens = new ArrayList<>();
private final Map<String,Expression> parameters = new TreeMap<>(String::compareToIgnoreCase);
private final Set<String> parameterNames;
@@ -1,6 +1,7 @@
package io.github.applecommander.bastools.api;
import java.io.OutputStream;
import java.io.Serial;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.TreeMap;
@@ -14,7 +15,8 @@ public class Directives {
private static final Map<String,Class<? extends Directive>> DIRECTIVES =
new TreeMap<String,Class<? extends Directive>>(String.CASE_INSENSITIVE_ORDER) {
private static final long serialVersionUID = -8111460701487331592L;
@Serial
private static final long serialVersionUID = -8111460701487331592L;
{
put(EmbeddedBinaryDirective.NAME, EmbeddedBinaryDirective.class);
@@ -34,13 +34,13 @@ public class TokenReader {
private final StreamTokenizer tokenizer;
/** A handy method to generate a list of Tokens from a file name. */
public static Queue<Token> tokenize(String filename) throws FileNotFoundException, IOException {
public static Queue<Token> tokenize(String filename) throws IOException {
try (FileReader fileReader = new FileReader(filename)) {
return tokenize(fileReader);
}
}
/** A handy method to generate a list of Tokens from a file. */
public static Queue<Token> tokenize(File file) throws FileNotFoundException, IOException {
public static Queue<Token> tokenize(File file) throws IOException {
try (FileReader fileReader = new FileReader(file)) {
return tokenize(fileReader);
}
@@ -30,12 +30,12 @@ public class AsmBuilder {
return this;
}
/** Generate a "LDA #value" in the output stream. */
public AsmBuilder lda(int value) throws IOException {
public AsmBuilder lda(int value) {
builder.add(state -> internalLDA(state, value));
return this;
}
/** Generate a "STA address" in the output stream. */
public AsmBuilder sta(int address) throws IOException {
public AsmBuilder sta(int address) {
builder.add(state -> internalSTA(state, address));
return this;
}
@@ -76,7 +76,7 @@ public class EmbeddedShapeTable extends Directive {
// We need to terminate a binary embedded line with some mechanism of skipping the binary content.
Optional<Line> nextLineOpt = line.nextLine();
nextLineOpt.ifPresent(nextLine -> basic.GOTO(nextLine.lineNumber));
if (!nextLineOpt.isPresent()) basic.RETURN();
if (nextLineOpt.isEmpty()) basic.RETURN();
// End line and inject binary content
basic.endLine().set(shapeTableStart);
@@ -22,7 +22,7 @@ import io.github.applecommander.bastools.api.visitors.VariableCollectorVisitor;
*/
public class ExtractConstantValues extends BaseVisitor {
/** These trigger the start of a replacement range. Note the special logic for assignments. */
public static List<ApplesoftKeyword> TARGET_STARTS = Arrays.asList(
public static final List<ApplesoftKeyword> TARGET_STARTS = Arrays.asList(
ApplesoftKeyword.FOR, ApplesoftKeyword.CALL, ApplesoftKeyword.PLOT, ApplesoftKeyword.HLIN,
ApplesoftKeyword.VLIN, ApplesoftKeyword.HCOLOR, ApplesoftKeyword.HPLOT, ApplesoftKeyword.DRAW,
ApplesoftKeyword.XDRAW, ApplesoftKeyword.HTAB, ApplesoftKeyword.SCALE, ApplesoftKeyword.COLOR,
@@ -30,7 +30,7 @@ public class ExtractConstantValues extends BaseVisitor {
ApplesoftKeyword.LET, ApplesoftKeyword.IF, ApplesoftKeyword.ON, ApplesoftKeyword.WAIT,
ApplesoftKeyword.POKE);
/** These trigger the end of a replacement range. End of statement is always an end. */
public static List<ApplesoftKeyword> TARGET_ENDS = Arrays.asList(
public static final List<ApplesoftKeyword> TARGET_ENDS = Arrays.asList(
ApplesoftKeyword.GOTO, ApplesoftKeyword.GOSUB, ApplesoftKeyword.THEN);
// Map keyed by value (Double isn't a good key, using a String of the number) and pointing to replacement variable name
@@ -82,7 +82,7 @@ public class ShapeTable {
List<byte[]> data = this.shapes.stream()
.map(Shape::toVector)
.map(VectorShape::toBytes)
.collect(Collectors.toList());
.toList();
// Build offset table
int offset = 2 + 2*data.size();
for (byte[] d : data) {
@@ -98,7 +98,7 @@ public class ShapeTable {
public void write(File file) throws IOException {
Objects.requireNonNull(file);
try (OutputStream outputStream = new FileOutputStream(file)) {
write(file);
write(outputStream);
}
}
public void write(Path path) throws IOException {
@@ -28,14 +28,14 @@ public class SourceShapeExporter implements ShapeExporter {
}
@Override
public void export(Shape shape, OutputStream outputStream) throws IOException {
public void export(Shape shape, OutputStream outputStream) {
PrintWriter pw = new PrintWriter(outputStream);
formatFunction.accept(shape, pw);
pw.flush();
}
@Override
public void export(ShapeTable shapeTable, OutputStream outputStream) throws IOException {
public void export(ShapeTable shapeTable, OutputStream outputStream) {
PrintWriter pw = new PrintWriter(outputStream);
shapeTable.shapes.stream()
.filter(this::displayThisShape)
@@ -52,7 +52,7 @@ public class SourceShapeExporter implements ShapeExporter {
pw.printf(".bitmap\n");
ByteArrayOutputStream os = new ByteArrayOutputStream();
textExporter.export(shape, new PaddedOutputStream(os, " "));
pw.print(new String(os.toByteArray()));
pw.print(os.toString());
pw.printf("\n");
} catch (IOException ex) {
throw new UncheckedIOException(ex);
@@ -48,7 +48,7 @@ public class TextShapeExporter implements ShapeExporter {
List<BitmapShape> blist = shapeTable.shapes.stream()
.filter(this::displayThisShape)
.map(Shape::toBitmap)
.collect(Collectors.toList());
.toList();
int width = blist.stream().mapToInt(BitmapShape::getWidth).max().getAsInt();
int height = blist.stream().mapToInt(BitmapShape::getHeight).max().getAsInt();
@@ -52,7 +52,7 @@ public class Converters {
stream = IntStream.concat(stream, IntStream.rangeClosed(low, high));
} else {
stream = IntStream.concat(stream,
Arrays.asList(range.split(",")).stream().mapToInt(Integer::parseInt));
Arrays.stream(range.split(",")).mapToInt(Integer::parseInt));
}
}
return stream;
@@ -11,8 +11,6 @@ import io.github.applecommander.bastools.api.model.Program;
import io.github.applecommander.bastools.api.model.Token;
import io.github.applecommander.bastools.api.utils.TokenBuilder;
import javax.xml.crypto.Data;
import static org.junit.Assert.assertEquals;
public class ParserTest {
@@ -1,7 +1,6 @@
package io.github.applecommander.bastools.api.shapes;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -132,7 +131,7 @@ public class ShapeGeneratorTest {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ShapeExporter exp = ShapeExporter.text().asciiTextBorder().build();
exp.export(shape, outputStream);
String actual = new String(outputStream.toByteArray());
String actual = outputStream.toString();
assertEquals(expected, actual);
}
}
@@ -1,7 +1,6 @@
package io.github.applecommander.bastools.api.shapes;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -54,6 +53,6 @@ public class ShapeWriteAndReadTests {
public String format(ShapeTable st) throws IOException {
ByteArrayOutputStream text = new ByteArrayOutputStream();
textExporter.export(st, text);
return new String(text.toByteArray());
return text.toString();
}
}
@@ -86,7 +86,7 @@ public class ShapesTest {
}
@Test
public void testToVectorFromBitmap() throws IOException {
public void testToVectorFromBitmap() {
BitmapShape bitmapShape = plotStandardBoxShape();
VectorShape vectorShape = bitmapShape.toVector();
@@ -108,7 +108,7 @@ public class ShapesTest {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ShapeExporter exp = ShapeExporter.text().noBorder().build();
exp.export(st.shapes.get(0), outputStream);
String actual = new String(outputStream.toByteArray());
String actual = outputStream.toString();
assertEquals(expected, actual);
}
@@ -128,7 +128,7 @@ public class ShapesTest {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ShapeExporter exp = ShapeExporter.text().asciiTextBorder().build();
exp.export(st.shapes.get(0), outputStream);
String actual = new String(outputStream.toByteArray());
String actual = outputStream.toString();
assertEquals(expected, actual);
}
@@ -152,7 +152,7 @@ public class ShapesTest {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ShapeExporter exp = ShapeExporter.text().maxWidth(12).noBorder().build();
exp.export(st, outputStream);
String actual = new String(outputStream.toByteArray());
String actual = outputStream.toString();
assertEquals(expected, actual);
}
@@ -178,7 +178,7 @@ public class ShapesTest {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ShapeExporter exp = ShapeExporter.text().maxWidth(12).asciiTextBorder().build();
exp.export(st, outputStream);
String actual = new String(outputStream.toByteArray());
String actual = outputStream.toString();
assertEquals(expected, actual);
}