1 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 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 38 public boolean deleteBytes(IResource resource) throws TeamException { 39 return flushBytes(resource, IResource.DEPTH_ZERO); 40 } 41 42 45 public void dispose() { 46 syncBytesCache.clear(); 47 membersCache.clear(); 48 } 49 50 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 72 public byte[] getBytes(IResource resource) throws TeamException { 73 byte[] syncBytes = internalGetSyncBytes(resource); 74 if (syncBytes != null && equals(syncBytes, NO_REMOTE)) { 75 return null; 77 } 78 return syncBytes; 79 } 80 81 85 public boolean isEmpty() { 86 return syncBytesCache.isEmpty(); 87 } 88 89 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 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 |