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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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