2 * XdccDownloader - Network.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.xdcc.data;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.Collection;
24 import com.google.common.collect.Sets;
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class Network {
33 /** The name of the network. */
34 private final String name;
36 /** The servers of this network. */
37 private final Collection<Server> servers = Sets.newHashSet();
40 * Creates a new network with the given name.
43 * The name of the network
44 * @throws NullPointerException
45 * if {@code name} is {@code null}
47 private Network(String name) throws NullPointerException {
48 this.name = checkNotNull(name, "name must not be null");
56 * Returns the name of this network.
58 * @return The name of this network
60 public String name() {
65 * Returns the servers of this network.
67 * @return The servers of this network
69 public Collection<Server> servers() {
77 private void addServer(Server server) {
86 public boolean equals(Object object) {
87 if (!(object instanceof Network)) {
90 Network network = (Network) object;
91 return name().equals(network.name());
95 public int hashCode() {
96 return name().hashCode();
100 public String toString() {
101 return String.format("%s(%d servers)", name(), servers().size());
109 * Returns a network builder.
112 * The name of the network to build
113 * @return A network builder
115 public static NetworkBuilder builder(String name) {
116 return new NetworkBuilder(name);
119 /** {@link Network} builder. */
120 public static class NetworkBuilder {
122 /** The network being built. */
123 private final Network network;
126 * Creates a new network builder.
129 * The name of the network being built
131 private NetworkBuilder(String name) {
132 network = new Network(name);
136 * Returns a server builder that will add the created server to this network.
138 * @return A server builder
140 public ServerBuilder addServer() {
141 return new ServerBuilder(this);
145 * Adds the given server to this builder.
149 * @return This network builder
151 private NetworkBuilder addServer(Server server) {
152 network.addServer(server);
157 * Returns the built network.
159 * @return The built network
161 public Network build() {
167 /** {@link Server} builder. */
168 public static class ServerBuilder {
170 /** The parent network builder. */
171 private final NetworkBuilder networkBuilder;
173 /** The hostname of the server. */
174 private String hostname;
176 /** The unencrypted port numbers. */
177 private final Collection<Integer> unencryptedPorts = Sets.newHashSet();
179 /** The encrypted port numbers. */
180 private final Collection<Integer> encryptedPorts = Sets.newHashSet();
183 * Creates a new server builder.
185 * @param networkBuilder
186 * The parent network builder
188 private ServerBuilder(NetworkBuilder networkBuilder) {
189 this.networkBuilder = networkBuilder;
193 * Sets the hostname of the server to build.
196 * The hostname of the server
197 * @return This server builder
199 public ServerBuilder at(String hostname) {
200 this.hostname = hostname;
205 * Adds the given port number to the list of unencrypted port numbers.
208 * The port number to add
209 * @return This server builder
211 public ServerBuilder port(int port) {
212 unencryptedPorts.add(port);
217 * Adds the given port number to the list of encrypted port numbers.
220 * The port number to add
221 * @return This server builder
223 public ServerBuilder sslPort(int sslPort) {
224 encryptedPorts.add(sslPort);
229 * Builds the server, adds it to the parent network builder and returns the
232 * @return The network builder with the configured server added
234 public NetworkBuilder endServer() {
235 return networkBuilder.addServer(build());
241 * @return The built server
243 private Server build() {
244 return new Server(networkBuilder.network, hostname, unencryptedPorts, encryptedPorts);