only:refactoring:extract part of functionality into `DefaultEnumIniFieldMigrator` class #7476

This commit is contained in:
kifir23917 2025-03-04 16:17:05 +03:00 committed by rusefillc
parent ce085bfe3b
commit ded32f30c3
2 changed files with 65 additions and 53 deletions

View File

@ -0,0 +1,64 @@
package com.rusefi.maintenance.migration;
import com.opensr5.ini.field.EnumIniField;
import com.rusefi.io.UpdateOperationCallbacks;
import java.util.Objects;
import java.util.Optional;
import static javax.management.ObjectName.quote;
public enum DefaultEnumIniFieldMigrator {
INSTANCE;
public Optional<String> tryMigrateValue(
final EnumIniField prevField,
final EnumIniField newField,
final String prevValue,
final UpdateOperationCallbacks callbacks
) {
Optional<String> result = Optional.empty();
if (!Objects.equals(prevField.getType(), newField.getType())) {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because type is updated: `%s` -> `%s`",
prevField.getName(),
prevField.getType(),
newField.getType()
));
} else if (!Objects.equals(prevField.getBitSize0(), newField.getBitSize0())) {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because bit size 0 is updated: `%s` -> `%s`",
prevField.getName(),
prevField.getBitSize0(),
newField.getBitSize0()
));
} else {
final EnumIniField.EnumKeyValueMap newFieldKeyValues = newField.getEnums();
if (newFieldKeyValues.indexOf(prevValue) == -1) {
if (newFieldKeyValues.isBitField()) {
final int prevValueOrdinal = prevField.getEnums().indexOf(prevValue);
final String migratedValue = newFieldKeyValues.get(prevValueOrdinal);
if (migratedValue != null) {
result = Optional.of(quote(migratedValue));
} else {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because of internal error: " +
"new enum doesn't value with ordinal %d",
prevField.getName(),
prevValueOrdinal
));
}
} else {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because value `%s` has disappeared",
prevField.getName(),
prevValue
));
}
} else {
result = Optional.of(prevValue);
}
}
return result;
}
}

View File

@ -11,7 +11,6 @@ import java.util.Optional;
import java.util.Set;
import static com.devexperts.logging.Logging.getLogging;
import static javax.management.ObjectName.quote;
public enum DefaultIniFieldMigrator implements IniFieldMigrator {
INSTANCE;
@ -54,7 +53,7 @@ public enum DefaultIniFieldMigrator implements IniFieldMigrator {
}
} else if (prevField instanceof EnumIniField) {
if (newField instanceof EnumIniField) {
result = tryMigrateEnumValue(
result = DefaultEnumIniFieldMigrator.INSTANCE.tryMigrateValue(
(EnumIniField) prevField,
(EnumIniField) newField,
prevValue,
@ -117,57 +116,6 @@ public enum DefaultIniFieldMigrator implements IniFieldMigrator {
return result;
}
private static Optional<String> tryMigrateEnumValue(
final EnumIniField prevField,
final EnumIniField newField,
final String prevValue,
final UpdateOperationCallbacks callbacks
) {
Optional<String> result = Optional.empty();
if (!Objects.equals(prevField.getType(), newField.getType())) {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because type is updated: `%s` -> `%s`",
prevField.getName(),
prevField.getType(),
newField.getType()
));
} else if (!Objects.equals(prevField.getBitSize0(), newField.getBitSize0())) {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because bit size 0 is updated: `%s` -> `%s`",
prevField.getName(),
prevField.getBitSize0(),
newField.getBitSize0()
));
} else {
final EnumIniField.EnumKeyValueMap newFieldKeyValues = newField.getEnums();
if (newFieldKeyValues.indexOf(prevValue) == -1) {
if (newFieldKeyValues.isBitField()) {
final int prevValueOrdinal = prevField.getEnums().indexOf(prevValue);
final String migratedValue = newFieldKeyValues.get(prevValueOrdinal);
if (migratedValue != null) {
result = Optional.of(quote(migratedValue));
} else {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because of internal error: " +
"new enum doesn't value with ordinal %d",
prevField.getName(),
prevValueOrdinal
));
}
} else {
callbacks.logLine(String.format(
"WARNING! Field `%s` cannot be migrated because value `%s` has disappeared",
prevField.getName(),
prevValue
));
}
} else {
result = Optional.of(prevValue);
}
}
return result;
}
private static boolean canArrayValueBeMigrated(
final ArrayIniField prevField,
final ArrayIniField newField,