001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.types;
028    
029    
030    /**
031     * This class defines a data structure that combines an address and
032     * port number, as may be used to accept a connection from or initiate
033     * a connection to a remote system.
034     */
035    @org.opends.server.types.PublicAPI(
036         stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
037         mayInstantiate=false,
038         mayExtend=false,
039         mayInvoke=true)
040    public final class HostPort
041    {
042      // The host for this object.
043      private final String host;
044    
045      // The port for this object;
046      private final int port;
047    
048    
049    
050      /**
051       * Creates a new {@code HostPort} object with the specified port
052       * number but no host.
053       *
054       * @param  port  The port number for this {@code HostPort} object.
055       */
056      public HostPort(int port)
057      {
058        this.host = null;
059        this.port = port;
060      }
061    
062    
063    
064      /**
065       * Creates a new {@code HostPort} object with the specified port
066       * number but no explicit host.
067       *
068       * @param  host  The host address or name for this {@code HostPort}
069       *               object, or {@code null} if there is none.
070       * @param  port  The port number for this {@code HostPort} object.
071       */
072      public HostPort(String host, int port)
073      {
074        this.host = host;
075        this.port = port;
076      }
077    
078    
079    
080      /**
081       * Retrieves the host for this {@code HostPort} object.
082       *
083       * @return  The host for this {@code HostPort} object, or
084       *          {@code null} if none was provided.
085       */
086      public String getHost()
087      {
088        return host;
089      }
090    
091    
092    
093      /**
094       * Retrieves the port number for this {@code HostPort} object.
095       *
096       * @return  The port number for this {@code HostPort} object.
097       */
098      public int getPort()
099      {
100        return port;
101      }
102    
103    
104    
105      /**
106       * Retrieves a string representation of this {@code HostPort}
107       * object.  It will be the host element (or nothing if no host was
108       * given) followed by a colon and the port number.
109       *
110       * @return  A string representation of this {@code HostPort} object.
111       */
112      public String toString()
113      {
114        if (host ==  null)
115        {
116          return ":" + port;
117        }
118        else
119        {
120          return host + ":" + port;
121        }
122      }
123    
124      /**
125       * Returns {@code true} if the provided Object is a HostPort object
126       * with the same host name and port than this HostPort object.
127       * @param obj the reference object with which to compare.
128       * @return  {@code true} if this object is the same as the obj
129       * argument; {@code false} otherwise.
130       */
131      public boolean equals(Object obj)
132      {
133        boolean equals = false;
134        if (obj != null)
135        {
136          if (obj == this)
137          {
138            equals = true;
139          }
140          else if (obj instanceof HostPort)
141          {
142            equals = toString().equals(obj.toString());
143          }
144        }
145        return equals;
146      }
147    
148      /**
149       * Retrieves a hash code for this HostPort object.
150       *
151       * @return  A hash code for this HostPort object.
152       */
153      public int hashCode()
154      {
155        return toString().hashCode();
156      }
157    }
158