001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.slots;
022:
023: import com.db4o.internal.*;
024:
025: /**
026: * @exclude
027: */
028: public class Slot {
029:
030: private final int _address;
031:
032: private final int _length;
033:
034: public static final Slot ZERO = new Slot(0, 0);
035:
036: public Slot(int address, int length) {
037: _address = address;
038: _length = length;
039: }
040:
041: public int address() {
042: return _address;
043: }
044:
045: public int length() {
046: return _length;
047: }
048:
049: public boolean equals(Object obj) {
050: if (obj == this ) {
051: return true;
052: }
053: if (!(obj instanceof Slot)) {
054: return false;
055: }
056: Slot other = (Slot) obj;
057: return (_address == other._address)
058: && (length() == other.length());
059: }
060:
061: public int hashCode() {
062: return _address ^ length();
063: }
064:
065: public Slot subSlot(int offset) {
066: return new Slot(_address + offset, length() - offset);
067: }
068:
069: public String toString() {
070: return "[A:" + _address + ",L:" + length() + "]";
071: }
072:
073: public Slot truncate(int requiredLength) {
074: return new Slot(_address, requiredLength);
075: }
076:
077: public static int MARSHALLED_LENGTH = Const4.INT_LENGTH * 2;
078:
079: public int compareByAddress(Slot slot) {
080: int res = slot._address - _address;
081: if (res != 0) {
082: return res;
083: }
084: return slot.length() - length();
085: }
086:
087: public int compareByLength(Slot slot) {
088: int res = slot.length() - length();
089: if (res != 0) {
090: return res;
091: }
092: return slot._address - _address;
093: }
094:
095: public boolean isDirectlyPreceding(Slot other) {
096: return _address + length() == other._address;
097: }
098:
099: public Slot append(Slot slot) {
100: return new Slot(address(), _length + slot.length());
101: }
102:
103: }
|