KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Observable JavaDoc;
14
15 public abstract class StoredObject extends Observable JavaDoc implements Referable, Insertable {
16
17     public static final int MAXIMUM_OBJECT_SIZE = ObjectStore.MAXIMUM_OBJECT_SIZE;
18     public static final int TYPE_OFFSET = 0;
19     public static final int TYPE_LENGTH = 2;
20
21     protected ObjectStore store;
22     protected ObjectAddress address;
23     protected int referenceCount;
24     protected int type;
25
26     /**
27      * Constructs a new object so that it can be stored.
28      */

29     protected StoredObject() {
30         type = getRequiredType();
31     }
32
33     /**
34      * Constructs a new instance from a field.
35      */

36     protected StoredObject(Field f, ObjectStore store, ObjectAddress address) throws ObjectStoreException {
37         if (f.length() < getMinimumSize()) {
38             throw new ObjectStoreException(ObjectStoreException.ObjectSizeFailure);
39         }
40         if (f.length() > getMaximumSize()) {
41             throw new ObjectStoreException(ObjectStoreException.ObjectSizeFailure);
42         }
43         extractValues(f);
44         setStore(store);
45         setAddress(address);
46     }
47
48     /**
49      * Provides a printable representation of this object. Subclasses must implement.
50      */

51     public abstract String JavaDoc toString();
52
53     /**
54      * Returns the required type of this class of object.
55      * Subclasses must override.
56      */

57     protected abstract int getRequiredType();
58
59     /**
60      * Returns a byte array value of the object.
61      */

62     public final byte[] toByteArray() {
63         Field f = new Field(length());
64         insertValues(f);
65         return f.get();
66     }
67
68     /**
69      * Adds a reference.
70      */

71     public final int addReference() {
72         referenceCount++;
73         return referenceCount;
74     }
75
76     /**
77      * Removes a reference.
78      */

79     public final int removeReference() {
80         if (referenceCount > 0)
81             referenceCount--;
82         return referenceCount;
83     }
84
85     /**
86      * Tests for existing references.
87      */

88     public final boolean hasReferences() {
89         return referenceCount > 0;
90     }
91
92     /**
93      * Returns the address of the object.
94      * Subclasses must not override.
95      */

96     public final ObjectAddress getAddress() {
97         return address;
98     }
99
100     public final void setStore(ObjectStore store) {
101         this.store = store;
102     }
103
104     public final void setAddress(ObjectAddress address) {
105         this.address = address;
106     }
107
108     /**
109      * Places the contents of the buffer into the members.
110      * Subclasses should implement and call super.
111      */

112     protected void extractValues(Field f) throws ObjectStoreException {
113         type = f.subfield(TYPE_OFFSET, TYPE_LENGTH).getInt();
114         if (type != getRequiredType())
115             throw new ObjectStoreException(ObjectStoreException.ObjectTypeFailure);
116     }
117
118     /**
119      * Places the contents of the fields into the buffer.
120      * Subclasses should implement and call super.
121      */

122     protected void insertValues(Field f) {
123         f.subfield(TYPE_OFFSET, TYPE_LENGTH).put(type);
124     }
125
126     /**
127      * Returns the maximum size of this object's instance -- including its type field.
128      * Subclasses can override. The default is to have the this be equal to the minimum
129      * size, forcing a fixed size object.
130      */

131     protected int getMaximumSize() {
132         return getMinimumSize();
133     }
134
135     /**
136      * Returns the minimum size of this object's instance -- including its type field.
137      * Subclasses should override.
138      */

139     protected int getMinimumSize() {
140         return 2;
141     }
142
143     /**
144      * Returns the actual size of this object's instance -- including its type field.
145      * Subclasses should override.
146      */

147     protected int length() {
148         return getMinimumSize();
149     }
150 }
151
Popular Tags