2 * XdccDownloader - ConnectionBuilder.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.irc;
20 import javax.net.SocketFactory;
21 import javax.net.ssl.SSLSocketFactory;
23 import com.google.common.base.Optional;
24 import com.google.common.eventbus.EventBus;
25 import com.google.inject.Inject;
28 * Builder for {@link Connection}s.
30 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32 public class ConnectionBuilder {
35 private final EventBus eventBus;
37 /** The hostname to connect to. */
38 private Optional<String> hostname = Optional.absent();
40 /** The port number to connect to. */
41 private int port = 6666;
43 /** Whether to use an SSL connection. */
44 private boolean secure = false;
47 * Creates a new connection builder.
53 public ConnectionBuilder(EventBus eventBus) {
54 this.eventBus = eventBus;
58 * Configures this builder to use the given hostname for the connection.
61 * The hostname of the IRC server
62 * @return This builder
64 public ConnectionBuilder connect(String hostname) {
65 this.hostname = Optional.fromNullable(hostname);
70 * Configures this builder to use the given port number for the connection.
73 * The port number of the IRC server
74 * @return This builder
76 public ConnectionBuilder port(int port) {
82 * Configures this builder to use an SSL connection.
84 * @return This builder
86 public ConnectionBuilder secure() {
92 * Builds the connection.
94 * @return The created connection
95 * @throws IllegalStateException
96 * if the hostname has not been set
98 public Connection build() throws IllegalStateException {
99 if (!hostname.isPresent()) {
100 throw new IllegalStateException("hostname must not be empty");
103 SocketFactory socketFactory = secure ? SSLSocketFactory.getDefault() : SocketFactory.getDefault();
104 return new Connection(eventBus, socketFactory, hostname.get(), port);