SimpleHost xref

    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/SimpleHost.java,v 1.1 2004/11/13 12:21:28 olegk Exp $
    3    * $Revision: 510582 $
    4    * $Date: 2007-02-22 17:42:43 +0100 (Thu, 22 Feb 2007) $
    5    *
    6    * ====================================================================
    7    *
    8    *  Licensed to the Apache Software Foundation (ASF) under one or more
    9    *  contributor license agreements.  See the NOTICE file distributed with
    10   *  this work for additional information regarding copyright ownership.
    11   *  The ASF licenses this file to You under the Apache License, Version 2.0
    12   *  (the "License"); you may not use this file except in compliance with
    13   *  the License.  You may obtain a copy of the License at
    14   *
    15   *      http://www.apache.org/licenses/LICENSE-2.0
    16   *
    17   *  Unless required by applicable law or agreed to in writing, software
    18   *  distributed under the License is distributed on an "AS IS" BASIS,
    19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    20   *  See the License for the specific language governing permissions and
    21   *  limitations under the License.
    22   * ====================================================================
    23   *
    24   * This software consists of voluntary contributions made by many
    25   * individuals on behalf of the Apache Software Foundation.  For more
    26   * information on the Apache Software Foundation, please see
    27   * <http://www.apache.org/>.
    28   *
    29   */
    30
    31  package org.apache.commons.httpclient.server;
    32
    33  /**
    34   * @author Oleg Kalnichevski
    35   */
    36  public class SimpleHost implements Cloneable {
    37
    38      private String hostname = null;
    39
    40      private int port = -1;
    41
    42      public SimpleHost(final String hostname, int port) {
    43          super();
    44          if (hostname == null) {
    45              throw new IllegalArgumentException("Host name may not be null");
    46          }
    47          if (port < 0) {
    48              throw new IllegalArgumentException("Port may not be negative");
    49          }
    50          this.hostname = hostname;
    51          this.port = port;
    52      }
    53
    54      public SimpleHost (final SimpleHost httphost) {
    55          super();
    56          init(httphost);
    57      }
    58
    59  	private void init(final SimpleHost httphost) {
    60  		this.hostname = httphost.hostname;
    61          this.port = httphost.port;
    62  	}
    63
    64      public Object clone() throws CloneNotSupportedException {
    65      	SimpleHost copy = (SimpleHost) super.clone();
    66      	copy.init(this);
    67          return copy;
    68      }
    69
    70      public String getHostName() {
    71          return this.hostname;
    72      }
    73
    74      public int getPort() {
    75          return this.port;
    76      }
    77
    78      public String toString() {
    79          StringBuffer buffer = new StringBuffer(50);
    80          buffer.append(this.hostname);
    81          buffer.append(':');
    82          buffer.append(this.port);
    83          return buffer.toString();
    84      }
    85
    86      public boolean equals(final Object o) {
    87
    88          if (o instanceof SimpleHost) {
    89              if (o == this) {
    90                  return true;
    91              }
    92              SimpleHost that = (SimpleHost) o;
    93              if (!this.hostname.equalsIgnoreCase(that.hostname)) {
    94                  return false;
    95              }
    96              if (this.port != that.port) {
    97                  return false;
    98              }
    99              return true;
    100         } else {
    101             return false;
    102         }
    103     }
    104
    105     public int hashCode() {
    106         return this.hostname.hashCode() + this.port;
    107     }
    108
    109 }