refactoring

This commit is contained in:
rusefillc 2023-01-06 10:52:09 -05:00
parent 4708b4080b
commit ac1da4856f
11 changed files with 75 additions and 18 deletions

View File

@ -0,0 +1,41 @@
package com.rusefi;
import com.rusefi.output.ConfigurationConsumer;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public interface IReaderState {
void setWithC_Defines(boolean withC_Defines);
void doJob() throws IOException;
String getHeader();
void setDefinitionInputFile(String definitionInputFile);
void addCHeaderDestination(String cHeader);
void addPrepend(String fileName);
void addDestination(ConfigurationConsumer... consumers);
VariableRegistry getVariableRegistry();
Map<String, String> getTsCustomLine();
void setHeaderMessage(String headerMessage);
String getTsFileOutputName();
void setTsFileOutputName(String tsFileOutputName);
String getDefinitionInputFile();
List<String> getPrependFiles();
boolean isDestinationsEmpty();
boolean isStackEmpty();
}

View File

@ -23,10 +23,7 @@ import static com.rusefi.output.JavaSensorsConsumer.quote;
* Andrey Belomutskiy, (c) 2013-2020 * Andrey Belomutskiy, (c) 2013-2020
* 12/19/18 * 12/19/18
*/ */
public class ReaderState { public class ReaderState implements IReaderState {
// used to update other files
private List<String> inputFiles = new ArrayList<>();
private static final Logging log = getLogging(ReaderState.class); private static final Logging log = getLogging(ReaderState.class);
public static final String BIT = "bit"; public static final String BIT = "bit";
@ -34,6 +31,8 @@ public class ReaderState {
private static final String END_STRUCT = "end_struct"; private static final String END_STRUCT = "end_struct";
private static final String STRUCT_NO_PREFIX = "struct_no_prefix "; private static final String STRUCT_NO_PREFIX = "struct_no_prefix ";
private static final String STRUCT = "struct "; private static final String STRUCT = "struct ";
// used to update other files
private final List<String> inputFiles = new ArrayList<>();
private final Stack<ConfigStructure> stack = new Stack<>(); private final Stack<ConfigStructure> stack = new Stack<>();
private final Map<String, Integer> tsCustomSize = new HashMap<>(); private final Map<String, Integer> tsCustomSize = new HashMap<>();
private final Map<String, String> tsCustomLine = new HashMap<>(); private final Map<String, String> tsCustomLine = new HashMap<>();
@ -49,6 +48,7 @@ public class ReaderState {
private final EnumsReader enumsReader = new EnumsReader(); private final EnumsReader enumsReader = new EnumsReader();
private final VariableRegistry variableRegistry = new VariableRegistry(); private final VariableRegistry variableRegistry = new VariableRegistry();
@Override
public void setWithC_Defines(boolean withC_Defines) { public void setWithC_Defines(boolean withC_Defines) {
this.withC_Defines = withC_Defines; this.withC_Defines = withC_Defines;
} }
@ -90,6 +90,7 @@ public class ReaderState {
structure.addBitField(bitField); structure.addBitField(bitField);
} }
@Override
public void doJob() throws IOException { public void doJob() throws IOException {
for (String prependFile : prependFiles) for (String prependFile : prependFiles)
variableRegistry.readPrependValues(prependFile); variableRegistry.readPrependValues(prependFile);
@ -338,18 +339,21 @@ public class ReaderState {
return quote(string); return quote(string);
} }
@Override
public String getHeader() { public String getHeader() {
if (headerMessage == null) if (headerMessage == null)
throw new NullPointerException("No header message yet"); throw new NullPointerException("No header message yet");
return headerMessage; return headerMessage;
} }
@Override
public void setDefinitionInputFile(String definitionInputFile) { public void setDefinitionInputFile(String definitionInputFile) {
this.definitionInputFile = definitionInputFile; this.definitionInputFile = definitionInputFile;
headerMessage = ToolUtil.getGeneratedAutomaticallyTag() + definitionInputFile + " " + new Date(); headerMessage = ToolUtil.getGeneratedAutomaticallyTag() + definitionInputFile + " " + new Date();
inputFiles.add(definitionInputFile); inputFiles.add(definitionInputFile);
} }
@Override
public void addCHeaderDestination(String cHeader) { public void addCHeaderDestination(String cHeader) {
destinations.add(new CHeaderConsumer(this, cHeader, withC_Defines)); destinations.add(new CHeaderConsumer(this, cHeader, withC_Defines));
} }
@ -358,6 +362,7 @@ public class ReaderState {
destinations.add(new FileJavaFieldsConsumer(this, fileName, 0)); destinations.add(new FileJavaFieldsConsumer(this, fileName, 0));
} }
@Override
public void addPrepend(String fileName) { public void addPrepend(String fileName) {
if (fileName == null || fileName.isEmpty()) { if (fileName == null || fileName.isEmpty()) {
// see LiveDataProcessor use-case with dynamic prepend usage // see LiveDataProcessor use-case with dynamic prepend usage
@ -367,6 +372,7 @@ public class ReaderState {
inputFiles.add(fileName); inputFiles.add(fileName);
} }
@Override
public void addDestination(ConfigurationConsumer... consumers) { public void addDestination(ConfigurationConsumer... consumers) {
destinations.addAll(Arrays.asList(consumers)); destinations.addAll(Arrays.asList(consumers));
} }
@ -375,14 +381,11 @@ public class ReaderState {
inputFiles.add(fileName); inputFiles.add(fileName);
} }
@Override
public VariableRegistry getVariableRegistry() { public VariableRegistry getVariableRegistry() {
return variableRegistry; return variableRegistry;
} }
public Stack<ConfigStructure> getStack() {
return stack;
}
public Map<String, Integer> getTsCustomSize() { public Map<String, Integer> getTsCustomSize() {
return tsCustomSize; return tsCustomSize;
} }
@ -391,31 +394,43 @@ public class ReaderState {
return structures; return structures;
} }
@Override
public Map<String, String> getTsCustomLine() { public Map<String, String> getTsCustomLine() {
return tsCustomLine; return tsCustomLine;
} }
@Override
public void setHeaderMessage(String headerMessage) { public void setHeaderMessage(String headerMessage) {
this.headerMessage = headerMessage; this.headerMessage = headerMessage;
} }
@Override
public String getTsFileOutputName() { public String getTsFileOutputName() {
return tsFileOutputName; return tsFileOutputName;
} }
@Override
public void setTsFileOutputName(String tsFileOutputName) { public void setTsFileOutputName(String tsFileOutputName) {
this.tsFileOutputName = tsFileOutputName; this.tsFileOutputName = tsFileOutputName;
} }
@Override
public String getDefinitionInputFile() { public String getDefinitionInputFile() {
return definitionInputFile; return definitionInputFile;
} }
@Override
public List<String> getPrependFiles() { public List<String> getPrependFiles() {
return prependFiles; return prependFiles;
} }
@Override
public boolean isDestinationsEmpty() { public boolean isDestinationsEmpty() {
return destinations.isEmpty(); return destinations.isEmpty();
} }
@Override
public boolean isStackEmpty() {
return stack.isEmpty();
}
} }

View File

@ -33,7 +33,7 @@ public class DataLogConsumer implements ConfigurationConsumer {
@Override @Override
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException { public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
if (readerState.getStack().isEmpty()) { if (readerState.isStackEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(readerState, structure.tsFields, "", PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(readerState, structure.tsFields, "",
(configField, prefix, prefix2) -> handle(prefix, prefix2)); (configField, prefix, prefix2) -> handle(prefix, prefix2));
iterator.loop(); iterator.loop();

View File

@ -1,13 +1,14 @@
package com.rusefi.output; package com.rusefi.output;
import com.rusefi.ConfigField; import com.rusefi.ConfigField;
import com.rusefi.IReaderState;
import com.rusefi.ReaderState; import com.rusefi.ReaderState;
import java.util.List; import java.util.List;
public abstract class FieldsStrategy { public abstract class FieldsStrategy {
public int run(ReaderState state, ConfigStructure structure, int sensorTsPosition) { public int run(IReaderState state, ConfigStructure structure, int sensorTsPosition) {
if (state.getStack().isEmpty()) { if (state.isStackEmpty()) {
return writeFields(structure.tsFields, "", sensorTsPosition); return writeFields(structure.tsFields, "", sensorTsPosition);
} }
return sensorTsPosition; return sensorTsPosition;

View File

@ -19,7 +19,7 @@ public class GaugeConsumer implements ConfigurationConsumer {
@Override @Override
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException { public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
if (readerState.getStack().isEmpty()) { if (readerState.isStackEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(readerState, structure.tsFields, "", PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(readerState, structure.tsFields, "",
(state, configField, prefix) -> handle(configField, prefix)); (state, configField, prefix) -> handle(configField, prefix));
iterator.loop(); iterator.loop();

View File

@ -61,7 +61,7 @@ public class GetConfigValueConsumer implements ConfigurationConsumer {
@Override @Override
public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException { public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException {
if (state.getStack().isEmpty()) { if (state.isStackEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "", PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "",
this::processConfig, "."); this::processConfig, ".");
iterator.loop(); iterator.loop();

View File

@ -34,7 +34,7 @@ public class GetOutputValueConsumer implements ConfigurationConsumer {
@Override @Override
public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException { public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException {
if (state.getStack().isEmpty()) { if (state.isStackEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "", PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "",
this::processOutput, "."); this::processOutput, ".");
iterator.loop(); iterator.loop();

View File

@ -32,7 +32,7 @@ public class OutputsSectionConsumer implements ConfigurationConsumer {
sensorTsPosition = tsOutput.run(readerState, structure, sensorTsPosition); sensorTsPosition = tsOutput.run(readerState, structure, sensorTsPosition);
if (readerState.getStack().isEmpty()) { if (readerState.isStackEmpty()) {
if (tsOutputsSectionFileName != null) { if (tsOutputsSectionFileName != null) {
FileWriter fos = new FileWriter(tsOutputsSectionFileName); FileWriter fos = new FileWriter(tsOutputsSectionFileName);
fos.write(tsOutput.getContent()); fos.write(tsOutput.getContent());

View File

@ -13,7 +13,7 @@ public class SdCardFieldsContent {
public String home = "engine->outputChannels"; public String home = "engine->outputChannels";
public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException { public void handleEndStruct(ReaderState state, ConfigStructure structure) throws IOException {
if (state.getStack().isEmpty()) { if (state.isStackEmpty()) {
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "", PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "",
(configField, prefix, prefix2) -> processOutput(prefix, prefix2), "."); (configField, prefix, prefix2) -> processOutput(prefix, prefix2), ".");
iterator.loop(); iterator.loop();

View File

@ -147,7 +147,7 @@ public class TSProjectConsumer implements ConfigurationConsumer {
state.getVariableRegistry().register(structure.name + "_size", structure.getTotalSize()); state.getVariableRegistry().register(structure.name + "_size", structure.getTotalSize());
totalTsSize = tsOutput.run(readerState, structure, 0); totalTsSize = tsOutput.run(readerState, structure, 0);
if (state.getStack().isEmpty()) { if (state.isStackEmpty()) {
state.getVariableRegistry().register("TOTAL_CONFIG_SIZE", totalTsSize); state.getVariableRegistry().register("TOTAL_CONFIG_SIZE", totalTsSize);
} }
} }

View File

@ -138,7 +138,7 @@ public class TsOutput {
}; };
sensorTsPosition = strategy.run(state, structure, sensorTsPosition); sensorTsPosition = strategy.run(state, structure, sensorTsPosition);
if (state.getStack().isEmpty()) { if (state.isStackEmpty()) {
tsHeader.append("; total TS size = " + sensorTsPosition + EOL); tsHeader.append("; total TS size = " + sensorTsPosition + EOL);
} }
return sensorTsPosition; return sensorTsPosition;