1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.naming;
  27 
  28 /**
  29   * This class represents the address of a communications end-point.
  30   * It consists of a type that describes the communication mechanism
  31   * and an address contents determined by an RefAddr subclass.
  32   *<p>
  33   * For example, an address type could be "BSD Printer Address",
  34   * which specifies that it is an address to be used with the BSD printing
  35   * protocol. Its contents could be the machine name identifying the
  36   * location of the printer server that understands this protocol.
  37   *<p>
  38   * A RefAddr is contained within a Reference.
  39   *<p>
  40   * RefAddr is an abstract class. Concrete implementations of it
  41   * determine its synchronization properties.
  42   *
  43   * @author Rosanna Lee
  44   * @author Scott Seligman
  45   *
  46   * @see Reference
  47   * @see LinkRef
  48   * @see StringRefAddr
  49   * @see BinaryRefAddr
  50   * @since 1.3
  51   */
  52 
  53   /*<p>
  54   * The serialized form of a RefAddr object consists of only its type name
  55   * String.
  56   */
  57 
  58 public abstract class RefAddr implements java.io.Serializable {
  59     /**
  60      * Contains the type of this address.
  61      * @serial
  62      */
  63     protected String addrType;
  64 
  65     /**
  66       * Constructs a new instance of RefAddr using its address type.
  67       *
  68       * @param addrType A non-null string describing the type of the address.
  69       */
  70     protected RefAddr(String addrType) {
  71         this.addrType = addrType;
  72     }
  73 
  74     /**
  75       * Retrieves the address type of this address.
  76       *
  77       * @return The non-null address type of this address.
  78       */
  79     public String getType() {
  80         return addrType;
  81     }
  82 
  83     /**
  84       * Retrieves the contents of this address.
  85       *
  86       * @return The possibly null address contents.
  87       */
  88     public abstract Object getContent();
  89 
  90     /**
  91       * Determines whether obj is equal to this RefAddr.
  92       *<p>
  93       * obj is equal to this RefAddr if all of these conditions are true
  94       *<ul>
  95       *<li> non-null
  96       *<li> instance of RefAddr
  97       *<li> obj has the same address type as this RefAddr (using String.compareTo())
  98       *<li> both obj and this RefAddr's contents are null or they are equal
  99       *         (using the equals() test).
 100       *</ul>
 101       * @param obj possibly null obj to check.
 102       * @return true if obj is equal to this refaddr; false otherwise.
 103       * @see #getContent
 104       * @see #getType
 105       */
 106     public boolean equals(Object obj) {
 107         if ((obj != null) && (obj instanceof RefAddr)) {
 108             RefAddr target = (RefAddr)obj;
 109             if (addrType.compareTo(target.addrType) == 0) {
 110                 Object thisobj = this.getContent();
 111                 Object thatobj = target.getContent();
 112                 if (thisobj == thatobj)
 113                     return true;
 114                 if (thisobj != null)
 115                     return thisobj.equals(thatobj);
 116             }
 117         }
 118         return false;
 119     }
 120 
 121     /**
 122       * Computes the hash code of this address using its address type and contents.
 123       * The hash code is the sum of the hash code of the address type and
 124       * the hash code of the address contents.
 125       *
 126       * @return The hash code of this address as an int.
 127       * @see java.lang.Object#hashCode
 128       */
 129     public int hashCode() {
 130         return (getContent() == null)
 131                 ? addrType.hashCode()
 132                 : addrType.hashCode() + getContent().hashCode();
 133     }
 134 
 135     /**
 136       * Generates the string representation of this address.
 137       * The string consists of the address's type and contents with labels.
 138       * This representation is intended for display only and not to be parsed.
 139       * @return The non-null string representation of this address.
 140       */
 141     public String toString(){
 142         StringBuffer str = new StringBuffer("Type: " + addrType + "\n");
 143 
 144         str.append("Content: " + getContent() + "\n");
 145         return (str.toString());
 146     }
 147 
 148     /**
 149      * Use serialVersionUID from JNDI 1.1.1 for interoperability
 150      */
 151     private static final long serialVersionUID = -1468165120479154358L;
 152 }