1 11 14 package org.eclipse.team.internal.ccvs.core.filesystem; 15 16 import java.util.HashMap ; 17 import java.util.Map ; 18 19 import org.eclipse.team.core.TeamException; 20 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile; 21 import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource; 22 import org.eclipse.team.internal.ccvs.core.ILogEntry; 23 import org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener; 24 import org.eclipse.team.internal.ccvs.core.util.Util; 25 26 class LogEntryCache implements ILogEntryListener { 27 28 31 private Map entries = new HashMap (); 32 33 Map internalGetLogEntries(String path) { 34 return (Map ) entries.get(path); 35 } 36 37 42 public ILogEntry[] getLogEntries(String path) { 43 Map map = internalGetLogEntries(path); 44 return (ILogEntry[]) map.values().toArray(new ILogEntry[map.values().size()]); 45 } 46 47 ILogEntry internalGetLogEntry(String path, String revision) { 48 Map fileEntries = internalGetLogEntries(path); 49 if (fileEntries != null) { 50 return (ILogEntry) fileEntries.get(revision); 51 } 52 return null; 53 } 54 55 public String [] getCachedFilePaths() { 56 return (String []) entries.keySet().toArray(new String [entries.size()]); 57 } 58 59 66 public synchronized ILogEntry getLogEntry(ICVSRemoteResource resource) { 67 if (resource instanceof ICVSRemoteFile) { 68 try { 69 String path = getFullPath(resource); 70 String revision = ((ICVSRemoteFile) resource).getRevision(); 71 return internalGetLogEntry(path, revision); 72 } catch (TeamException e) { 73 } 75 } 76 return null; 77 } 78 79 85 public synchronized ILogEntry[] getLogEntries(ICVSRemoteResource resource) { 86 Map fileEntries = internalGetLogEntries(getFullPath(resource)); 87 if (fileEntries != null) { 88 return (ILogEntry[]) fileEntries.values().toArray(new ILogEntry[fileEntries.size()]); 89 } 90 return new ILogEntry[0]; 91 } 92 93 99 String getFullPath(ICVSRemoteResource resource) { 100 return Util.appendPath(resource.getRepository().getLocation(false), resource.getRepositoryRelativePath()); 101 } 102 103 public synchronized void clearEntries() { 104 entries.clear(); 105 } 106 107 public synchronized ICVSRemoteFile getImmediatePredecessor(ICVSRemoteFile file) throws TeamException { 108 ILogEntry[] allLogs = getLogEntries(file); 109 String revision = file.getRevision(); 110 String predecessorRevision = getPredecessorRevision(revision); 112 ICVSRemoteFile predecessor = findRevison(allLogs, predecessorRevision); 113 if (predecessor == null && isBrancheRevision(revision)) { 115 predecessorRevision = getBaseRevision(revision); 116 predecessor = findRevison(allLogs, predecessorRevision); 117 } 118 if (predecessor == null) { 121 } 124 return predecessor; 125 } 126 127 131 ICVSRemoteFile findRevison(ILogEntry[] allLogs, String predecessorRevision) throws TeamException { 132 for (int i = 0; i < allLogs.length; i++) { 133 ILogEntry entry = allLogs[i]; 134 ICVSRemoteFile file = entry.getRemoteFile(); 135 if (file.getRevision().equals(predecessorRevision)) { 136 return file; 137 } 138 } 139 return null; 140 } 141 142 145 String getPredecessorRevision(String revision) { 146 int digits[] = Util.convertToDigits(revision); 147 digits[digits.length - 1]--; 148 StringBuffer buffer = new StringBuffer (revision.length()); 149 for (int i = 0; i < digits.length; i++) { 150 buffer.append(Integer.toString(digits[i])); 151 if (i < digits.length - 1) { 152 buffer.append('.'); 153 } 154 } 155 return buffer.toString(); 156 } 157 158 162 boolean isBrancheRevision(String revision) { 163 return Util.convertToDigits(revision).length > 2; 164 } 165 166 171 String getBaseRevision(String revision) { 172 int digits[] = Util.convertToDigits(revision); 173 int length = digits.length - 1; 174 if (length % 2 == 1) { 175 length--; 176 } 177 StringBuffer buffer = new StringBuffer (revision.length()); 178 for (int i = 0; i < length; i++) { 179 buffer.append(Integer.toString(digits[i])); 180 if (i < length - 1) { 181 buffer.append('.'); 182 } 183 } 184 return buffer.toString(); 185 } 186 187 191 public synchronized void clearEntries(ICVSRemoteResource resource) { 192 String remotePath = getFullPath(resource); 193 entries.remove(remotePath); 194 } 195 196 199 public void handleLogEntryReceived(ILogEntry entry) { 200 ICVSRemoteFile file = entry.getRemoteFile(); 201 String fullPath = getFullPath(file); 202 String revision = entry.getRevision(); 203 Map fileEntries = internalGetLogEntries(fullPath); 204 if (fileEntries == null) { 205 fileEntries = new HashMap (); 206 entries.put(fullPath, fileEntries); 207 } 208 fileEntries.put(revision, entry); 209 } 210 } 211 | Popular Tags |