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