1. /*
  2. * @(#)RefAddr.java 1.6 01/02/09
  3. *
  4. * Copyright 1999-2001 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.naming;
  11. /**
  12. * This class represents the address of a communications end-point.
  13. * It consists of a type that describes the communication mechanism
  14. * and an address contents determined by an RefAddr subclass.
  15. *<p>
  16. * For example, an address type could be "BSD Printer Address",
  17. * which specifies that it is an address to be used with the BSD printing
  18. * protocol. Its contents could be the machine name identifying the
  19. * location of the printer server that understands this protocol.
  20. *<p>
  21. * A RefAddr is contained within a Reference.
  22. *<p>
  23. * RefAddr is an abstract class. Concrete implementations of it
  24. * determine its synchronization properties.
  25. *
  26. * @author Rosanna Lee
  27. * @author Scott Seligman
  28. * @version 1.6 01/02/09
  29. *
  30. * @see Reference
  31. * @see LinkRef
  32. * @see StringRefAddr
  33. * @see BinaryRefAddr
  34. * @since 1.3
  35. */
  36. /*<p>
  37. * The serialized form of a RefAddr object consists of only its type name
  38. * String.
  39. */
  40. public abstract class RefAddr implements java.io.Serializable {
  41. /**
  42. * Contains the type of this address.
  43. * @serial
  44. */
  45. protected String addrType;
  46. /**
  47. * Constructs a new instance of RefAddr using its address type.
  48. *
  49. * @param addrType A non-null string describing the type of the address.
  50. */
  51. protected RefAddr(String addrType) {
  52. this.addrType = addrType;
  53. }
  54. /**
  55. * Retrieves the address type of this address.
  56. *
  57. * @return The non-null address type of this address.
  58. */
  59. public String getType() {
  60. return addrType;
  61. }
  62. /**
  63. * Retrieves the contents of this address.
  64. *
  65. * @return The possibly null address contents.
  66. */
  67. public abstract Object getContent();
  68. /**
  69. * Determines whether obj is equal to this RefAddr.
  70. *<p>
  71. * obj is equal to this RefAddr all of these conditions are true
  72. *<ul> non-null
  73. *<li> instance of RefAddr
  74. *<li> obj has the same address type as this RefAddr (using String.compareTo())
  75. *<li> both obj and this RefAddr's contents are null or they are equal
  76. * (using the equals() test).
  77. *</ul>
  78. * @param obj possibly null obj to check.
  79. * @return true if obj is equal to this refaddr; false otherwise.
  80. * @see #getContent
  81. * @see #getType
  82. */
  83. public boolean equals(Object obj) {
  84. if ((obj != null) && (obj instanceof RefAddr)) {
  85. RefAddr target = (RefAddr)obj;
  86. if (addrType.compareTo(target.addrType) == 0) {
  87. Object thisobj = this.getContent();
  88. Object thatobj = target.getContent();
  89. if (thisobj == thatobj)
  90. return true;
  91. if (thisobj != null)
  92. return thisobj.equals(thatobj);
  93. }
  94. }
  95. return false;
  96. }
  97. /**
  98. * Computes the hash code of this address using its address type and contents.
  99. * The hash code is the sum of the hash code of the address type and
  100. * the hash code of the address contents.
  101. *
  102. * @return The hash code of this address as an int.
  103. * @see java.lang.Object#hashCode
  104. */
  105. public int hashCode() {
  106. return (getContent() == null)
  107. ? addrType.hashCode()
  108. : addrType.hashCode() + getContent().hashCode();
  109. }
  110. /**
  111. * Generates the string representation of this address.
  112. * The string consists of the address's type and contents with labels.
  113. * This representation is intended for display only and not to be parsed.
  114. * @return The non-null string representation of this address.
  115. */
  116. public String toString(){
  117. StringBuffer str = new StringBuffer("Type: " + addrType + "\n");
  118. str.append("Content: " + getContent() + "\n");
  119. return (str.toString());
  120. }
  121. /**
  122. * Use serialVersionUID from JNDI 1.1.1 for interoperability
  123. */
  124. private static final long serialVersionUID = -1468165120479154358L;
  125. }