1. /*
  2. * @(#)InternalBindingKey.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. /*
  8. * @(#)InternalBindingKey.java 1.7 03/01/23
  9. *
  10. * Copyright 1993-1997 Sun Microsystems, Inc. 901 San Antonio Road,
  11. * Palo Alto, California, 94303, U.S.A. All Rights Reserved.
  12. *
  13. * This software is the confidential and proprietary information of Sun
  14. * Microsystems, Inc. ("Confidential Information"). You shall not
  15. * disclose such Confidential Information and shall use it only in
  16. * accordance with the terms of the license agreement you entered into
  17. * with Sun.
  18. *
  19. * CopyrightVersion 1.2
  20. *
  21. */
  22. package com.sun.corba.se.internal.PCosNaming;
  23. import java.io.Serializable;
  24. import org.omg.CosNaming.NameComponent;
  25. /**
  26. * Class InternalBindingKey implements the necessary wrapper code
  27. * around the org.omg.CosNaming::NameComponent class to implement the proper
  28. * equals() method and the hashCode() method for use in a hash table.
  29. * It computes the hashCode once and stores it, and also precomputes
  30. * the lengths of the id and kind strings for faster comparison.
  31. */
  32. public class InternalBindingKey
  33. implements Serializable
  34. {
  35. // computed by serialver tool
  36. private static final long serialVersionUID = -5410796631793704055L;
  37. public String id;
  38. public String kind;
  39. // Default Constructor
  40. public InternalBindingKey() {}
  41. // Normal constructor
  42. public InternalBindingKey(NameComponent n)
  43. {
  44. setup(n);
  45. }
  46. // Setup the object
  47. protected void setup(NameComponent n) {
  48. this.id = n.id;
  49. this.kind = n.kind;
  50. }
  51. // Compare the keys by comparing name's id and kind
  52. public boolean equals(java.lang.Object o) {
  53. if (o == null)
  54. return false;
  55. if (o instanceof InternalBindingKey) {
  56. InternalBindingKey that = (InternalBindingKey)o;
  57. if( this.id != null && that.id != null )
  58. {
  59. if (this.id.length() != that.id.length() )
  60. {
  61. return false;
  62. }
  63. // If id is set is must be equal
  64. if (this.id.length() > 0 && this.id.equals(that.id) == false)
  65. {
  66. return false;
  67. }
  68. }
  69. else
  70. {
  71. // If One is Null and the other is not then it's a mismatch
  72. // So, return false
  73. if( ( this.id == null && that.id != null )
  74. || ( this.id !=null && that.id == null ) )
  75. {
  76. return false;
  77. }
  78. }
  79. if( this.kind != null && that.kind != null )
  80. {
  81. if (this.kind.length() != that.kind.length() )
  82. {
  83. return false;
  84. }
  85. // If kind is set it must be equal
  86. if (this.kind.length() > 0 && this.kind.equals(that.kind) == false)
  87. {
  88. return false;
  89. }
  90. }
  91. else
  92. {
  93. // If One is Null and the other is not then it's a mismatch
  94. // So, return false
  95. if( ( this.kind == null && that.kind != null )
  96. || ( this.kind !=null && that.kind == null ) )
  97. {
  98. return false;
  99. }
  100. }
  101. // We have checked all the possibilities, so return true
  102. return true;
  103. } else {
  104. return false;
  105. }
  106. }
  107. // Return precomputed value
  108. public int hashCode() {
  109. int hashVal = 0;
  110. if (this.id.length() > 0)
  111. {
  112. hashVal += this.id.hashCode();
  113. }
  114. if (this.kind.length() > 0)
  115. {
  116. hashVal += this.kind.hashCode();
  117. }
  118. return hashVal;
  119. }
  120. }