Add basic IRC framework.
[xudocci.git] / src / main / java / net / pterodactylus / irc / ConnectionBuilder.java
diff --git a/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java b/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java
new file mode 100644 (file)
index 0000000..feefe1f
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * XdccDownloader - ConnectionBuilder.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.irc;
+
+import javax.net.SocketFactory;
+import javax.net.ssl.SSLSocketFactory;
+
+import com.google.common.base.Optional;
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Inject;
+
+/**
+ * Builder for {@link Connection}s.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class ConnectionBuilder {
+
+       /** The event bus. */
+       private final EventBus eventBus;
+
+       /** The hostname to connect to. */
+       private Optional<String> hostname = Optional.absent();
+
+       /** The port number to connect to. */
+       private int port = 6666;
+
+       /** Whether to use an SSL connection. */
+       private boolean secure = false;
+
+       /**
+        * Creates a new connection builder.
+        *
+        * @param eventBus
+        *              The event bus
+        */
+       @Inject
+       public ConnectionBuilder(EventBus eventBus) {
+               this.eventBus = eventBus;
+       }
+
+       /**
+        * Configures this builder to use the given hostname for the connection.
+        *
+        * @param hostname
+        *              The hostname of the IRC server
+        * @return This builder
+        */
+       public ConnectionBuilder connect(String hostname) {
+               this.hostname = Optional.fromNullable(hostname);
+               return this;
+       }
+
+       /**
+        * Configures this builder to use the given port number for the connection.
+        *
+        * @param port
+        *              The port number of the IRC server
+        * @return This builder
+        */
+       public ConnectionBuilder port(int port) {
+               this.port = port;
+               return this;
+       }
+
+       /**
+        * Configures this builder to use an SSL connection.
+        *
+        * @return This builder
+        */
+       public ConnectionBuilder secure() {
+               this.secure = true;
+               return this;
+       }
+
+       /**
+        * Builds the connection.
+        *
+        * @return The created connection
+        * @throws IllegalStateException
+        *              if the hostname has not been set
+        */
+       public Connection build() throws IllegalStateException {
+               if (!hostname.isPresent()) {
+                       throw new IllegalStateException("hostname must not be empty");
+               }
+
+               SocketFactory socketFactory = secure ? SSLSocketFactory.getDefault() : SocketFactory.getDefault();
+               return new Connection(eventBus, socketFactory, hostname.get(), port);
+       }
+
+}