KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > filesystem > LogEntryCache


1 /*******************************************************************************
2  * Copyright (c) 2005, 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 /**
12  *
13  */

14 package org.eclipse.team.internal.ccvs.core.filesystem;
15
16 import java.util.HashMap JavaDoc;
17 import java.util.Map JavaDoc;
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     /*
29      * Cache of all log entries
30      */

31     private Map JavaDoc entries = new HashMap JavaDoc(); /* Map String:remoteFilePath->Map (String:revision -> ILogEntry) */
32
33     Map JavaDoc internalGetLogEntries(String JavaDoc path) {
34         return (Map JavaDoc) entries.get(path);
35     }
36
37     /**
38      * Return all the log entries at the given path
39      * @param path the file path
40      * @return the log entries for the file
41      */

42     public ILogEntry[] getLogEntries(String JavaDoc path) {
43         Map JavaDoc map = internalGetLogEntries(path);
44         return (ILogEntry[]) map.values().toArray(new ILogEntry[map.values().size()]);
45     }
46
47     ILogEntry internalGetLogEntry(String JavaDoc path, String JavaDoc revision) {
48         Map JavaDoc fileEntries = internalGetLogEntries(path);
49         if (fileEntries != null) {
50             return (ILogEntry) fileEntries.get(revision);
51         }
52         return null;
53     }
54
55     public String JavaDoc[] getCachedFilePaths() {
56         return (String JavaDoc[]) entries.keySet().toArray(new String JavaDoc[entries.size()]);
57     }
58
59     /**
60      * Return the log entry that for the given resource
61      * or <code>null</code> if no entry was fetched or the
62      * resource is not a file.
63      * @param getFullPath(resource) the resource
64      * @return the log entry or <code>null</code>
65      */

66     public synchronized ILogEntry getLogEntry(ICVSRemoteResource resource) {
67         if (resource instanceof ICVSRemoteFile) {
68             try {
69                 String JavaDoc path = getFullPath(resource);
70                 String JavaDoc revision = ((ICVSRemoteFile) resource).getRevision();
71                 return internalGetLogEntry(path, revision);
72             } catch (TeamException e) {
73                 // Log and return null
74
}
75         }
76         return null;
77     }
78
79     /**
80      * Return the log entries that were fetched for the given resource
81      * or an empty list if no entry was fetched.
82      * @param getFullPath(resource) the resource
83      * @return the fetched log entries or an empty list is none were found
84      */

85     public synchronized ILogEntry[] getLogEntries(ICVSRemoteResource resource) {
86         Map JavaDoc 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     /*
94      * Return the full path that uniquely identifies the resource
95      * accross repositories. This path include the repository and
96      * resource path but does not include the revision so that
97      * all log entries for a file can be retrieved.
98      */

99     String JavaDoc 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 JavaDoc revision = file.getRevision();
110         // First decrement the last digit and see if that revision exists
111
String JavaDoc predecessorRevision = getPredecessorRevision(revision);
112         ICVSRemoteFile predecessor = findRevison(allLogs, predecessorRevision);
113         // If nothing was found, try to fond the base of a branch
114
if (predecessor == null && isBrancheRevision(revision)) {
115             predecessorRevision = getBaseRevision(revision);
116             predecessor = findRevison(allLogs, predecessorRevision);
117         }
118         // If that fails, it is still possible that there is a revision.
119
// This can happen if the revision has been manually set.
120
if (predecessor == null) {
121             // We don't search in this case since this is costly and would be done
122
// for any file that is new as well.
123
}
124         return predecessor;
125     }
126
127     /*
128      * Find the given revision in the list of log entries.
129      * Return null if the revision wasn't found.
130      */

131     ICVSRemoteFile findRevison(ILogEntry[] allLogs, String JavaDoc 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     /*
143      * Decrement the trailing digit by one.
144      */

145     String JavaDoc getPredecessorRevision(String JavaDoc revision) {
146         int digits[] = Util.convertToDigits(revision);
147         digits[digits.length - 1]--;
148         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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     /*
159      * Return true if there are more than 2 digits in the revision number
160      * (i.e. the revision is on a branch)
161      */

162     boolean isBrancheRevision(String JavaDoc revision) {
163         return Util.convertToDigits(revision).length > 2;
164     }
165
166     /*
167      * Remove the trailing revision digits such that the
168      * returned revision is shorter than the given revision
169      * and is an even number of digits long
170      */

171     String JavaDoc getBaseRevision(String JavaDoc revision) {
172         int digits[] = Util.convertToDigits(revision);
173         int length = digits.length - 1;
174         if (length % 2 == 1) {
175             length--;
176         }
177         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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     /**
188      * Remove any entries for the remote resources
189      * @param resource the remote resource
190      */

191     public synchronized void clearEntries(ICVSRemoteResource resource) {
192         String JavaDoc remotePath = getFullPath(resource);
193         entries.remove(remotePath);
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener#addEntry(org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry)
198      */

199     public void handleLogEntryReceived(ILogEntry entry) {
200         ICVSRemoteFile file = entry.getRemoteFile();
201         String JavaDoc fullPath = getFullPath(file);
202         String JavaDoc revision = entry.getRevision();
203         Map JavaDoc fileEntries = internalGetLogEntries(fullPath);
204         if (fileEntries == null) {
205             fileEntries = new HashMap JavaDoc();
206             entries.put(fullPath, fileEntries);
207         }
208         fileEntries.put(revision, entry);
209     }
210 }
211
Popular Tags