001: package org.jacorb.poa.util;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: /**
024: * This class wraps byte arrays so that they can be used as keys
025: * in hashtables.
026: *
027: * @author Steve Osselton
028: * @version $Id: ByteArrayKey.java,v 1.8 2004/05/06 12:40:01 nicolas Exp $
029: */
030:
031: public class ByteArrayKey {
032: private int cacheH = 0;
033: private byte[] bytes = null;
034: private String cacheS = null;
035:
036: public ByteArrayKey(byte[] array) {
037: bytes = array;
038: }
039:
040: public ByteArrayKey(ByteArrayKey bak) {
041: cacheH = bak.cacheH;
042: cacheS = bak.cacheS;
043: bytes = bak.bytes;
044: }
045:
046: public byte[] getBytes() {
047: return bytes;
048: }
049:
050: /**
051: * Overrides hashCode () in Object
052: */
053: public int hashCode() {
054: if (cacheH == 0) {
055: long h = 1234;
056:
057: if ((bytes != null) && (bytes.length > 0)) {
058: for (int i = bytes.length; --i >= 0;) {
059: h ^= bytes[i] * (i + 1);
060: }
061: cacheH = (int) ((h >> 32) ^ h);
062: }
063: }
064: return cacheH;
065: }
066:
067: /**
068: * Overrides equals () in Object
069: */
070: public boolean equals(Object obj) {
071: boolean result = false;
072:
073: if (obj instanceof ByteArrayKey) {
074: ByteArrayKey key = (ByteArrayKey) obj;
075:
076: if ((bytes == key.bytes)
077: || ((bytes == null) && (key.bytes == null))) {
078: result = true;
079: } else if ((bytes != null) && (key.bytes != null)) {
080: if (bytes.length == key.bytes.length) {
081: result = true;
082: for (int i = 0; i < bytes.length; i++) {
083: if (bytes[i] != key.bytes[i]) {
084: result = false;
085: break;
086: }
087: }
088: }
089: }
090: }
091:
092: return result;
093: }
094:
095: public String toString() {
096: if (cacheS == null) {
097: if (bytes == null) {
098: cacheS = "";
099: } else {
100: cacheS = new String(bytes);
101: }
102: }
103: return cacheS;
104: }
105: }
|