KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > poa > util > ByteArrayKey


1 package org.jacorb.poa.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23
24 /**
25  * This class wraps byte arrays so that they can be used as keys
26  * in hashtables.
27  *
28  * @author Steve Osselton
29  * @version $Id: ByteArrayKey.java,v 1.8 2004/05/06 12:40:01 nicolas Exp $
30  */

31
32 public class ByteArrayKey
33 {
34     private int cacheH = 0;
35     private byte[] bytes = null;
36     private String JavaDoc cacheS = null;
37
38     public ByteArrayKey (byte[] array)
39     {
40         bytes = array;
41     }
42
43     public ByteArrayKey (ByteArrayKey bak)
44     {
45        cacheH = bak.cacheH;
46        cacheS = bak.cacheS;
47        bytes = bak.bytes;
48     }
49
50     public byte[] getBytes ()
51     {
52         return bytes;
53     }
54
55     /**
56      * Overrides hashCode () in Object
57      */

58     public int hashCode ()
59     {
60         if( cacheH == 0 )
61         {
62             long h = 1234;
63
64             if ((bytes != null) && (bytes.length > 0))
65             {
66                 for (int i = bytes.length; --i >= 0; )
67                 {
68                     h ^= bytes[i] * (i + 1);
69                 }
70                 cacheH = (int)((h >> 32) ^ h);
71             }
72         }
73         return cacheH;
74     }
75
76     /**
77      * Overrides equals () in Object
78      */

79     public boolean equals (Object JavaDoc obj)
80     {
81         boolean result = false;
82
83         if (obj instanceof ByteArrayKey)
84         {
85             ByteArrayKey key = (ByteArrayKey) obj;
86
87             if ((bytes == key.bytes) || ((bytes == null) && (key.bytes == null)))
88             {
89                 result = true;
90             }
91             else if ((bytes != null) && (key.bytes != null))
92             {
93                 if (bytes.length == key.bytes.length)
94                 {
95                     result = true;
96                     for (int i = 0; i < bytes.length; i++)
97                     {
98                         if (bytes[i] != key.bytes[i])
99                         {
100                             result = false;
101                             break;
102                         }
103                     }
104                 }
105             }
106         }
107
108         return result;
109     }
110
111     public String JavaDoc toString()
112     {
113         if( cacheS == null )
114         {
115             if( bytes == null )
116             {
117                 cacheS = "";
118             }
119             else
120             {
121                 cacheS = new String JavaDoc(bytes);
122             }
123         }
124         return cacheS;
125     }
126 }
127
Popular Tags