KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.team.core.variants;
12
13 import java.util.*;
14
15 import org.eclipse.core.resources.IContainer;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.team.core.TeamException;
19
20 /**
21  * A <code>ResourceVariantByteStore</code> that caches the variant bytes in memory
22  * and does not persist them over workbench invocations.
23  * <p>
24  * This class is not intended to be subclassed by clients.
25  *
26  * @since 3.0
27  */

28 public class SessionResourceVariantByteStore extends ResourceVariantByteStore {
29
30     private static final byte[] NO_REMOTE = new byte[0];
31     private Map membersCache = new HashMap();
32     
33     private Map syncBytesCache = new HashMap();
34
35     /* (non-Javadoc)
36      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#deleteBytes(org.eclipse.core.resources.IResource)
37      */

38     public boolean deleteBytes(IResource resource) throws TeamException {
39         return flushBytes(resource, IResource.DEPTH_ZERO);
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#dispose()
44      */

45     public void dispose() {
46         syncBytesCache.clear();
47         membersCache.clear();
48     }
49
50     /* (non-Javadoc)
51      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#flushBytes(org.eclipse.core.resources.IResource, int)
52      */

53     public boolean flushBytes(IResource resource, int depth) throws TeamException {
54         if (getSyncBytesCache().containsKey(resource)) {
55             if (depth != IResource.DEPTH_ZERO) {
56                 IResource[] members = members(resource);
57                 for (int i = 0; i < members.length; i++) {
58                     IResource child = members[i];
59                     flushBytes(child, (depth == IResource.DEPTH_INFINITE) ? IResource.DEPTH_INFINITE: IResource.DEPTH_ZERO);
60                 }
61             }
62             getSyncBytesCache().remove(resource);
63             internalRemoveFromParent(resource);
64             return true;
65         }
66         return false;
67     }
68
69     /* (non-Javadoc)
70      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#getBytes(org.eclipse.core.resources.IResource)
71      */

72     public byte[] getBytes(IResource resource) throws TeamException {
73         byte[] syncBytes = internalGetSyncBytes(resource);
74         if (syncBytes != null && equals(syncBytes, NO_REMOTE)) {
75             // If it is known that there is no remote, return null
76
return null;
77         }
78         return syncBytes;
79     }
80
81     /**
82      * Return <code>true</code> if no bytes are contained in this tree.
83      * @return <code>true</code> if no bytes are contained in this tree.
84      */

85     public boolean isEmpty() {
86         return syncBytesCache.isEmpty();
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#members(org.eclipse.core.resources.IResource)
91      */

92     public IResource[] members(IResource resource) {
93         List members = (List)membersCache.get(resource);
94         if (members == null) {
95             return new IResource[0];
96         }
97         return (IResource[]) members.toArray(new IResource[members.size()]);
98     }
99
100     /* (non-Javadoc)
101      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#setBytes(org.eclipse.core.resources.IResource, byte[])
102      */

103     public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
104         Assert.isNotNull(bytes);
105         byte[] oldBytes = internalGetSyncBytes(resource);
106         if (oldBytes != null && equals(oldBytes, bytes)) return false;
107         internalSetSyncInfo(resource, bytes);
108         return true;
109     }
110     
111     private Map getSyncBytesCache() {
112         return syncBytesCache;
113     }
114     
115     private void internalAddToParent(IResource resource) {
116         IContainer parent = resource.getParent();
117         if (parent == null) return;
118         List members = (List)membersCache.get(parent);
119         if (members == null) {
120             members = new ArrayList();
121             membersCache.put(parent, members);
122         }
123         members.add(resource);
124     }
125     
126     private byte[] internalGetSyncBytes(IResource resource) {
127         return (byte[])getSyncBytesCache().get(resource);
128     }
129
130     private void internalRemoveFromParent(IResource resource) {
131         IContainer parent = resource.getParent();
132         List members = (List)membersCache.get(parent);
133         if (members != null) {
134             members.remove(resource);
135             if (members.isEmpty()) {
136                 membersCache.remove(parent);
137             }
138         }
139     }
140     
141     private void internalSetSyncInfo(IResource resource, byte[] bytes) {
142         getSyncBytesCache().put(resource, bytes);
143         internalAddToParent(resource);
144     }
145 }
146
Popular Tags