From 13d1441e43285dee4a49d07f9a89c033200575e8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 29 Nov 2019 10:56:31 +0100 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20PreferencesLoader=20wit?= =?utf8?q?h=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/core/PreferencesLoader.java | 82 ---------------------- .../pterodactylus/sone/core/PreferencesLoader.kt | 58 +++++++++++++++ 2 files changed, 58 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/net/pterodactylus/sone/core/PreferencesLoader.java create mode 100644 src/main/java/net/pterodactylus/sone/core/PreferencesLoader.kt 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] } + } + +} -- 2.7.4