java code: two or three classes with potential code duplication #7259
only:step
This commit is contained in:
parent
1ce5470b05
commit
364fb97dbf
|
@ -39,22 +39,7 @@ public class FieldIterator {
|
|||
}
|
||||
|
||||
public void loop() {
|
||||
FieldsStrategy empty = new FieldsStrategy() {
|
||||
@Override
|
||||
int writeOneField(FieldIterator iterator, String prefix, int tsPosition) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
//
|
||||
// empty.writeFields(fields, "", 0);
|
||||
|
||||
FieldIterator iterator = new FieldIterator(fields);
|
||||
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
start(i);
|
||||
empty.writeOneField(iterator, "", 0);
|
||||
end();
|
||||
}
|
||||
FieldsStrategy.VOID.loopIterator(fields, "", 0, this);
|
||||
}
|
||||
|
||||
public void end() {
|
||||
|
@ -62,4 +47,8 @@ public class FieldIterator {
|
|||
prev = cf;
|
||||
bitState.incrementBitIndex(cf, next);
|
||||
}
|
||||
|
||||
public int adjustSize(int tsPosition) {
|
||||
return tsPosition + cf.getSize(next);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,13 @@ import java.util.List;
|
|||
* @see PerFieldWithStructuresIterator#strategy
|
||||
*/
|
||||
public abstract class FieldsStrategy {
|
||||
static final FieldsStrategy VOID = new FieldsStrategy() {
|
||||
@Override
|
||||
int writeOneField(FieldIterator iterator, String prefix, int tsPosition) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
public int run(ReaderState state, ConfigStructure structure, int structureStartingTsPosition) {
|
||||
if (state.isStackEmpty()) {
|
||||
return writeFields(structure.getTsFields(), "", structureStartingTsPosition);
|
||||
|
@ -20,6 +27,10 @@ public abstract class FieldsStrategy {
|
|||
|
||||
protected int writeFields(List<ConfigField> fields, String prefix, int tsPosition) {
|
||||
FieldIterator iterator = new FieldIterator(fields);
|
||||
return loopIterator(fields, prefix, tsPosition, iterator);
|
||||
}
|
||||
|
||||
protected int loopIterator(List<ConfigField> fields, String prefix, int tsPosition, FieldIterator iterator) {
|
||||
for (int i = 0; i < fields.size(); i++) {
|
||||
iterator.start(i);
|
||||
tsPosition = writeOneField(iterator, prefix, tsPosition);
|
||||
|
|
|
@ -79,8 +79,7 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
writeJavaFieldName(nameWithPrefix, tsPosition);
|
||||
content.append("FieldType.BIT, " + bitIndex + ")" + terminateField());
|
||||
}
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
return iterator.adjustSize(tsPosition);
|
||||
}
|
||||
|
||||
if (TypesHelper.isFloat(configField.getTypeName())) {
|
||||
|
@ -115,9 +114,7 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
}
|
||||
}
|
||||
|
||||
tsPosition += configField.getSize(next);
|
||||
|
||||
return tsPosition;
|
||||
return iterator.adjustSize(tsPosition);
|
||||
}
|
||||
};
|
||||
fieldsStrategy.run(state, structure, 0);
|
||||
|
|
|
@ -62,7 +62,7 @@ public class JavaSensorsConsumer implements ConfigurationConsumer {
|
|||
|
||||
|
||||
}
|
||||
tsPosition += configField.getSize(next);
|
||||
tsPosition = iterator.adjustSize(tsPosition);
|
||||
// this value would be consumed by LiveDataProcessor
|
||||
// todo: too many variables that's fragile shall we move tsPosition to iterator state?
|
||||
structSize = tsPosition - sensorTsPosition;
|
||||
|
|
|
@ -61,8 +61,7 @@ public class TsOutput {
|
|||
|
||||
// note that we need to handle account for unused bits size below!
|
||||
if (configField.getName().startsWith(ConfigStructureImpl.ALIGNMENT_FILL_AT)) {
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
return it.adjustSize(tsPosition);
|
||||
}
|
||||
|
||||
if (configField.isDirective() && configField.getComment() != null) {
|
||||
|
@ -94,8 +93,7 @@ public class TsOutput {
|
|||
tsHeader.append(EOL);
|
||||
}
|
||||
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
return it.adjustSize(tsPosition);
|
||||
}
|
||||
|
||||
if (configField.getState().getTsCustomLine().containsKey(configField.getTypeName())) {
|
||||
|
|
Loading…
Reference in New Issue