Start outfitting core classes for Guice
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 May 2015 08:15:42 +0000 (10:15 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 9 May 2015 08:17:09 +0000 (10:17 +0200)
build.gradle
src/main/java/net/pterodactylus/rhynodge/engine/Engine.java
src/main/java/net/pterodactylus/rhynodge/engine/Starter.java
src/main/java/net/pterodactylus/rhynodge/states/StateManager.java
src/test/java/net/pterodactylus/rhynodge/states/StateManagerTest.java

index af795fc..d772187 100644 (file)
@@ -38,6 +38,7 @@ dependencies {
     compile group: "com.fasterxml.jackson.core", name: "jackson-core", version: "2.1.2"
     compile group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: "2.1.2"
     compile group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.1.2"
+    compile group: "com.google.inject", name: "guice", version: "4.0"
 
     testCompile group: "junit", name: "junit", version:"4.12"
     testCompile group: "org.hamcrest", name: "hamcrest-library", version:"1.3"
index 7103dc5..079350b 100644 (file)
@@ -28,6 +28,9 @@ import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
 import net.pterodactylus.rhynodge.Reaction;
 import net.pterodactylus.rhynodge.actions.EmailAction;
 import net.pterodactylus.rhynodge.states.StateManager;
@@ -37,6 +40,7 @@ import net.pterodactylus.rhynodge.states.StateManager;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class Engine {
 
        private final StateManager stateManager;
@@ -44,6 +48,7 @@ public class Engine {
        private final Map<String, Future<?>> scheduledFutures = new ConcurrentHashMap<>();
        private final EmailAction errorEmailAction;
 
+       @Inject
        public Engine(StateManager stateManager, EmailAction errorEmailAction) {
                this.stateManager = stateManager;
                this.errorEmailAction = errorEmailAction;
index 7ca75ca..d824eb8 100644 (file)
@@ -22,6 +22,7 @@ import java.io.IOException;
 import net.pterodactylus.rhynodge.actions.EmailAction;
 import net.pterodactylus.rhynodge.loader.ChainWatcher;
 import net.pterodactylus.rhynodge.states.StateManager;
+import net.pterodactylus.rhynodge.states.StateManager.Directory;
 import net.pterodactylus.util.envopt.Parser;
 
 /**
@@ -42,7 +43,7 @@ public class Starter {
                Options options = Parser.fromSystemEnvironment().parseEnvironment(Options::new);
 
                /* create the state manager. */
-               StateManager stateManager = new StateManager(options.stateDirectory);
+               StateManager stateManager = new StateManager(Directory.of(options.stateDirectory));
 
                /* create the engine. */
                Engine engine = new Engine(stateManager, createErrorEmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient));
index 3e08c29..bb8aff5 100644 (file)
@@ -24,6 +24,9 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Optional;
 
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
 import net.pterodactylus.rhynodge.State;
 
 import org.apache.log4j.Logger;
@@ -38,6 +41,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class StateManager {
 
        /** The logger. */
@@ -55,8 +59,9 @@ public class StateManager {
         * @param directory
         *            The directory to store states in
         */
-       public StateManager(String directory) {
-               this.directory = directory;
+       @Inject
+       public StateManager(Directory directory) {
+               this.directory = directory.getDirectory();
        }
 
        //
@@ -159,4 +164,22 @@ public class StateManager {
                return empty();
        }
 
+       public static class Directory {
+
+               private final String directory;
+
+               private Directory(String directory) {
+                       this.directory = directory;
+               }
+
+               public String getDirectory() {
+                       return directory;
+               }
+
+               public static Directory of(String directory) {
+                       return new Directory(directory);
+               }
+
+       }
+
 }
index 6541c11..0821b68 100644 (file)
@@ -12,6 +12,7 @@ import java.io.IOException;
 import java.util.Optional;
 
 import net.pterodactylus.rhynodge.State;
+import net.pterodactylus.rhynodge.states.StateManager.Directory;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.google.common.io.Files;
@@ -41,7 +42,7 @@ public class StateManagerTest {
        public StateManagerTest() throws IOException {
                tempFolder.create();
                statePath = tempFolder.newFolder();
-               stateManager = new StateManager(statePath.getPath());
+               stateManager = new StateManager(Directory.of(statePath.getPath()));
        }
 
        @Test