From ded32f30c3d792a911c80420db40d8ef81ba10e0 Mon Sep 17 00:00:00 2001 From: kifir23917 Date: Tue, 4 Mar 2025 16:17:05 +0300 Subject: [PATCH] only:refactoring:extract part of functionality into `DefaultEnumIniFieldMigrator` class #7476 --- .../DefaultEnumIniFieldMigrator.java | 64 +++++++++++++++++++ .../migration/DefaultIniFieldMigrator.java | 54 +--------------- 2 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultEnumIniFieldMigrator.java diff --git a/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultEnumIniFieldMigrator.java b/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultEnumIniFieldMigrator.java new file mode 100644 index 0000000000..4f28eb18c6 --- /dev/null +++ b/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultEnumIniFieldMigrator.java @@ -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 tryMigrateValue( + final EnumIniField prevField, + final EnumIniField newField, + final String prevValue, + final UpdateOperationCallbacks callbacks + ) { + Optional 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; + } +} diff --git a/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultIniFieldMigrator.java b/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultIniFieldMigrator.java index 4d47b4f994..7ed1c56dcc 100644 --- a/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultIniFieldMigrator.java +++ b/java_console/ui/src/main/java/com/rusefi/maintenance/migration/DefaultIniFieldMigrator.java @@ -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 tryMigrateEnumValue( - final EnumIniField prevField, - final EnumIniField newField, - final String prevValue, - final UpdateOperationCallbacks callbacks - ) { - Optional 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,