mirror of
https://github.com/thunderbird/thunderbird-android.git
synced 2024-09-20 04:02:14 +02:00
Extract Settings.upgrade()
to SettingsUpgradeHelper
This commit is contained in:
parent
2443f08ec7
commit
50eea08993
@ -303,7 +303,7 @@ class AccountSettingsDescriptions {
|
||||
}
|
||||
|
||||
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
|
||||
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
}
|
||||
|
||||
public static Map<String, String> convert(Map<String, Object> settings) {
|
||||
|
@ -58,7 +58,7 @@ class FolderSettingsDescriptions {
|
||||
}
|
||||
|
||||
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
|
||||
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
}
|
||||
|
||||
public static Map<String, String> convert(Map<String, Object> settings) {
|
||||
|
@ -316,7 +316,7 @@ class GeneralSettingsDescriptions {
|
||||
}
|
||||
|
||||
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
|
||||
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
}
|
||||
|
||||
public static Map<String, String> convert(Map<String, Object> settings) {
|
||||
|
@ -51,7 +51,7 @@ class IdentitySettingsDescriptions {
|
||||
}
|
||||
|
||||
public static Map<String, Object> upgrade(int version, Map<String, Object> validatedSettings) {
|
||||
return Settings.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
return SettingsUpgradeHelper.upgrade(version, UPGRADERS, SETTINGS, validatedSettings);
|
||||
}
|
||||
|
||||
public static Map<String, String> convert(Map<String, Object> settings) {
|
||||
|
@ -8,7 +8,7 @@ internal class ServerSettingsUpgrader(
|
||||
return server
|
||||
}
|
||||
|
||||
val upgradedSettings = Settings.upgrade(
|
||||
val upgradedSettings = SettingsUpgradeHelper.upgrade(
|
||||
contentVersion,
|
||||
serverSettingsDescriptions.upgraders,
|
||||
serverSettingsDescriptions.settings,
|
||||
|
@ -93,79 +93,6 @@ class Settings {
|
||||
return validatedSettings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upgrade settings using the settings structure and/or special upgrade code.
|
||||
*
|
||||
* @param version
|
||||
* The content version of the settings in {@code settings}.
|
||||
* @param upgraders
|
||||
* A map of {@link SettingsUpgrader}s for nontrivial settings upgrades.
|
||||
* @param settingsDescriptions
|
||||
* The structure describing the different settings, possibly containing multiple versions.
|
||||
* @param settings
|
||||
* The validated settings as returned by {@link Settings#validate(int, Map, Map, boolean)}.
|
||||
*
|
||||
* @return The upgraded settings.
|
||||
*/
|
||||
public static Map<String, Object> upgrade(int version, Map<Integer, SettingsUpgrader> upgraders,
|
||||
Map<String, TreeMap<Integer, SettingsDescription<?>>> settingsDescriptions, Map<String, Object> settings) {
|
||||
|
||||
Map<String, Object> upgradedSettings = new HashMap<>(settings);
|
||||
for (int toVersion = version + 1; toVersion <= VERSION; toVersion++) {
|
||||
if (upgraders.containsKey(toVersion)) {
|
||||
SettingsUpgrader upgrader = upgraders.get(toVersion);
|
||||
upgrader.upgrade(upgradedSettings);
|
||||
}
|
||||
|
||||
upgradeSettingsGeneric(settingsDescriptions, upgradedSettings, toVersion);
|
||||
}
|
||||
|
||||
return upgradedSettings;
|
||||
}
|
||||
|
||||
private static void upgradeSettingsGeneric(
|
||||
Map<String, TreeMap<Integer, SettingsDescription<?>>> settingsDescriptions,
|
||||
Map<String, Object> mutableSettings, int toVersion) {
|
||||
for (Entry<String, TreeMap<Integer, SettingsDescription<?>>> versions : settingsDescriptions.entrySet()) {
|
||||
String settingName = versions.getKey();
|
||||
TreeMap<Integer, SettingsDescription<?>> versionedSettingsDescriptions = versions.getValue();
|
||||
|
||||
boolean isNewlyAddedSetting = versionedSettingsDescriptions.firstKey() == toVersion;
|
||||
if (isNewlyAddedSetting) {
|
||||
boolean wasHandledByCustomUpgrader = mutableSettings.containsKey(settingName);
|
||||
if (wasHandledByCustomUpgrader) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SettingsDescription<?> settingDescription = versionedSettingsDescriptions.get(toVersion);
|
||||
if (settingDescription == null) {
|
||||
throw new AssertionError("First version of a setting must be non-null!");
|
||||
}
|
||||
upgradeSettingInsertDefault(mutableSettings, settingName, settingDescription);
|
||||
}
|
||||
|
||||
Integer highestVersion = versionedSettingsDescriptions.lastKey();
|
||||
boolean isRemovedSetting = highestVersion == toVersion &&
|
||||
versionedSettingsDescriptions.get(highestVersion) == null;
|
||||
|
||||
if (isRemovedSetting) {
|
||||
mutableSettings.remove(settingName);
|
||||
Timber.v("Removed setting \"%s\"", settingName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void upgradeSettingInsertDefault(Map<String, Object> mutableSettings,
|
||||
String settingName, SettingsDescription<T> settingDescription) {
|
||||
T defaultValue = settingDescription.getDefaultValue();
|
||||
mutableSettings.put(settingName, defaultValue);
|
||||
|
||||
if (K9.isDebugLoggingEnabled()) {
|
||||
String prettyValue = settingDescription.toPrettyString(defaultValue);
|
||||
Timber.v("Added new setting \"%s\" with default value \"%s\"", settingName, prettyValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert settings from the internal representation to the string representation used in the
|
||||
* preference storage.
|
||||
|
@ -0,0 +1,87 @@
|
||||
package com.fsck.k9.preferences;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.fsck.k9.K9;
|
||||
import com.fsck.k9.preferences.Settings.SettingsDescription;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
class SettingsUpgradeHelper {
|
||||
/**
|
||||
* Upgrade settings using the settings structure and/or special upgrade code.
|
||||
*
|
||||
* @param version
|
||||
* The content version of the settings in {@code settings}.
|
||||
* @param upgraders
|
||||
* A map of {@link SettingsUpgrader}s for nontrivial settings upgrades.
|
||||
* @param settingsDescriptions
|
||||
* The structure describing the different settings, possibly containing multiple versions.
|
||||
* @param settings
|
||||
* The validated settings as returned by {@link Settings#validate(int, Map, Map, boolean)}.
|
||||
*
|
||||
* @return The upgraded settings.
|
||||
*/
|
||||
public static Map<String, Object> upgrade(int version, Map<Integer, SettingsUpgrader> upgraders,
|
||||
Map<String, TreeMap<Integer, SettingsDescription<?>>> settingsDescriptions, Map<String, Object> settings) {
|
||||
|
||||
Map<String, Object> upgradedSettings = new HashMap<>(settings);
|
||||
for (int toVersion = version + 1; toVersion <= Settings.VERSION; toVersion++) {
|
||||
if (upgraders.containsKey(toVersion)) {
|
||||
SettingsUpgrader upgrader = upgraders.get(toVersion);
|
||||
upgrader.upgrade(upgradedSettings);
|
||||
}
|
||||
|
||||
upgradeSettingsGeneric(settingsDescriptions, upgradedSettings, toVersion);
|
||||
}
|
||||
|
||||
return upgradedSettings;
|
||||
}
|
||||
|
||||
private static void upgradeSettingsGeneric(
|
||||
Map<String, TreeMap<Integer, SettingsDescription<?>>> settingsDescriptions,
|
||||
Map<String, Object> mutableSettings, int toVersion) {
|
||||
for (Entry<String, TreeMap<Integer, SettingsDescription<?>>> versions : settingsDescriptions.entrySet()) {
|
||||
String settingName = versions.getKey();
|
||||
TreeMap<Integer, SettingsDescription<?>> versionedSettingsDescriptions = versions.getValue();
|
||||
|
||||
boolean isNewlyAddedSetting = versionedSettingsDescriptions.firstKey() == toVersion;
|
||||
if (isNewlyAddedSetting) {
|
||||
boolean wasHandledByCustomUpgrader = mutableSettings.containsKey(settingName);
|
||||
if (wasHandledByCustomUpgrader) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SettingsDescription<?> settingDescription = versionedSettingsDescriptions.get(toVersion);
|
||||
if (settingDescription == null) {
|
||||
throw new AssertionError("First version of a setting must be non-null!");
|
||||
}
|
||||
upgradeSettingInsertDefault(mutableSettings, settingName, settingDescription);
|
||||
}
|
||||
|
||||
Integer highestVersion = versionedSettingsDescriptions.lastKey();
|
||||
boolean isRemovedSetting = highestVersion == toVersion &&
|
||||
versionedSettingsDescriptions.get(highestVersion) == null;
|
||||
|
||||
if (isRemovedSetting) {
|
||||
mutableSettings.remove(settingName);
|
||||
Timber.v("Removed setting \"%s\"", settingName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void upgradeSettingInsertDefault(Map<String, Object> mutableSettings,
|
||||
String settingName, SettingsDescription<T> settingDescription) {
|
||||
T defaultValue = settingDescription.getDefaultValue();
|
||||
mutableSettings.put(settingName, defaultValue);
|
||||
|
||||
if (K9.isDebugLoggingEnabled()) {
|
||||
String prettyValue = settingDescription.toPrettyString(defaultValue);
|
||||
Timber.v("Added new setting \"%s\" with default value \"%s\"", settingName, prettyValue);
|
||||
}
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ import com.fsck.k9.preferences.Settings.StringSetting
|
||||
import java.util.TreeMap
|
||||
import kotlin.test.Test
|
||||
|
||||
class SettingsTest {
|
||||
class SettingsUpgradeHelperTest {
|
||||
@Test
|
||||
fun `upgrade() with new setting being added`() {
|
||||
val version = 1
|
||||
@ -29,7 +29,12 @@ class SettingsTest {
|
||||
"one" to false,
|
||||
)
|
||||
|
||||
val result = Settings.upgrade(version, upgraders, settingsDescriptions, settings)
|
||||
val result = SettingsUpgradeHelper.upgrade(
|
||||
version,
|
||||
upgraders,
|
||||
settingsDescriptions,
|
||||
settings,
|
||||
)
|
||||
|
||||
assertThat(result).isEqualTo(
|
||||
mapOf(
|
||||
@ -56,7 +61,12 @@ class SettingsTest {
|
||||
"one" to false,
|
||||
)
|
||||
|
||||
val result = Settings.upgrade(version, upgraders, settingsDescriptions, settings)
|
||||
val result = SettingsUpgradeHelper.upgrade(
|
||||
version,
|
||||
upgraders,
|
||||
settingsDescriptions,
|
||||
settings,
|
||||
)
|
||||
|
||||
assertThat(result).isEqualTo(
|
||||
mapOf(
|
||||
@ -87,7 +97,12 @@ class SettingsTest {
|
||||
"one" to false,
|
||||
)
|
||||
|
||||
val result = Settings.upgrade(version, upgraders, settingsDescriptions, settings)
|
||||
val result = SettingsUpgradeHelper.upgrade(
|
||||
version,
|
||||
upgraders,
|
||||
settingsDescriptions,
|
||||
settings,
|
||||
)
|
||||
|
||||
assertThat(result).isEqualTo(
|
||||
mapOf(
|
||||
@ -114,7 +129,12 @@ class SettingsTest {
|
||||
"setting" to false,
|
||||
)
|
||||
|
||||
val result = Settings.upgrade(version, upgraders, settingsDescriptions, settings)
|
||||
val result = SettingsUpgradeHelper.upgrade(
|
||||
version,
|
||||
upgraders,
|
||||
settingsDescriptions,
|
||||
settings,
|
||||
)
|
||||
|
||||
assertThat(result).isEqualTo(
|
||||
mapOf(
|
||||
@ -138,7 +158,12 @@ class SettingsTest {
|
||||
)
|
||||
|
||||
assertFailure {
|
||||
Settings.upgrade(version, upgraders, settingsDescriptions, settings)
|
||||
SettingsUpgradeHelper.upgrade(
|
||||
version,
|
||||
upgraders,
|
||||
settingsDescriptions,
|
||||
settings,
|
||||
)
|
||||
}.isInstanceOf<AssertionError>()
|
||||
.hasMessage("First version of a setting must be non-null!")
|
||||
}
|
Loading…
Reference in New Issue
Block a user