KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > RegistryObject


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.registry;
12
13 /**
14  * An object which has the general characteristics of all the nestable elements
15  * in a plug-in manifest.
16  */

17 public abstract class RegistryObject implements KeyedElement {
18     //Object identifier
19
private int objectId = RegistryObjectManager.UNKNOWN;
20     //The children of the element
21
protected int[] children = RegistryObjectManager.EMPTY_INT_ARRAY;
22
23     // The field combines offset, persistence flag, and no offset flag
24
private int extraDataOffset = EMPTY_MASK;
25
26     // it is assumed that int has 32 bits (bits #0 to #31);
27
// bits #0 - #29 are the offset (limited to about 1Gb)
28
// bit #30 - persistance flag
29
// bit #31 - registry object has no extra data offset
30
// the bit#31 is a sign bit; bit#30 is the highest mantissa bit
31
static final int EMPTY_MASK = 0x80000000; // only taking bit #31
32
static final int PERSIST_MASK = 0x40000000; // only taking bit #30
33
static final int OFFSET_MASK = 0x3FFFFFFF; // all bits but #30, #31
34

35     //The registry that owns this object
36
protected ExtensionRegistry registry;
37
38     protected RegistryObject(ExtensionRegistry registry, boolean persist) {
39         this.registry = registry;
40         setPersist(persist);
41     }
42
43     void setRawChildren(int[] values) {
44         children = values;
45     }
46
47     //This can not return null. It returns the singleton empty array or an array
48
protected int[] getRawChildren() {
49         return children;
50     }
51
52     void setObjectId(int value) {
53         objectId = value;
54     }
55
56     protected int getObjectId() {
57         return objectId;
58     }
59
60     //Implementation of the KeyedElement interface
61
public int getKeyHashCode() {
62         return objectId;
63     }
64
65     public Object JavaDoc getKey() {
66         return new Integer JavaDoc(objectId);
67     }
68
69     public boolean compare(KeyedElement other) {
70         return objectId == ((RegistryObject) other).objectId;
71     }
72
73     protected boolean shouldPersist() {
74         return (extraDataOffset & PERSIST_MASK) == PERSIST_MASK;
75     }
76
77     private void setPersist(boolean persist) {
78         if (persist)
79             extraDataOffset |= PERSIST_MASK;
80         else
81             extraDataOffset &= ~PERSIST_MASK;
82     }
83
84     protected boolean noExtraData() {
85         return (extraDataOffset & EMPTY_MASK) == EMPTY_MASK;
86     }
87
88     // Convert no extra data to -1 on output
89
protected int getExtraDataOffset() {
90         if (noExtraData())
91             return -1;
92         return extraDataOffset & OFFSET_MASK;
93     }
94
95     // Accept -1 as "no extra data" on input
96
protected void setExtraDataOffset(int offset) {
97         if (offset == -1) {
98             extraDataOffset &=~OFFSET_MASK; // clear all offset bits
99
extraDataOffset |= EMPTY_MASK;
100             return;
101         }
102         
103         if ((offset & OFFSET_MASK) != offset)
104             throw new IllegalArgumentException JavaDoc("Registry object: extra data offset is out of range"); //$NON-NLS-1$
105

106         extraDataOffset &=~(OFFSET_MASK | EMPTY_MASK) ; // clear all offset bits; mark as non-empty
107
extraDataOffset |= (offset & OFFSET_MASK); // set all offset bits
108
}
109 }
110
Popular Tags