🎨 Replace PreferencesLoader with Kotlin version
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 29 Nov 2019 09:56:31 +0000 (10:56 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 29 Nov 2019 17:27:04 +0000 (18:27 +0100)
src/main/java/net/pterodactylus/sone/core/PreferencesLoader.java [deleted file]
src/main/java/net/pterodactylus/sone/core/PreferencesLoader.kt [new file with mode: 0644]

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 (file)
index 5fa33e4..0000000
+++ /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 (file)
index 0000000..32c35cb
--- /dev/null
@@ -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] }
+       }
+
+}