feefe1fcfff0e1ad41212c6ab3cbd35adc8baecf
[xudocci.git] / src / main / java / net / pterodactylus / irc / ConnectionBuilder.java
1 /*
2  * XdccDownloader - ConnectionBuilder.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.irc;
19
20 import javax.net.SocketFactory;
21 import javax.net.ssl.SSLSocketFactory;
22
23 import com.google.common.base.Optional;
24 import com.google.common.eventbus.EventBus;
25 import com.google.inject.Inject;
26
27 /**
28  * Builder for {@link Connection}s.
29  *
30  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31  */
32 public class ConnectionBuilder {
33
34         /** The event bus. */
35         private final EventBus eventBus;
36
37         /** The hostname to connect to. */
38         private Optional<String> hostname = Optional.absent();
39
40         /** The port number to connect to. */
41         private int port = 6666;
42
43         /** Whether to use an SSL connection. */
44         private boolean secure = false;
45
46         /**
47          * Creates a new connection builder.
48          *
49          * @param eventBus
50          *              The event bus
51          */
52         @Inject
53         public ConnectionBuilder(EventBus eventBus) {
54                 this.eventBus = eventBus;
55         }
56
57         /**
58          * Configures this builder to use the given hostname for the connection.
59          *
60          * @param hostname
61          *              The hostname of the IRC server
62          * @return This builder
63          */
64         public ConnectionBuilder connect(String hostname) {
65                 this.hostname = Optional.fromNullable(hostname);
66                 return this;
67         }
68
69         /**
70          * Configures this builder to use the given port number for the connection.
71          *
72          * @param port
73          *              The port number of the IRC server
74          * @return This builder
75          */
76         public ConnectionBuilder port(int port) {
77                 this.port = port;
78                 return this;
79         }
80
81         /**
82          * Configures this builder to use an SSL connection.
83          *
84          * @return This builder
85          */
86         public ConnectionBuilder secure() {
87                 this.secure = true;
88                 return this;
89         }
90
91         /**
92          * Builds the connection.
93          *
94          * @return The created connection
95          * @throws IllegalStateException
96          *              if the hostname has not been set
97          */
98         public Connection build() throws IllegalStateException {
99                 if (!hostname.isPresent()) {
100                         throw new IllegalStateException("hostname must not be empty");
101                 }
102
103                 SocketFactory socketFactory = secure ? SSLSocketFactory.getDefault() : SocketFactory.getDefault();
104                 return new Connection(eventBus, socketFactory, hostname.get(), port);
105         }
106
107 }