1. /*
  2. * @(#)InternalBindingKey.java 1.23 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.impl.naming.cosnaming;
  8. import org.omg.CosNaming.NameComponent;
  9. /**
  10. * Class InternalBindingKey implements the necessary wrapper code
  11. * around the org.omg.CosNaming::NameComponent class to implement the proper
  12. * equals() method and the hashCode() method for use in a hash table.
  13. * It computes the hashCode once and stores it, and also precomputes
  14. * the lengths of the id and kind strings for faster comparison.
  15. */
  16. public class InternalBindingKey
  17. {
  18. // A key contains a name
  19. public NameComponent name;
  20. private int idLen;
  21. private int kindLen;
  22. private int hashVal;
  23. // Default Constructor
  24. public InternalBindingKey() {}
  25. // Normal constructor
  26. public InternalBindingKey(NameComponent n)
  27. {
  28. idLen = 0;
  29. kindLen = 0;
  30. setup(n);
  31. }
  32. // Setup the object
  33. protected void setup(NameComponent n) {
  34. this.name = n;
  35. // Precompute lengths and values since they will not change
  36. if( this.name.id != null ) {
  37. idLen = this.name.id.length();
  38. }
  39. if( this.name.kind != null ) {
  40. kindLen = this.name.kind.length();
  41. }
  42. hashVal = 0;
  43. if (idLen > 0)
  44. hashVal += this.name.id.hashCode();
  45. if (kindLen > 0)
  46. hashVal += this.name.kind.hashCode();
  47. }
  48. // Compare the keys by comparing name's id and kind
  49. public boolean equals(java.lang.Object o) {
  50. if (o == null)
  51. return false;
  52. if (o instanceof InternalBindingKey) {
  53. InternalBindingKey that = (InternalBindingKey)o;
  54. // Both lengths must match
  55. if (this.idLen != that.idLen || this.kindLen != that.kindLen) {
  56. return false;
  57. }
  58. // If id is set is must be equal
  59. if (this.idLen > 0 && this.name.id.equals(that.name.id) == false) {
  60. return false;
  61. }
  62. // If kind is set it must be equal
  63. if (this.kindLen > 0 && this.name.kind.equals(that.name.kind) == false) {
  64. return false;
  65. }
  66. // Must be the same
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. }
  72. // Return precomputed value
  73. public int hashCode() {
  74. return this.hashVal;
  75. }
  76. }