InternalBindingKey.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.naming.cosnaming;
27
28import org.omg.CosNaming.NameComponent;
29
30/**
31 * Class InternalBindingKey implements the necessary wrapper code
32 * around the org.omg.CosNaming::NameComponent class to implement the proper
33 * equals() method and the hashCode() method for use in a hash table.
34 * It computes the hashCode once and stores it, and also precomputes
35 * the lengths of the id and kind strings for faster comparison.
36 */
37public class InternalBindingKey
38{
39    // A key contains a name
40    public NameComponent name;
41    private int idLen;
42    private int kindLen;
43    private int hashVal;
44
45    // Default Constructor
46    public InternalBindingKey() {}
47
48    // Normal constructor
49    public InternalBindingKey(NameComponent n)
50    {
51        idLen = 0;
52        kindLen = 0;
53        setup(n);
54    }
55
56    // Setup the object
57    protected void setup(NameComponent n) {
58        this.name = n;
59        // Precompute lengths and values since they will not change
60        if( this.name.id != null ) {
61            idLen = this.name.id.length();
62        }
63        if( this.name.kind != null ) {
64            kindLen = this.name.kind.length();
65        }
66        hashVal = 0;
67        if (idLen > 0)
68            hashVal += this.name.id.hashCode();
69        if (kindLen > 0)
70            hashVal += this.name.kind.hashCode();
71    }
72
73    // Compare the keys by comparing name's id and kind
74    public boolean equals(java.lang.Object o) {
75        if (o == null)
76            return false;
77        if (o instanceof InternalBindingKey) {
78            InternalBindingKey that = (InternalBindingKey)o;
79            // Both lengths must match
80            if (this.idLen != that.idLen || this.kindLen != that.kindLen) {
81                return false;
82            }
83            // If id is set is must be equal
84            if (this.idLen > 0 && this.name.id.equals(that.name.id) == false) {
85                return false;
86            }
87            // If kind is set it must be equal
88            if (this.kindLen > 0 && this.name.kind.equals(that.name.kind) == false) {
89                return false;
90            }
91            // Must be the same
92            return true;
93        } else {
94            return false;
95        }
96    }
97    // Return precomputed value
98    public int hashCode() {
99        return this.hashVal;
100    }
101}
102