Jake Slack | 03928ae | 2014-05-13 18:41:56 -0700 | [diff] [blame] | 1 | // |
| 2 | // ======================================================================== |
| 3 | // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
| 4 | // ------------------------------------------------------------------------ |
| 5 | // All rights reserved. This program and the accompanying materials |
| 6 | // are made available under the terms of the Eclipse Public License v1.0 |
| 7 | // and Apache License v2.0 which accompanies this distribution. |
| 8 | // |
| 9 | // The Eclipse Public License is available at |
| 10 | // http://www.eclipse.org/legal/epl-v10.html |
| 11 | // |
| 12 | // The Apache License v2.0 is available at |
| 13 | // http://www.opensource.org/licenses/apache2.0.php |
| 14 | // |
| 15 | // You may elect to redistribute this code under either of these licenses. |
| 16 | // ======================================================================== |
| 17 | // |
| 18 | |
| 19 | package org.eclipse.jetty.client; |
| 20 | |
| 21 | import java.net.InetSocketAddress; |
| 22 | |
| 23 | /** |
| 24 | * @version $Revision: 4135 $ $Date: 2008-12-02 11:57:07 +0100 (Tue, 02 Dec 2008) $ |
| 25 | */ |
| 26 | public class Address |
| 27 | { |
| 28 | private final String host; |
| 29 | private final int port; |
| 30 | |
| 31 | public static Address from(String hostAndPort) |
| 32 | { |
| 33 | String host; |
| 34 | int port; |
| 35 | int colon = hostAndPort.indexOf(':'); |
| 36 | if (colon >= 0) |
| 37 | { |
| 38 | host = hostAndPort.substring(0, colon); |
| 39 | port = Integer.parseInt(hostAndPort.substring(colon + 1)); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | host = hostAndPort; |
| 44 | port = 0; |
| 45 | } |
| 46 | return new Address(host, port); |
| 47 | } |
| 48 | |
| 49 | public Address(String host, int port) |
| 50 | { |
| 51 | if (host == null) |
| 52 | throw new IllegalArgumentException("Host is null"); |
| 53 | |
| 54 | this.host = host.trim(); |
| 55 | this.port = port; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public boolean equals(Object obj) |
| 60 | { |
| 61 | if (this == obj) return true; |
| 62 | if (obj == null || getClass() != obj.getClass()) return false; |
| 63 | Address that = (Address)obj; |
| 64 | if (!host.equals(that.host)) return false; |
| 65 | return port == that.port; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public int hashCode() |
| 70 | { |
| 71 | int result = host.hashCode(); |
| 72 | result = 31 * result + port; |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | public String getHost() |
| 77 | { |
| 78 | return host; |
| 79 | } |
| 80 | |
| 81 | public int getPort() |
| 82 | { |
| 83 | return port; |
| 84 | } |
| 85 | |
| 86 | public InetSocketAddress toSocketAddress() |
| 87 | { |
| 88 | return new InetSocketAddress(getHost(), getPort()); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public String toString() |
| 93 | { |
| 94 | return host + ":" + port; |
| 95 | } |
| 96 | } |