1 /*
   2  * Copyright (c) 1999, 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 
  26 package com.sun.corba.se.impl.naming.pcosnaming;
  27 
  28 import java.io.Serializable;
  29 import org.omg.CosNaming.NameComponent;
  30 
  31 
  32 /**
  33  * Class InternalBindingKey implements the necessary wrapper code
  34  * around the org.omg.CosNaming::NameComponent class to implement the proper
  35  * equals() method and the hashCode() method for use in a hash table.
  36  * It computes the hashCode once and stores it, and also precomputes
  37  * the lengths of the id and kind strings for faster comparison.
  38  */
  39 public class InternalBindingKey
  40         implements Serializable
  41 {
  42 
  43     // computed by serialver tool
  44     private static final long serialVersionUID = -5410796631793704055L;
  45 
  46     public String id;
  47     public String kind;
  48 
  49     // Default Constructor
  50     public InternalBindingKey() {}
  51 
  52     // Normal constructor
  53     public InternalBindingKey(NameComponent n)
  54     {
  55         setup(n);
  56     }
  57 
  58     // Setup the object
  59     protected void setup(NameComponent n) {
  60         this.id = n.id;
  61         this.kind = n.kind;
  62     }
  63 
  64     // Compare the keys by comparing name's id and kind
  65     public boolean equals(java.lang.Object o) {
  66         if (o == null)
  67             return false;
  68         if (o instanceof InternalBindingKey) {
  69             InternalBindingKey that = (InternalBindingKey)o;
  70             if( this.id != null && that.id != null )
  71             {
  72                 if (this.id.length() != that.id.length() )
  73                 {
  74                         return false;
  75                 }
  76                 // If id is set is must be equal
  77                 if (this.id.length() > 0 && this.id.equals(that.id) == false)
  78                 {
  79                         return false;
  80                 }
  81             }
  82             else
  83             {
  84                 // If One is Null and the other is not then it's a mismatch
  85                 // So, return false
  86                 if( ( this.id == null && that.id != null )
  87                 ||  ( this.id !=null && that.id == null ) )
  88                 {
  89                         return false;
  90                 }
  91             }
  92             if( this.kind != null && that.kind != null )
  93             {
  94                 if (this.kind.length() != that.kind.length() )
  95                 {
  96                         return false;
  97                 }
  98                 // If kind is set it must be equal
  99                 if (this.kind.length() > 0 && this.kind.equals(that.kind) == false)
 100                 {
 101                         return false;
 102                 }
 103             }
 104             else
 105             {
 106                 // If One is Null and the other is not then it's a mismatch
 107                 // So, return false
 108                 if( ( this.kind == null && that.kind != null )
 109                 ||  ( this.kind !=null && that.kind == null ) )
 110                 {
 111                         return false;
 112                 }
 113             }
 114             // We have checked all the possibilities, so return true
 115             return true;
 116         } else {
 117             return false;
 118         }
 119     }
 120 
 121 
 122     // Return precomputed value
 123     public int hashCode() {
 124         int hashVal = 0;
 125         if (this.id.length() > 0)
 126         {
 127             hashVal += this.id.hashCode();
 128         }
 129         if (this.kind.length() > 0)
 130         {
 131             hashVal += this.kind.hashCode();
 132         }
 133         return hashVal;
 134     }
 135 }