KickJava   Java API By Example, From Geeks To Geeks.

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


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 class IndexedStoreContext extends IndexedStoreObject {
14
15     public static final int SIZE = 32;
16     public static final int TYPE = 2;
17
18     /*
19      The open number field is no longer used to generate object ids, but may not be deleted since a non-zero
20      open number indicates that the object number field has not been initialized.
21      */

22     private static final int OpenNumberOffset = 2;
23     private static final int OpenNumberLength = 4;
24     private Field openNumberField;
25     private int openNumber;
26
27     private static final int ObjectDirectoryAddressOffset = 6;
28     private static final int ObjectDirectoryAddressLength = 4;
29     private Field objectDirectoryAddressField;
30     private ObjectAddress objectDirectoryAddress;
31
32     private static final int IndexDirectoryAddressOffset = 10;
33     private static final int IndexDirectoryAddressLength = 4;
34     private Field indexDirectoryAddressField;
35     private ObjectAddress indexDirectoryAddress;
36
37     private static final int ObjectNumberOffset = 14;
38     private static final int ObjectNumberLength = 8;
39     private Field objectNumberField;
40     private long objectNumber;
41
42     /**
43      * Constructs a new context.
44      */

45     IndexedStoreContext() {
46         super();
47         indexDirectoryAddress = ObjectAddress.Null;
48         objectDirectoryAddress = ObjectAddress.Null;
49         openNumber = 0;
50         objectNumber = 0;
51     }
52
53     /**
54      * Constructs a context from a field read from the store.
55      */

56     IndexedStoreContext(Field f, ObjectStore store, ObjectAddress address) throws ObjectStoreException {
57         super(f, store, address);
58     }
59
60     /**
61      * Sets the fields definitions as subfields of a contents field.
62      */

63     protected void setFields(Field contents) {
64         openNumberField = contents.subfield(OpenNumberOffset, OpenNumberLength);
65         objectDirectoryAddressField = contents.subfield(ObjectDirectoryAddressOffset, ObjectDirectoryAddressLength);
66         indexDirectoryAddressField = contents.subfield(IndexDirectoryAddressOffset, IndexDirectoryAddressLength);
67         objectNumberField = contents.subfield(ObjectNumberOffset, ObjectNumberLength);
68     }
69
70     /**
71      * Places the contents of the buffer into the fields.
72      * Subclasses should implement and call super.
73      */

74     protected void extractValues(Field contents) throws ObjectStoreException {
75         super.extractValues(contents);
76         setFields(contents);
77         openNumber = openNumberField.getInt();
78         objectDirectoryAddress = new ObjectAddress(objectDirectoryAddressField.get());
79         indexDirectoryAddress = new ObjectAddress(indexDirectoryAddressField.get());
80         objectNumber = objectNumberField.getLong();
81         /* here is where we transition to using object numbers -- upward compatible change */
82         if (openNumber > 0) {
83             objectNumber = (long) openNumber << 32;
84             openNumber = 0;
85             setChanged();
86         }
87     }
88
89     /**
90      * Places the contents of the fields into the buffer.
91      * Subclasses should implement and call super.
92      */

93     protected void insertValues(Field contents) {
94         super.insertValues(contents);
95         setFields(contents);
96         openNumberField.put(openNumber);
97         objectDirectoryAddressField.put(objectDirectoryAddress);
98         indexDirectoryAddressField.put(indexDirectoryAddress);
99         objectNumberField.put(objectNumber);
100     }
101
102     /**
103      * Returns the index directory address from the buffer.
104      */

105     ObjectAddress getIndexDirectoryAddress() {
106         return indexDirectoryAddress;
107     }
108
109     /**
110      * Returns the minimum size of this object's instance -- including its type field.
111      * Subclasses should override.
112      */

113     protected int getMinimumSize() {
114         return SIZE;
115     }
116
117     /**
118      * Returns the object directory address from the buffer.
119      */

120     ObjectAddress getObjectDirectoryAddress() {
121         return objectDirectoryAddress;
122     }
123
124     /**
125      * Returns the required type of this class of object.
126      * Subclasses must override.
127      */

128     protected int getRequiredType() {
129         return TYPE;
130     }
131
132     /**
133      * Generates and returns the next object number. This is essentially the
134      * count of the number of user-defined objects generated in this store.
135      */

136     long getNextObjectNumber() {
137         objectNumber++;
138         setChanged();
139         return objectNumber;
140     }
141
142     /**
143      * Sets the index directory address.
144      */

145     void setIndexDirectoryAddress(ObjectAddress address) {
146         this.indexDirectoryAddress = address;
147         setChanged();
148     }
149
150     /**
151      * Sets the object directory address.
152      */

153     void setObjectDirectoryAddress(ObjectAddress address) {
154         this.objectDirectoryAddress = address;
155         setChanged();
156     }
157
158     /**
159      * Provides a printable representation of this object.
160      */

161     public String JavaDoc toString() {
162         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
163         b.append("Context("); //$NON-NLS-1$
164
b.append(objectNumber);
165         b.append(","); //$NON-NLS-1$
166
b.append(indexDirectoryAddress);
167         b.append(","); //$NON-NLS-1$
168
b.append(objectDirectoryAddress);
169         b.append(")"); //$NON-NLS-1$
170
return b.toString();
171     }
172
173 }
174
Popular Tags