/*
 * Copyright (c) 2008-2014 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb;

import org.mongodb.annotations.Immutable;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

@Immutable
public class ServerAddress {
    private final org.mongodb.connection.ServerAddress proxied;

    /**
     * Returns the default database host: "127.0.0.1"
     *
     * @return IP address of default host.
     */
    public static String defaultHost() {
        return org.mongodb.connection.ServerAddress.getDefaultHost();
    }

    /**
     * Returns the default database port: 27017
     *
     * @return the default port
     */
    public static int defaultPort() {
        return org.mongodb.connection.ServerAddress.getDefaultPort();
    }

    public ServerAddress() throws UnknownHostException {
        proxied = new org.mongodb.connection.ServerAddress();
    }

    public ServerAddress(final String host) throws UnknownHostException {
        proxied = new org.mongodb.connection.ServerAddress(host);
    }

    public ServerAddress(final String host, final int port) {
        proxied = new org.mongodb.connection.ServerAddress(host, port);
    }

    public ServerAddress(final InetAddress inetAddress) {
        this(inetAddress.getHostName(), defaultPort());
    }

    public ServerAddress(final InetAddress inetAddress, final int port) {
        this(inetAddress.getHostName(), port);
    }

    public ServerAddress(final InetSocketAddress inetSocketAddress) {
        this(inetSocketAddress.getAddress(), inetSocketAddress.getPort());
    }

    public ServerAddress(final org.mongodb.connection.ServerAddress address) {
        proxied = address;
    }

    /**
     * Gets the hostname
     *
     * @return hostname
     */
    public String getHost() {
        return proxied.getHost();
    }

    /**
     * Gets the port number
     *
     * @return port
     */
    public int getPort() {
        return proxied.getPort();
    }

    /**
     * Gets the underlying socket address
     *
     * @return socket address
     */
    public InetSocketAddress getSocketAddress() {
        try {
            return proxied.getSocketAddress();
        } catch (UnknownHostException e) {
            throw new MongoException("Unknown host", e);
        }
    }

    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        if (other == null || getClass() != other.getClass()) {
            return false;
        }

        ServerAddress that = (ServerAddress) other;

        if (!proxied.equals(that.proxied)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return proxied.hashCode();
    }

    @Override
    public String toString() {
        return proxied.toString();
    }

    org.mongodb.connection.ServerAddress toNew() {
        return proxied;
    }

    /**
     * Determines whether this address is the same as a given host.
     *
     * @param hostName the address to compare
     * @return if they are the same
     */
    public boolean sameHost(final String hostName) {
        String hostToUse = hostName;
        int idx = hostToUse.indexOf(":");
        int portToUse = defaultPort();
        if (idx > 0) {
            portToUse = Integer.parseInt(hostToUse.substring(idx + 1));
            hostToUse = hostToUse.substring(0, idx);
        }

        return getPort() == portToUse && getHost().equalsIgnoreCase(hostToUse);
    }
}

@@ -1319,32 +1319,142 @@
 tPort();%0A    %7D%0A%0A
+    /**%0A     * Creates a ServerAddress with default host and port%0A     * @throws UnknownHostException%0A     */%0A
     public Serve
@@ -1555,32 +1555,285 @@
 dress();%0A    %7D%0A%0A
+    /**%0A     * Creates a ServerAddress with default port.%0A     * If the host parameter contains a port, i.e. 127.0.0.1:3333%0A     * it will use that port instead of the default one.%0A     * @param host hostname%0A     * @throws UnknownHostException%0A     */%0A
     public Serve
@@ -1955,32 +1955,174 @@
 s(host);%0A    %7D%0A%0A
+    /**%0A     * Creates a ServerAddress%0A     * @param host hostname%0A     * @param port mongod port%0A     * @throws UnknownHostException%0A     */%0A
     public Serve
@@ -2238,32 +2238,129 @@
 , port);%0A    %7D%0A%0A
+    /**%0A     * Creates a ServerAddress with default port%0A     * @param addr host address%0A     */%0A
     public Serve
@@ -2456,32 +2456,142 @@
 Port());%0A    %7D%0A%0A
+    /**%0A     * Creates a ServerAddress%0A     * @param addr host address%0A     * @param port mongod port%0A     */%0A
     public Serve
@@ -2694,32 +2694,147 @@
 , port);%0A    %7D%0A%0A
+    /**%0A     * Creates a ServerAddress%0A     * @param addr inet socket address containing hostname and port%0A     */%0A
     public Serve

Enhance doc on ServerAddress constructors