From: David ‘Bombe’ Roden Date: Mon, 8 Apr 2013 19:09:22 +0000 (+0200) Subject: Add network and server container. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ed9bb3b5ae3b4d4c9ac086bab6a8d7ca3412e237;p=xudocci.git Add network and server container. --- diff --git a/src/main/java/net/pterodactylus/xdcc/data/Network.java b/src/main/java/net/pterodactylus/xdcc/data/Network.java new file mode 100644 index 0000000..17453a1 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/data/Network.java @@ -0,0 +1,231 @@ +/* + * XdccDownloader - Network.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 . + */ + +package net.pterodactylus.xdcc.data; + +import java.util.Collection; + +import com.beust.jcommander.internal.Sets; + +/** + * Defines a network. + * + * @author David ‘Bombe’ Roden + */ +public class Network { + + /** The name of the network. */ + private final String name; + + /** The servers of this network. */ + private final Collection servers = Sets.newHashSet(); + + /** + * Creates a new network with the given name. + * + * @param name + * The name of the network + */ + private Network(String name) { + this.name = name; + } + + // + // ACCESSORS + // + + /** + * Returns the name of this network. + * + * @return The name of this network + */ + public String name() { + return name; + } + + /** + * Returns the servers of this network. + * + * @return The servers of this network + */ + public Collection servers() { + return servers; + } + + // + // MUTATORS + // + + private void addServer(Server server) { + servers.add(server); + } + + // + // OBJECT METHODS + // + + @Override + public String toString() { + return String.format("%s(%d servers)", name(), servers().size()); + } + + // + // STATIC METHODS + // + + /** + * Returns a network builder. + * + * @param name + * The name of the network to build + * @return A network builder + */ + public static NetworkBuilder builder(String name) { + return new NetworkBuilder(name); + } + + /** {@link Network} builder. */ + public static class NetworkBuilder { + + /** The network being built. */ + private final Network network; + + /** + * Creates a new network builder. + * + * @param name + * The name of the network being built + */ + private NetworkBuilder(String name) { + network = new Network(name); + } + + /** + * Returns a server builder that will add the created server to this network. + * + * @return A server builder + */ + public ServerBuilder addServer() { + return new ServerBuilder(this); + } + + /** + * Adds the given server to this builder. + * + * @param server + * The server to add + * @return This network builder + */ + private NetworkBuilder addServer(Server server) { + network.addServer(server); + return this; + } + + /** + * Returns the built network. + * + * @return The built network + */ + public Network build() { + return network; + } + + } + + /** {@link Server} builder. */ + public static class ServerBuilder { + + /** The parent network builder. */ + private final NetworkBuilder networkBuilder; + + /** The hostname of the server. */ + private String hostname; + + /** The unencrypted port numbers. */ + private final Collection unencryptedPorts = Sets.newHashSet(); + + /** The encrypted port numbers. */ + private final Collection encryptedPorts = Sets.newHashSet(); + + /** + * Creates a new server builder. + * + * @param networkBuilder + * The parent network builder + */ + private ServerBuilder(NetworkBuilder networkBuilder) { + this.networkBuilder = networkBuilder; + } + + /** + * Sets the hostname of the server to build. + * + * @param hostname + * The hostname of the server + * @return This server builder + */ + public ServerBuilder at(String hostname) { + this.hostname = hostname; + return this; + } + + /** + * Adds the given port number to the list of unencrypted port numbers. + * + * @param port + * The port number to add + * @return This server builder + */ + public ServerBuilder port(int port) { + unencryptedPorts.add(port); + return this; + } + + /** + * Adds the given port number to the list of encrypted port numbers. + * + * @param sslPort + * The port number to add + * @return This server builder + */ + public ServerBuilder sslPort(int sslPort) { + encryptedPorts.add(sslPort); + return this; + } + + /** + * Builds the server, adds it to the parent network builder and returns the + * network builder. + * + * @return The network builder with the configured server added + */ + public NetworkBuilder endServer() { + return networkBuilder.addServer(build()); + } + + /** + * Builds the server. + * + * @return The built server + */ + private Server build() { + return new Server(networkBuilder.network, hostname, unencryptedPorts, encryptedPorts); + } + + } + +} diff --git a/src/main/java/net/pterodactylus/xdcc/data/Server.java b/src/main/java/net/pterodactylus/xdcc/data/Server.java new file mode 100644 index 0000000..38fce24 --- /dev/null +++ b/src/main/java/net/pterodactylus/xdcc/data/Server.java @@ -0,0 +1,102 @@ +/* + * XdccDownloader - Server.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 . + */ + +package net.pterodactylus.xdcc.data; + +import java.util.Collection; + +import com.google.common.collect.ImmutableSet; + +/** + * Defines a server in a {@link Network}. + * + * @author David ‘Bombe’ Roden + */ +public class Server { + + /** The network this server belongs to. */ + private final Network network; + + /** The hostname of this server. */ + private final String hostname; + + /** The unencrypted port numbers. */ + private final Collection unencryptedPorts; + + /** The encrypted port numbers. */ + private final Collection encryptedPorts; + + /** + * Creates a new server. + * + * @param network + * The network this server belongs to + * @param hostname + * The hostname of the server + * @param unencryptedPorts + * The unencrypted port numbers + * @param encryptedPorts + * The encrypted port numbers + */ + public Server(Network network, String hostname, Collection unencryptedPorts, Collection encryptedPorts) { + this.network = network; + this.hostname = hostname; + this.unencryptedPorts = ImmutableSet.copyOf(unencryptedPorts); + this.encryptedPorts = ImmutableSet.copyOf(encryptedPorts); + } + + // + // ACCESSORS + // + + /** + * Returns the network this server belongs to. + * + * @return The network this server belongs to + */ + public Network network() { + return network; + } + + /** + * Returns the hostname of this server. + * + * @return The hostname of this server + */ + public String hostname() { + return hostname; + } + + /** + * Returns the unencrypted port numbers. + * + * @return The unencrypted port numbers + */ + public Collection unencryptedPorts() { + return unencryptedPorts; + } + + /** + * Returns the encrypted port numbers. + * + * @return The encrypted port numbers + */ + public Collection encryptedPorts() { + return encryptedPorts; + } + +}