reducing code duplication
This commit is contained in:
parent
16a6112002
commit
782a06d211
|
@ -70,7 +70,7 @@ public class UsagesReader {
|
|||
state.setDefinitionInputFile(folder + File.separator + name + ".txt");
|
||||
state.withC_Defines = withCDefines;
|
||||
|
||||
JavaSensorsConsumer javaSensorsConsumer = new JavaSensorsConsumer(state, usagesReader.sensorTsPosition);
|
||||
JavaSensorsConsumer javaSensorsConsumer = new JavaSensorsConsumer(usagesReader.sensorTsPosition);
|
||||
state.addDestination(javaSensorsConsumer);
|
||||
|
||||
state.addPrepend(prepend);
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package com.rusefi.output;
|
||||
|
||||
import com.rusefi.ConfigField;
|
||||
import com.rusefi.ReaderState;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class FieldsStrategy {
|
||||
public void run(ReaderState state, ConfigStructure structure) throws IOException {
|
||||
if (state.stack.isEmpty()) {
|
||||
writeJavaFields(structure.tsFields, "", 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected int writeJavaFields(List<ConfigField> tsFields, String prefix, int tsPosition) throws IOException {
|
||||
FieldIterator iterator = new FieldIterator(tsFields);
|
||||
for (int i = 0; i < tsFields.size(); i++) {
|
||||
iterator.start(i);
|
||||
tsPosition = writeOneField(iterator.cf, prefix, tsPosition, iterator.next,
|
||||
iterator.bitState.get(),
|
||||
iterator.getPrev());
|
||||
|
||||
iterator.end();
|
||||
}
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
abstract int writeOneField(ConfigField configField, String prefix, int tsPosition, ConfigField next, int bitIndex, ConfigField prev) throws IOException;
|
||||
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package com.rusefi.output;
|
||||
|
||||
import com.rusefi.ConfigDefinition;
|
||||
import com.rusefi.ReaderState;
|
||||
import com.rusefi.ToolUtil;
|
||||
import com.rusefi.util.LazyFile;
|
||||
|
|
|
@ -35,74 +35,6 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
+ tsPosition + ", ");
|
||||
}
|
||||
|
||||
private int writeJavaFields(List<ConfigField> tsFields, String prefix, int tsPosition) throws IOException {
|
||||
FieldIterator iterator = new FieldIterator(tsFields);
|
||||
for (int i = 0; i < tsFields.size(); i++) {
|
||||
iterator.start(i);
|
||||
tsPosition = writeOneField(iterator.cf, prefix, tsPosition, iterator.next,
|
||||
iterator.bitState.get(),
|
||||
iterator.getPrev());
|
||||
|
||||
iterator.end();
|
||||
}
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
private int writeOneField(ConfigField configField, String prefix, int tsPosition, ConfigField next, int bitIndex, ConfigField prev) throws IOException {
|
||||
if (configField.isDirective())
|
||||
return tsPosition;
|
||||
// skip duplicate names which happens in case of conditional compilation
|
||||
if (configField.getName().equals(prev.getName())) {
|
||||
return tsPosition;
|
||||
}
|
||||
ConfigStructure cs = configField.getState().structures.get(configField.getType());
|
||||
if (cs != null) {
|
||||
String extraPrefix = cs.withPrefix ? configField.getName() + "_" : "";
|
||||
return writeJavaFields(cs.tsFields, prefix + extraPrefix, tsPosition);
|
||||
}
|
||||
|
||||
String nameWithPrefix = prefix + configField.getName();
|
||||
|
||||
if (configField.isBit()) {
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, 1);
|
||||
javaFieldsWriter.append("FieldType.BIT, " + bitIndex + ");" + EOL);
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
if (TypesHelper.isFloat(configField.getType())) {
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, configField.autoscaleSpecNumber());
|
||||
javaFieldsWriter.write("FieldType.FLOAT);" + EOL);
|
||||
} else {
|
||||
String enumOptions = state.variableRegistry.get(configField.getType() + VariableRegistry.ENUM_SUFFIX);
|
||||
|
||||
if (enumOptions != null && !javaEnums.contains(configField.getType())) {
|
||||
javaEnums.add(configField.getType());
|
||||
javaFieldsWriter.write("\tpublic static final String[] " + configField.getType() + " = {" + enumOptions + "};" + EOL);
|
||||
}
|
||||
|
||||
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, configField.autoscaleSpecNumber());
|
||||
if (isStringField(configField)) {
|
||||
String custom = state.tsCustomLine.get(configField.getType());
|
||||
String[] tokens = custom.split(",");
|
||||
String stringSize = tokens[3].trim();
|
||||
javaFieldsWriter.write(stringSize + ", FieldType.STRING");
|
||||
} else {
|
||||
javaFieldsWriter.write(getJavaType(configField.getElementSize()));
|
||||
}
|
||||
if (enumOptions != null) {
|
||||
javaFieldsWriter.write(", " + configField.getType());
|
||||
}
|
||||
javaFieldsWriter.write(")" + ".setScale(" + configField.autoscaleSpecNumber() + ")" +
|
||||
";" + EOL);
|
||||
}
|
||||
|
||||
tsPosition += configField.getSize(next);
|
||||
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
public static String getJavaType(int elementSize) {
|
||||
if (elementSize == 1) {
|
||||
return ("FieldType.INT8");
|
||||
|
@ -119,8 +51,62 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
}
|
||||
|
||||
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
|
||||
if (state.stack.isEmpty()) {
|
||||
writeJavaFields(structure.tsFields, "", 0);
|
||||
}
|
||||
FieldsStrategy fieldsStrategy = new FieldsStrategy() {
|
||||
protected int writeOneField(ConfigField configField, String prefix, int tsPosition, ConfigField next, int bitIndex, ConfigField prev) throws IOException {
|
||||
if (configField.isDirective())
|
||||
return tsPosition;
|
||||
// skip duplicate names which happens in case of conditional compilation
|
||||
if (configField.getName().equals(prev.getName())) {
|
||||
return tsPosition;
|
||||
}
|
||||
ConfigStructure cs = configField.getState().structures.get(configField.getType());
|
||||
if (cs != null) {
|
||||
String extraPrefix = cs.withPrefix ? configField.getName() + "_" : "";
|
||||
return writeJavaFields(cs.tsFields, prefix + extraPrefix, tsPosition);
|
||||
}
|
||||
|
||||
String nameWithPrefix = prefix + configField.getName();
|
||||
|
||||
if (configField.isBit()) {
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, 1);
|
||||
javaFieldsWriter.append("FieldType.BIT, " + bitIndex + ");" + EOL);
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
if (TypesHelper.isFloat(configField.getType())) {
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, configField.autoscaleSpecNumber());
|
||||
javaFieldsWriter.write("FieldType.FLOAT);" + EOL);
|
||||
} else {
|
||||
String enumOptions = state.variableRegistry.get(configField.getType() + VariableRegistry.ENUM_SUFFIX);
|
||||
|
||||
if (enumOptions != null && !javaEnums.contains(configField.getType())) {
|
||||
javaEnums.add(configField.getType());
|
||||
javaFieldsWriter.write("\tpublic static final String[] " + configField.getType() + " = {" + enumOptions + "};" + EOL);
|
||||
}
|
||||
|
||||
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, configField.autoscaleSpecNumber());
|
||||
if (isStringField(configField)) {
|
||||
String custom = state.tsCustomLine.get(configField.getType());
|
||||
String[] tokens = custom.split(",");
|
||||
String stringSize = tokens[3].trim();
|
||||
javaFieldsWriter.write(stringSize + ", FieldType.STRING");
|
||||
} else {
|
||||
javaFieldsWriter.write(getJavaType(configField.getElementSize()));
|
||||
}
|
||||
if (enumOptions != null) {
|
||||
javaFieldsWriter.write(", " + configField.getType());
|
||||
}
|
||||
javaFieldsWriter.write(")" + ".setScale(" + configField.autoscaleSpecNumber() + ")" +
|
||||
";" + EOL);
|
||||
}
|
||||
|
||||
tsPosition += configField.getSize(next);
|
||||
|
||||
return tsPosition;
|
||||
}
|
||||
};
|
||||
fieldsStrategy.run(state, structure);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,17 +4,13 @@ import com.rusefi.ConfigField;
|
|||
import com.rusefi.ReaderState;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public class JavaSensorsConsumer implements ConfigurationConsumer {
|
||||
private final ReaderState state;
|
||||
public int sensorTsPosition;
|
||||
|
||||
private final StringBuilder sb = new StringBuilder();
|
||||
|
||||
public JavaSensorsConsumer(ReaderState state, int sensorTsPosition) {
|
||||
|
||||
this.state = state;
|
||||
public JavaSensorsConsumer(int sensorTsPosition) {
|
||||
this.sensorTsPosition = sensorTsPosition;
|
||||
}
|
||||
|
||||
|
@ -28,59 +24,46 @@ public class JavaSensorsConsumer implements ConfigurationConsumer {
|
|||
|
||||
@Override
|
||||
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
|
||||
if (state.stack.isEmpty()) {
|
||||
sensorTsPosition = writeJavaFields(structure.tsFields, "", sensorTsPosition);
|
||||
}
|
||||
FieldsStrategy fieldsStrategy = new FieldsStrategy() {
|
||||
public int writeOneField(ConfigField configField, String prefix, int tsPosition, ConfigField next, int i, ConfigField prev) {
|
||||
if (configField.isBit()) {
|
||||
// do not support bits for now but need to account for size
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
sb.append(configField.getName()).append("(");
|
||||
|
||||
String string = readerState.variableRegistry.applyVariables(configField.getComment());
|
||||
if (string == null || string.isEmpty()) {
|
||||
string = quote(configField.getName());
|
||||
} else if (string.charAt(0) != '"') {
|
||||
// huh? weird conditional quoting?
|
||||
string = quote(string);
|
||||
}
|
||||
|
||||
sb.append(string).append(", ");
|
||||
|
||||
sb.append("SensorCategory.SENSOR_INPUTS, ");
|
||||
sb.append(JavaFieldsConsumer.getJavaType(configField.getElementSize())).append(", ");
|
||||
sb.append(tsPosition).append(", ");
|
||||
sb.append(configField.autoscaleSpecNumber()).append(", "); // scale
|
||||
sb.append(configField.getMin()).append(", ");
|
||||
sb.append(configField.getMax()).append(", ");
|
||||
|
||||
sb.append(quote(configField.getUnits())).append(")");
|
||||
|
||||
|
||||
sb.append(",");
|
||||
sb.append("\n");
|
||||
|
||||
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
};
|
||||
fieldsStrategy.run(readerState, structure);
|
||||
}
|
||||
|
||||
private int writeJavaFields(List<ConfigField> tsFields, String prefix, int tsPosition) {
|
||||
FieldIterator iterator = new FieldIterator(tsFields);
|
||||
for (int i = 0; i < tsFields.size(); i++) {
|
||||
iterator.start(i);
|
||||
tsPosition = writeOneField(iterator.cf, prefix, tsPosition, iterator.next,
|
||||
iterator.bitState.get(),
|
||||
iterator.getPrev());
|
||||
|
||||
iterator.end();
|
||||
}
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
private int writeOneField(ConfigField configField, String prefix, int tsPosition, ConfigField next, int i, ConfigField prev) {
|
||||
if (configField.isBit()) {
|
||||
// do not support bits for now but need to account for size
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
sb.append(configField.getName()).append("(");
|
||||
|
||||
String string = state.variableRegistry.applyVariables(configField.getComment());
|
||||
if (string == null || string.isEmpty()) {
|
||||
string = quote(configField.getName());
|
||||
} else if (string.charAt(0) != '"') {
|
||||
// huh? weird conditional quoting?
|
||||
string = quote(string);
|
||||
}
|
||||
|
||||
sb.append(string).append(", ");
|
||||
|
||||
sb.append("SensorCategory.SENSOR_INPUTS, ");
|
||||
sb.append(JavaFieldsConsumer.getJavaType(configField.getElementSize())).append(", ");
|
||||
sb.append(tsPosition).append(", ");
|
||||
sb.append(configField.autoscaleSpecNumber()).append(", "); // scale
|
||||
sb.append(configField.getMin()).append(", ");
|
||||
sb.append(configField.getMax()).append(", ");
|
||||
|
||||
sb.append(quote(configField.getUnits())).append(")");
|
||||
|
||||
|
||||
sb.append(",");
|
||||
sb.append("\n");
|
||||
|
||||
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
private static String quote(String string) {
|
||||
return "\"" + string + "\"";
|
||||
|
|
|
@ -28,7 +28,7 @@ public class JavaSensorsConsumerTest {
|
|||
"\tuint8_t vehicleSpeedKph\n" +
|
||||
"\tint8_t autoscale internalMcuTemperature;mcu;\"deg C\",1, 0, 0, 0, 0\n" +
|
||||
"end_struct\n";
|
||||
JavaSensorsConsumer javaSensorsConsumer = new JavaSensorsConsumer(state, 0);
|
||||
JavaSensorsConsumer javaSensorsConsumer = new JavaSensorsConsumer(0);
|
||||
state.readBufferedReader(outputChannels, javaSensorsConsumer);
|
||||
|
||||
assertEquals("RPMValue(\"hello\", SensorCategory.SENSOR_INPUTS, FieldType.INT16, 4, 1.0, 0.0, 8000.0, \"RPM\"),\n" +
|
||||
|
|
Loading…
Reference in New Issue