KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > ObjectAddress


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.indexing;
12
13 public class ObjectAddress implements Insertable {
14
15     public static final int Size = 4;
16     public static ObjectAddress Null = new ObjectAddress(0, 0);
17     private static final int PageNumberOffset = 0;
18     private static final int ObjectNumberOffset = 3;
19     private int pageNumber;
20     private int objectNumber;
21
22     /**
23      * Constructor for an address from a four byte field.
24      */

25     public ObjectAddress(byte[] b) throws IllegalArgumentException JavaDoc {
26         if (b.length != Size)
27             throw new IllegalArgumentException JavaDoc();
28         Buffer buf = new Buffer(b);
29         pageNumber = buf.getUInt(PageNumberOffset, 3);
30         objectNumber = buf.getUInt(ObjectNumberOffset, 1);
31     }
32
33     /**
34      * Constructs an address from its constituent page and object numbers.
35      */

36     public ObjectAddress(int pageNumber, int objectNumber) throws IllegalArgumentException JavaDoc {
37         if (pageNumber == 0 && objectNumber == 0) {
38             this.pageNumber = 0;
39             this.objectNumber = 0;
40             return;
41         }
42         if (pageNumber < 0 || pageNumber > 0xFFFFFF)
43             throw new IllegalArgumentException JavaDoc();
44         if (pageNumber % ObjectStorePage.SIZE == 0)
45             throw new IllegalArgumentException JavaDoc();
46         if (objectNumber < 0 || objectNumber > 0xFF)
47             throw new IllegalArgumentException JavaDoc();
48         this.pageNumber = pageNumber;
49         this.objectNumber = objectNumber;
50     }
51
52     /**
53      * Returns true if and only if the addresses are equal.
54      */

55     public boolean equals(Object JavaDoc anObject) {
56         if (!(anObject instanceof ObjectAddress))
57             return false;
58         ObjectAddress address = (ObjectAddress) anObject;
59         if (pageNumber != address.pageNumber)
60             return false;
61         if (objectNumber != address.objectNumber)
62             return false;
63         return true;
64     }
65
66     /**
67      * Returns the object number from the address.
68      */

69     public int getObjectNumber() {
70         return objectNumber;
71     }
72
73     /**
74      * Returns the page number from the address.
75      */

76     public int getPageNumber() {
77         return pageNumber;
78     }
79
80     /**
81      * Returns an int representing the hash code for the address.
82      */

83     public int hashCode() {
84         return (pageNumber << 8) | objectNumber;
85     }
86
87     /**
88      * Tests the address for the null address value.
89      */

90     public boolean isNull() {
91         return (pageNumber == 0 && objectNumber == 0);
92     }
93
94     /**
95      * Returns a byte array form of the address.
96      */

97     public byte[] toByteArray() {
98         Buffer buf = new Buffer(Size);
99         buf.put(PageNumberOffset, 3, pageNumber);
100         buf.put(ObjectNumberOffset, 1, objectNumber);
101         return buf.get();
102     }
103 }
104
Popular Tags