KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > ResourceInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.resources;
12
13 import java.io.*;
14 import java.util.Map JavaDoc;
15 import org.eclipse.core.internal.localstore.FileStoreRoot;
16 import org.eclipse.core.internal.utils.*;
17 import org.eclipse.core.internal.watson.IElementTreeData;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.QualifiedName;
20
21 /**
22  * A data structure containing the in-memory state of a resource in the workspace.
23  */

24 public class ResourceInfo implements IElementTreeData, ICoreConstants, IStringPoolParticipant {
25     protected static final int LOWER = 0xFFFF;
26     protected static final int UPPER = 0xFFFF0000;
27
28     /**
29      * This field stores the resource modification stamp in the lower two bytes,
30      * and the character set generation count in the higher two bytes.
31      */

32     protected volatile int charsetAndContentId = 0;
33
34     /**
35      * The file system root that this resource is stored in
36      */

37     protected FileStoreRoot fileStoreRoot;
38
39     /** Set of flags which reflect various states of the info (used, derived, ...). */
40     protected int flags = 0;
41
42     /** Local sync info */
43     // thread safety: (Concurrency004)
44
protected volatile long localInfo = I_NULL_SYNC_INFO;
45
46     /**
47      * This field stores the sync info generation in the lower two bytes, and
48      * the marker generation count in the upper two bytes.
49      */

50     protected volatile int markerAndSyncStamp;
51
52     /** The collection of markers for this resource. */
53     protected MarkerSet markers = null;
54
55     /** Modification stamp */
56     protected long modStamp = 0;
57
58     /** Unique node identifier */
59     // thread safety: (Concurrency004)
60
protected volatile long nodeId = 0;
61
62     /**
63      * The properties which are maintained for the lifecycle of the workspace.
64      * <p>
65      * This field is declared as the implementing class rather than the
66      * interface so we ensure that we get it right since we are making certain
67      * assumptions about the object type w.r.t. casting.
68      */

69     protected ObjectMap sessionProperties = null;
70
71     /**
72      * The table of sync information.
73      * <p>
74      * This field is declared as the implementing class rather than the
75      * interface so we ensure that we get it right since we are making certain
76      * assumptions about the object type w.r.t. casting.
77      */

78     protected ObjectMap syncInfo = null;
79
80     /**
81      * Returns the integer value stored in the indicated part of this info's flags.
82      */

83     protected static int getBits(int flags, int mask, int start) {
84         return (flags & mask) >> start;
85     }
86
87     /**
88      * Returns the type setting for this info. Valid values are
89      * FILE, FOLDER, PROJECT,
90      */

91     public static int getType(int flags) {
92         return getBits(flags, M_TYPE, M_TYPE_START);
93     }
94
95     /**
96      * Returns true if all of the bits indicated by the mask are set.
97      */

98     public static boolean isSet(int flags, int mask) {
99         return (flags & mask) == mask;
100     }
101
102     /**
103      * Clears all of the bits indicated by the mask.
104      */

105     public void clear(int mask) {
106         flags &= ~mask;
107     }
108
109     public void clearModificationStamp() {
110         modStamp = IResource.NULL_STAMP;
111     }
112
113     public synchronized void clearSessionProperties() {
114         sessionProperties = null;
115     }
116
117     public Object JavaDoc clone() {
118         try {
119             return super.clone();
120         } catch (CloneNotSupportedException JavaDoc e) {
121             return null; // never gets here.
122
}
123     }
124
125     public int getCharsetGenerationCount() {
126         return charsetAndContentId >> 16;
127     }
128
129     public int getContentId() {
130         return charsetAndContentId & LOWER;
131     }
132
133     public FileStoreRoot getFileStoreRoot() {
134         return fileStoreRoot;
135     }
136
137     /**
138      * Returns the set of flags for this info.
139      */

140     public int getFlags() {
141         return flags;
142     }
143
144     /**
145      * Gets the local-relative sync information.
146      */

147     public long getLocalSyncInfo() {
148         return localInfo;
149     }
150
151     /**
152      * Returns the marker generation count.
153      * The count is incremented whenever markers on the resource change.
154      */

155     public int getMarkerGenerationCount() {
156         return markerAndSyncStamp >> 16;
157     }
158
159     /**
160      * Returns a copy of the collection of makers on this resource.
161      * <code>null</code> is returned if there are none.
162      */

163     public MarkerSet getMarkers() {
164         return getMarkers(true);
165     }
166
167     /**
168      * Returns the collection of makers on this resource.
169      * <code>null</code> is returned if there are none.
170      */

171     public MarkerSet getMarkers(boolean makeCopy) {
172         if (markers == null)
173             return null;
174         return makeCopy ? (MarkerSet) markers.clone() : markers;
175     }
176
177     public long getModificationStamp() {
178         return modStamp;
179     }
180
181     public long getNodeId() {
182         return nodeId;
183     }
184
185     /**
186      * Returns the property store associated with this info. The return value may be null.
187      */

188     public Object JavaDoc getPropertyStore() {
189         return null;
190     }
191
192     /**
193      * Returns the value of the identified session property
194      */

195     public Object JavaDoc getSessionProperty(QualifiedName name) {
196         // thread safety: (Concurrency001)
197
Map JavaDoc temp = sessionProperties;
198         if (temp == null)
199             return null;
200         return temp.get(name);
201     }
202
203     /**
204      * The parameter to this method is the implementing class rather than the
205      * interface so we ensure that we get it right since we are making certain
206      * assumptions about the object type w.r.t. casting.
207      */

208     public synchronized ObjectMap getSyncInfo(boolean makeCopy) {
209         if (syncInfo == null)
210             return null;
211         return makeCopy ? (ObjectMap) syncInfo.clone() : syncInfo;
212     }
213
214     public synchronized byte[] getSyncInfo(QualifiedName id, boolean makeCopy) {
215         // thread safety: (Concurrency001)
216
byte[] b;
217         if (syncInfo == null)
218             return null;
219         b = (byte[]) syncInfo.get(id);
220         return b == null ? null : (makeCopy ? (byte[]) b.clone() : b);
221     }
222
223     /**
224      * Returns the sync information generation count.
225      * The count is incremented whenever sync info on the resource changes.
226      */

227     public int getSyncInfoGenerationCount() {
228         return markerAndSyncStamp & LOWER;
229     }
230
231     /**
232      * Returns the type setting for this info. Valid values are
233      * FILE, FOLDER, PROJECT,
234      */

235     public int getType() {
236         return getType(flags);
237     }
238
239     /**
240      * Increments the charset generation count.
241      * The count is incremented whenever the encoding on the resource changes.
242      */

243     public void incrementCharsetGenerationCount() {
244         //increment high order bits
245
charsetAndContentId = ((charsetAndContentId + LOWER + 1) & UPPER) + (charsetAndContentId & LOWER);
246     }
247
248     /**
249      * Mark this resource info as having changed content
250      */

251     public void incrementContentId() {
252         //increment low order bits
253
charsetAndContentId = (charsetAndContentId & UPPER) + ((charsetAndContentId + 1) & LOWER);
254     }
255
256     /**
257      * Increments the marker generation count.
258      * The count is incremented whenever markers on the resource change.
259      */

260     public void incrementMarkerGenerationCount() {
261         //increment high order bits
262
markerAndSyncStamp = ((markerAndSyncStamp + LOWER + 1) & UPPER) + (markerAndSyncStamp & LOWER);
263     }
264
265     /**
266      * Change the modification stamp to indicate that this resource has changed.
267      * The exact value of the stamp doesn't matter, as long as it can be used to
268      * distinguish two arbitrary resource generations.
269      */

270     public void incrementModificationStamp() {
271         modStamp++;
272     }
273
274     /**
275      * Increments the sync information generation count.
276      * The count is incremented whenever sync info on the resource changes.
277      */

278     public void incrementSyncInfoGenerationCount() {
279         //increment low order bits
280
markerAndSyncStamp = (markerAndSyncStamp & UPPER) + ((markerAndSyncStamp + 1) & LOWER);
281     }
282
283     /**
284      * Returns true if all of the bits indicated by the mask are set.
285      */

286     public boolean isSet(int mask) {
287         return (flags & mask) == mask;
288     }
289
290     public void readFrom(int newFlags, DataInput input) throws IOException {
291         // The flags for this info are read by the visitor (flattener).
292
// See Workspace.readElement(). This allows the reader to look ahead
293
// and see what type of info is being loaded.
294
this.flags = newFlags;
295         localInfo = input.readLong();
296         nodeId = input.readLong();
297         charsetAndContentId = input.readInt() & LOWER;
298         modStamp = input.readLong();
299     }
300
301     /**
302      * Sets all of the bits indicated by the mask.
303      */

304     public void set(int mask) {
305         flags |= mask;
306     }
307
308     /**
309      * Sets the value of the indicated bits to be the given value.
310      */

311     protected void setBits(int mask, int start, int value) {
312         int baseMask = mask >> start;
313         int newValue = (value & baseMask) << start;
314         // thread safety: (guarantee atomic assignment)
315
int temp = flags;
316         temp &= ~mask;
317         temp |= newValue;
318         flags = temp;
319     }
320
321     public void setFileStoreRoot(FileStoreRoot fileStoreRoot) {
322         this.fileStoreRoot = fileStoreRoot;
323     }
324
325     /**
326      * Sets the flags for this info.
327      */

328     protected void setFlags(int value) {
329         flags = value;
330     }
331
332     /**
333      * Sets the local-relative sync information.
334      */

335     public void setLocalSyncInfo(long info) {
336         localInfo = info;
337     }
338
339     /**
340      * Sets the collection of makers for this resource.
341      * <code>null</code> is passed in if there are no markers.
342      */

343     public void setMarkers(MarkerSet value) {
344         markers = value;
345     }
346
347     /**
348      * Sets the resource modification stamp.
349      */

350     public void setModificationStamp(long value) {
351         this.modStamp = value;
352     }
353
354     /**
355      *
356      */

357     public void setNodeId(long id) {
358         nodeId = id;
359     }
360
361     /**
362      * Sets the property store associated with this info. The value may be null.
363      */

364     public void setPropertyStore(Object JavaDoc value) {
365         // needs to be implemented on subclasses
366
}
367
368     /**
369      * Sets the identified session property to the given value. If
370      * the value is null, the property is removed.
371      */

372     public synchronized void setSessionProperty(QualifiedName name, Object JavaDoc value) {
373         // thread safety: (Concurrency001)
374
if (value == null) {
375             if (sessionProperties == null)
376                 return;
377             ObjectMap temp = (ObjectMap) sessionProperties.clone();
378             temp.remove(name);
379             if (temp.isEmpty())
380                 sessionProperties = null;
381             else
382                 sessionProperties = temp;
383         } else {
384             ObjectMap temp = sessionProperties;
385             if (temp == null)
386                 temp = new ObjectMap(5);
387             else
388                 temp = (ObjectMap) sessionProperties.clone();
389             temp.put(name, value);
390             sessionProperties = temp;
391         }
392     }
393
394     /**
395      * The parameter to this method is the implementing class rather than the
396      * interface so we ensure that we get it right since we are making certain
397      * assumptions about the object type w.r.t. casting.
398      */

399     protected void setSyncInfo(ObjectMap syncInfo) {
400         this.syncInfo = syncInfo;
401     }
402
403     public synchronized void setSyncInfo(QualifiedName id, byte[] value) {
404         if (value == null) {
405             //delete sync info
406
if (syncInfo == null)
407                 return;
408             syncInfo.remove(id);
409             if (syncInfo.isEmpty())
410                 syncInfo = null;
411         } else {
412             //add sync info
413
if (syncInfo == null)
414                 syncInfo = new ObjectMap(5);
415             syncInfo.put(id, value.clone());
416         }
417     }
418
419     /**
420      * Sets the type for this info to the given value. Valid values are
421      * FILE, FOLDER, PROJECT
422      */

423     public void setType(int value) {
424         setBits(M_TYPE, M_TYPE_START, value);
425     }
426
427     /* (non-Javadoc
428      * Method declared on IStringPoolParticipant
429      */

430     public void shareStrings(StringPool set) {
431         ObjectMap map = syncInfo;
432         if (map != null)
433             map.shareStrings(set);
434         map = sessionProperties;
435         if (map != null)
436             map.shareStrings(set);
437         MarkerSet markerSet = markers;
438         if (markerSet != null)
439             markerSet.shareStrings(set);
440     }
441
442     public void writeTo(DataOutput output) throws IOException {
443         // The flags for this info are written by the visitor (flattener).
444
// See SaveManager.writeElement(). This allows the reader to look ahead
445
// and see what type of info is being loaded.
446
output.writeLong(localInfo);
447         output.writeLong(nodeId);
448         output.writeInt(getContentId());
449         output.writeLong(modStamp);
450     }
451 }
452
Popular Tags