Skip to content

Commit

Permalink
Added Cobol structure generator tool
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerknoche committed Apr 29, 2024
1 parent 4631bae commit 2820e4e
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package gutta.apievolution.tools.generator;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class CobolBenchmarkStructsGenerator {

public static void main(String[] arguments) throws IOException {
String mode = arguments[0];
int numberOfElements = Integer.parseInt(arguments[1]);

try (Writer outputWriter = new OutputStreamWriter(System.out)) {
GeneratorMode generatorMode = createGeneratorMode(mode, outputWriter);
generatorMode.generateCode(numberOfElements);
}
}

private static GeneratorMode createGeneratorMode(String mode, Writer outputWriter) {
String normalizedMode = mode.toLowerCase();

switch (normalizedMode) {
case "structure":
return new StructureGeneratorMode(outputWriter);

case "initializer":
return new InitializationGeneratorMode(outputWriter);

default:
throw new IllegalArgumentException("Unsupported mode '" + mode + "'.");
}
}

private static class StructureGeneratorMode extends GeneratorMode {

public StructureGeneratorMode(Writer outputWriter) {
super(outputWriter);
}

@Override
public void generateCode(int numberOfElements) {
this.writeLine(" * Test structure with " + numberOfElements + " elements");
this.writeLine(" 05 '*-'TEST-STRUCT-" + numberOfElements + ".");

for (int elementIndex = 1; elementIndex <= numberOfElements; elementIndex++) {
this.writeLine(" 10 '*-'INT-FIELD-" + elementIndex + "-FLAGS PIC 9 BINARY.");
this.writeLine(" 88 VALUE-ABSENT VALUE 0.");
this.writeLine(" 88 VALUE-PRESENT VALUE 1.");
this.writeLine(" 88 VALUE-UNREPRESENTABLE VALUE 2.");
this.writeLine(" 10 '*-'INT-FIELD-" + elementIndex + " PIC S9(9) BINARY.");
this.writeLine("");
}

for (int elementIndex = 1; elementIndex <= numberOfElements; elementIndex++) {
this.writeLine(" 10 '*-'STRING-FIELD-" + elementIndex + "-FLAGS PIC 9 BINARY.");
this.writeLine(" 88 VALUE-ABSENT VALUE 0.");
this.writeLine(" 88 VALUE-PRESENT VALUE 1.");
this.writeLine(" 88 VALUE-UNREPRESENTABLE VALUE 2.");
this.writeLine(" 10 '*-'STRING-FIELD-" + elementIndex + " PIC X(10).");
this.writeLine("");
}
}
}

private static class InitializationGeneratorMode extends GeneratorMode {

public InitializationGeneratorMode(Writer outputWriter) {
super(outputWriter);
}

@Override
public void generateCode(int numberOfElements) {
for (int elementIndex = 1; elementIndex <= numberOfElements; elementIndex++) {
String currentFieldName = "C" + numberOfElements + "-INT-FIELD-" + elementIndex;

this.writeLine(" SET VALUE-PRESENT IN " + currentFieldName + "-FLAGS");
this.writeLine(" TO TRUE");
this.writeLine(" MOVE " + elementIndex);
this.writeLine(" TO " + currentFieldName);
}

this.writeLine("");

for (int elementIndex = 1; elementIndex <= numberOfElements; elementIndex++) {
String currentFieldName = "C" + numberOfElements + "-STRING-FIELD-" + elementIndex;

this.writeLine(" SET VALUE-PRESENT IN " + currentFieldName + "-FLAGS");
this.writeLine(" TO TRUE");
this.writeLine(" MOVE '" + elementIndex + "'");
this.writeLine(" TO " + currentFieldName);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gutta.apievolution.tools.generator;

import java.io.IOException;
import java.io.Writer;

abstract class GeneratorMode {

protected final Writer outputWriter;

protected GeneratorMode(Writer outputWriter) {
this.outputWriter = outputWriter;
}

protected void write(String output) {
try {
this.outputWriter.write(output);
} catch (IOException e) {
throw new RuntimeException("Error writing output line.", e);
}
}

protected void writeLine(String line) {
this.write(line + "\n");
}

public abstract void generateCode(int numberOfElements);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gutta.apievolution.jmh.util;
package gutta.apievolution.tools.generator;

import java.io.IOException;
import java.io.OutputStreamWriter;
Expand Down Expand Up @@ -40,30 +40,6 @@ private static GeneratorMode createGeneratorMode(String mode, Writer outputWrite
}
}

private static abstract class GeneratorMode {

protected final Writer outputWriter;

protected GeneratorMode(Writer outputWriter) {
this.outputWriter = outputWriter;
}

protected void write(String output) {
try {
this.outputWriter.write(output);
} catch (IOException e) {
throw new RuntimeException("Error writing output line.", e);
}
}

protected void writeLine(String line) {
this.write(line + "\n");
}

public abstract void generateCode(int numberOfElements);

}

private static class RecordGeneratorMode extends GeneratorMode {

public RecordGeneratorMode(Writer outputWriter) {
Expand Down

0 comments on commit 2820e4e

Please sign in to comment.