KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > variants > ResourceVariantByteStore


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.team.core.variants;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IWorkspaceRunnable;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.team.core.TeamException;
17
18 /**
19  * The purpose of a <code>ResourceVariantByteStore</code> is to support the caching of
20  * the synchronization bytes for the resource variants that represent
21  * a resource line-up of interest such as a version, baseline or branch. The
22  * cache stores bytes in order to minimize the memory footprint of the tree. It is the
23  * responsibility of the client of this API to cache enough bytes to meaningfully identify
24  * a resource variant (and possibly create an {@link IResourceVariant} handle from them).
25  * <p>
26  * The bytes for a resource variant are accessed using the local <code>IResource</code> handle
27  * that corresponds to the resource variant (using the <code>getBytes</code> method).
28  * The potential children of a resource variant are also accessed
29  * by using the local handle that corresponds to the resource variant
30  * (using the <code>members</code> method).
31  *
32  * @since 3.0
33  */

34 public abstract class ResourceVariantByteStore {
35
36     /**
37      * Dispose of any cached sync bytes when this cache is no longer needed.
38      */

39     public abstract void dispose();
40     
41     /**
42      * Return the bytes for the variant corresponding the given local resource.
43      * A return value of <code>null</code> means that no bytes have been stored
44      * for the resource variant. It is up to the client to determine whether
45      * this means that the resource variant does not exist or that it has not been
46      * fetched or otherwise determined yet.
47      * @param resource the local resource
48      * @return the bytes that represent the resource's variant
49      * @throws TeamException
50      */

51     public abstract byte[] getBytes(IResource resource) throws TeamException;
52     
53     /**
54      * Set the bytes for the variant corresponding the given local resource.
55      * The bytes should never be <code>null</code>. If it is known that the remote
56      * does not exist, <code>deleteBytes(IResource)</code> should be used instead.
57      * If the sync bytes for the remote are stale and should be removed,
58      * <code>flushBytes(IResouce, int)</code> should be called.
59      * @param resource the local resource
60      * @param bytes the bytes that represent the resource's variant
61      * @return <code>true</code> if the bytes changed
62      * @throws TeamException
63      */

64     public abstract boolean setBytes(IResource resource, byte[] bytes) throws TeamException;
65     
66     /**
67      * Remove the bytes from the tree for the resource variants corresponding to the
68      * given local resource and its descendants to the given depth.
69      * After the bytes are removed, <code>getBytes(resource)</code> will
70      * return <code>null</code> for the affected resources.
71      * @param resource the local resource
72      * @param depth the depth of the operation (one of <code>IResource.DEPTH_ZERO</code>,
73      * <code>IResource.DEPTH_ONE</code>, or <code>IResource.DEPTH_INFINITE</code>)
74      * @return <code>true</code> if there were bytes present which were removed
75      * @throws TeamException
76      */

77     public abstract boolean flushBytes(IResource resource, int depth) throws TeamException;
78     
79     /**
80      * Method called to indicate that it is known that there is no variant associated
81      * with the local resource. Subclasses may handle this information in different ways.
82      * The <code>flush(IResource, int)</code> method should be used in the cases
83      * where a client wishes to remove bytes for other reason.
84      * @param resource the local resource
85      * @return <code>true</code> if this changes the bytes for the variant
86      */

87     public abstract boolean deleteBytes(IResource resource) throws TeamException;
88     
89     /**
90      * Return the children of the given resource that have resource variants in this tree.
91      * @param resource the parent resource
92      * @return the members who have resource variants in this tree.
93      */

94     public abstract IResource[] members(IResource resource) throws TeamException;
95     
96     /**
97      * Helper method to compare two byte arrays for equality
98      * @param syncBytes1 the first byte array or <code>null</code>
99      * @param syncBytes2 the second byte array or <code>null</code>
100      * @return whether the two arrays are equal (i.e. same content)
101      */

102     protected boolean equals(byte[] syncBytes1, byte[] syncBytes2) {
103         if (syncBytes1 == null) {
104             return syncBytes2 == null;
105         } else if (syncBytes2 == null) {
106             return false;
107         }
108         if (syncBytes1.length != syncBytes2.length) return false;
109         for (int i = 0; i < syncBytes1.length; i++) {
110             if (syncBytes1[i] != syncBytes2[i]) return false;
111         }
112         return true;
113     }
114
115     /**
116      * Run the given action which may contain multiple modifications
117      * to the byte store. By default, the action is run. Subclasses
118      * may override to obtain scheduling rules or batch deltas (if
119      * the byte store modifies workspace resources).
120      * @param root the root resource for all modifications
121      * @param runnable the action to perform
122      * @param monitor a progress monitor.
123      * @exception TeamException if the operation failed.
124      * @exception OperationCanceledException if the operation is canceled.
125      */

126     public void run(IResource root, IWorkspaceRunnable runnable, IProgressMonitor monitor) throws TeamException {
127         try {
128             runnable.run(monitor);
129         } catch (CoreException e) {
130             throw TeamException.asTeamException(e);
131         }
132     }
133 }
134
Popular Tags