From: David ‘Bombe’ Roden Date: Fri, 29 Nov 2019 09:56:31 +0000 (+0100) Subject: 🎨 Replace PreferencesLoader with Kotlin version X-Git-Tag: v81^2~15^2~12 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=13d1441e43285dee4a49d07f9a89c033200575e8;p=Sone.git 🎨 Replace PreferencesLoader with Kotlin version --- diff --git a/src/main/java/net/pterodactylus/sone/core/PreferencesLoader.java b/src/main/java/net/pterodactylus/sone/core/PreferencesLoader.java deleted file mode 100644 index 5fa33e4..0000000 --- a/src/main/java/net/pterodactylus/sone/core/PreferencesLoader.java +++ /dev/null @@ -1,82 +0,0 @@ -package net.pterodactylus.sone.core; - -import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; -import net.pterodactylus.util.config.Configuration; -import net.pterodactylus.util.config.ConfigurationException; - -/** - * Loads preferences stored in a {@link Configuration} into a {@link - * Preferences} object. - */ -public class PreferencesLoader { - - private final Preferences preferences; - - public PreferencesLoader(Preferences preferences) { - this.preferences = preferences; - } - - public void loadFrom(Configuration configuration) { - loadInsertionDelay(configuration); - loadPostsPerPage(configuration); - loadImagesPerPage(configuration); - loadCharactersPerPost(configuration); - loadPostCutOffLength(configuration); - loadRequireFullAccess(configuration); - loadFcpInterfaceActive(configuration); - loadFcpFullAccessRequired(configuration); - } - - private void loadInsertionDelay(Configuration configuration) { - preferences.setNewInsertionDelay(configuration.getIntValue( - "Option/InsertionDelay").getValue(null)); - } - - private void loadPostsPerPage(Configuration configuration) { - preferences.setNewPostsPerPage( - configuration.getIntValue("Option/PostsPerPage") - .getValue(null)); - } - - private void loadImagesPerPage(Configuration configuration) { - preferences.setNewImagesPerPage( - configuration.getIntValue("Option/ImagesPerPage") - .getValue(null)); - } - - private void loadCharactersPerPost(Configuration configuration) { - preferences.setNewCharactersPerPost( - configuration.getIntValue("Option/CharactersPerPost") - .getValue(null)); - } - - private void loadPostCutOffLength(Configuration configuration) { - try { - preferences.setNewPostCutOffLength( - configuration.getIntValue("Option/PostCutOffLength") - .getValue(null)); - } catch (IllegalArgumentException iae1) { - /* previous versions allowed -1, ignore and use default. */ - } - } - - private void loadRequireFullAccess(Configuration configuration) { - preferences.setNewRequireFullAccess( - configuration.getBooleanValue("Option/RequireFullAccess") - .getValue(null)); - } - - private void loadFcpInterfaceActive(Configuration configuration) { - preferences.setNewFcpInterfaceActive(configuration.getBooleanValue( - "Option/ActivateFcpInterface").getValue(null)); - } - - private void loadFcpFullAccessRequired(Configuration configuration) { - Integer fullAccessRequiredInteger = configuration - .getIntValue("Option/FcpFullAccessRequired").getValue(null); - preferences.setNewFcpFullAccessRequired( - (fullAccessRequiredInteger == null) ? null : - FullAccessRequired.values()[fullAccessRequiredInteger]); - } - -} diff --git a/src/main/java/net/pterodactylus/sone/core/PreferencesLoader.kt b/src/main/java/net/pterodactylus/sone/core/PreferencesLoader.kt new file mode 100644 index 0000000..32c35cb --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/PreferencesLoader.kt @@ -0,0 +1,58 @@ +package net.pterodactylus.sone.core + +import net.pterodactylus.sone.fcp.FcpInterface.* +import net.pterodactylus.util.config.* + +/** + * Loads preferences stored in a [Configuration] into a [Preferences] object. + */ +class PreferencesLoader(private val preferences: Preferences) { + + fun loadFrom(configuration: Configuration) { + loadInsertionDelay(configuration) + loadPostsPerPage(configuration) + loadImagesPerPage(configuration) + loadCharactersPerPost(configuration) + loadPostCutOffLength(configuration) + loadRequireFullAccess(configuration) + loadFcpInterfaceActive(configuration) + loadFcpFullAccessRequired(configuration) + } + + private fun loadInsertionDelay(configuration: Configuration) { + preferences.newInsertionDelay = configuration.getIntValue("Option/InsertionDelay").getValue(null) + } + + private fun loadPostsPerPage(configuration: Configuration) { + preferences.newPostsPerPage = configuration.getIntValue("Option/PostsPerPage").getValue(null) + } + + private fun loadImagesPerPage(configuration: Configuration) { + preferences.newImagesPerPage = configuration.getIntValue("Option/ImagesPerPage").getValue(null) + } + + private fun loadCharactersPerPost(configuration: Configuration) { + preferences.newCharactersPerPost = configuration.getIntValue("Option/CharactersPerPost").getValue(null) + } + + private fun loadPostCutOffLength(configuration: Configuration) { + try { + preferences.newPostCutOffLength = configuration.getIntValue("Option/PostCutOffLength").getValue(null) + } catch (iae1: IllegalArgumentException) { /* previous versions allowed -1, ignore and use default. */ + } + } + + private fun loadRequireFullAccess(configuration: Configuration) { + preferences.newRequireFullAccess = configuration.getBooleanValue("Option/RequireFullAccess").getValue(null) + } + + private fun loadFcpInterfaceActive(configuration: Configuration) { + preferences.newFcpInterfaceActive = configuration.getBooleanValue("Option/ActivateFcpInterface").getValue(null) + } + + private fun loadFcpFullAccessRequired(configuration: Configuration) { + val fullAccessRequiredInteger = configuration.getIntValue("Option/FcpFullAccessRequired").getValue(null) + preferences.newFcpFullAccessRequired = fullAccessRequiredInteger?.let { FullAccessRequired.values()[it] } + } + +}