/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.api.keys;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.exception.ErrorCode;
import org.chaidb.db.index.Key;
import java.util.ArrayList;
public class ByteArrayKey implements Key {
byte[] key;
public ByteArrayKey(byte[] key) {
System.arraycopy(key, 0, this.key, 0, key.length);
}
public ByteArrayKey(ByteArrayKey orgKey) {
System.arraycopy(orgKey.key, 0, this.key, 0, orgKey.key.length);
}
public byte[] toBytes() {
byte[] ret = new byte[key.length];
System.arraycopy(ret, 0, key, 0, key.length);
return ret;
}
public int size() {
return key.length;
}
public String toString() {
return "ByteArrayKey[id=" + this.hashCode() + ", length=" + key.length + "]";
}
public int getKeyType() {
return BYTEARRAY_KEY;
}
public Object clone() {
return new ByteArrayKey(key);
}
public int compareTo(Key otherKey) throws ChaiDBException {
checkKeyType(otherKey);
ByteArrayKey key2 = (ByteArrayKey) otherKey;
return KeyTool.compareByteAray(key, key2.key);
}
private void checkKeyType(Key key) throws ChaiDBException {
int keyType = key.getKeyType();
if (keyType != BYTEARRAY_KEY) {
ArrayList params = new ArrayList(2);
// required
params.add(Key.KEY_TYPES[Key.BYTEARRAY_KEY]);
// found
params.add(Key.KEY_TYPES[keyType]);
throw new ChaiDBException(ErrorCode.INDEX_KEY_TYPE_ERROR, params);
}
}
// override java.lang.Object methods
public int hashCode() {
return key.hashCode();
}
public boolean equals(Object obj) {
if (obj instanceof ByteArrayKey) return KeyTool.compareByteAray(key, ((ByteArrayKey) obj).key) == 0;
return false;
}
}