KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > naming > cosnaming > InternalBindingKey


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
8 package com.sun.corba.se.impl.naming.cosnaming;
9
10 import org.omg.CosNaming.NameComponent JavaDoc;
11
12 /**
13  * Class InternalBindingKey implements the necessary wrapper code
14  * around the org.omg.CosNaming::NameComponent class to implement the proper
15  * equals() method and the hashCode() method for use in a hash table.
16  * It computes the hashCode once and stores it, and also precomputes
17  * the lengths of the id and kind strings for faster comparison.
18  */

19 public class InternalBindingKey
20 {
21     // A key contains a name
22
public NameComponent JavaDoc name;
23     private int idLen;
24     private int kindLen;
25     private int hashVal;
26
27     // Default Constructor
28
public InternalBindingKey() {}
29
30     // Normal constructor
31
public InternalBindingKey(NameComponent JavaDoc n)
32     {
33         idLen = 0;
34         kindLen = 0;
35     setup(n);
36     }
37
38     // Setup the object
39
protected void setup(NameComponent JavaDoc n) {
40     this.name = n;
41     // Precompute lengths and values since they will not change
42
if( this.name.id != null ) {
43         idLen = this.name.id.length();
44         }
45         if( this.name.kind != null ) {
46         kindLen = this.name.kind.length();
47         }
48     hashVal = 0;
49     if (idLen > 0)
50         hashVal += this.name.id.hashCode();
51     if (kindLen > 0)
52         hashVal += this.name.kind.hashCode();
53     }
54
55     // Compare the keys by comparing name's id and kind
56
public boolean equals(java.lang.Object JavaDoc o) {
57     if (o == null)
58         return false;
59     if (o instanceof InternalBindingKey) {
60         InternalBindingKey that = (InternalBindingKey)o;
61         // Both lengths must match
62
if (this.idLen != that.idLen || this.kindLen != that.kindLen) {
63         return false;
64         }
65         // If id is set is must be equal
66
if (this.idLen > 0 && this.name.id.equals(that.name.id) == false) {
67         return false;
68         }
69         // If kind is set it must be equal
70
if (this.kindLen > 0 && this.name.kind.equals(that.name.kind) == false) {
71         return false;
72         }
73         // Must be the same
74
return true;
75     } else {
76         return false;
77     }
78     }
79     // Return precomputed value
80
public int hashCode() {
81     return this.hashVal;
82     }
83 }
84
85
Popular Tags