Skip to content

Commit 2fc1b48

Browse files
author
rcartwright
committed
This revision streamlines the implementation of the getLength
operation on ODD's; the new implementation uses the _image stored in the DDReconstructor for the tdocument if the DefintionsDocument does not exist (eliminating the construction of the DefinitionsDocument). The revision also simplifies the implementation of the newFile() operation in AbstractGlobalModel.java. The following files were modified: M src/edu/rice/cs/drjava/model/cache/DocumentCache.java M src/edu/rice/cs/drjava/model/cache/DocumentCacheTest.java M src/edu/rice/cs/drjava/model/cache/DCacheAdapter.java M src/edu/rice/cs/drjava/model/DefaultGlobalModel.java M src/edu/rice/cs/drjava/model/repl/newjvm/MainJVM.java M src/edu/rice/cs/drjava/model/AbstractGlobalModel.java M src/edu/rice/cs/drjava/CommandLineTest.java M src/edu/rice/cs/drjava/ui/DefinitionsPane.java M src/edu/rice/cs/drjava/ui/AbstractDJPane.java M src/edu/rice/cs/util/NullFile.java git-svn-id: file:///tmp/test-svn/trunk@4179 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 3138149 commit 2fc1b48

10 files changed

Lines changed: 151 additions & 146 deletions

File tree

drjava/src/edu/rice/cs/drjava/CommandLineTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public void testOpenOne() throws BadLocationException {
227227
// _log.log("got OpenDefDocs");
228228
assertEquals("Only one document opened?", 1, docs.size());
229229
OpenDefinitionsDocument doc = docs.get(0);
230+
// System.err.println("Doc text = " + doc.getText());
230231
assertEquals("Correct length of file?", f1_contents.length(), doc.getLength());
231232
// _log.log("Ready to perform getText operation");
232233
assertEquals("Do the contents match?", f1_contents, doc.getText(0,f1_contents.length()));

drjava/src/edu/rice/cs/drjava/model/AbstractGlobalModel.java

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,8 @@ public JavadocModel getJavadocModel() {
10221022
* @return The new open document
10231023
*/
10241024
public OpenDefinitionsDocument newFile(File parentDir) {
1025-
final ConcreteOpenDefDoc doc = _createOpenDefinitionsDocument();
1025+
final ConcreteOpenDefDoc doc = _createOpenDefinitionsDocument(new NullFile());
10261026
doc.setParentDirectory(parentDir);
1027-
doc.setFile(new NullFile());
10281027
addDocToNavigator(doc);
10291028
_notifier.newFileCreated(doc);
10301029
return doc;
@@ -2765,8 +2764,8 @@ public int getMaximumSize() {
27652764
*/
27662765
ConcreteOpenDefDoc(File f) { this(f, f.getParentFile(), f.lastModified()); }
27672766

2768-
/* Standard constructor for a new document (no associated file). */
2769-
ConcreteOpenDefDoc() { this(null, null, 0L); }
2767+
/* Standard constructor for a new document (associated file is NullFile which does not exit in file system). */
2768+
ConcreteOpenDefDoc(NullFile f) { this(f, null, 0L); }
27702769

27712770
/* General constructor. Only used privately. */
27722771
private ConcreteOpenDefDoc(File f, File dir, long stamp) {
@@ -2778,9 +2777,8 @@ private ConcreteOpenDefDoc(File f, File dir, long stamp) {
27782777
_id = ID_COUNTER++;
27792778

27802779
try {
2781-
// System.out.println("about to make reconstructor " + this);
27822780
DDReconstructor ddr = makeReconstructor();
2783-
// System.out.println("finished making reconstructor " + this);
2781+
// System.err.println("Registering " + this);
27842782
_cacheAdapter = _cache.register(this, ddr);
27852783
} catch(IllegalStateException e) { throw new UnexpectedException(e); }
27862784

@@ -2894,7 +2892,8 @@ public String getMainClassName() throws ClassNameNotFoundException {
28942892

28952893
/** Returns the name of this file, or "(Untitled)" if no file. */
28962894
public String getFileName() {
2897-
if (isUntitled()) return "(Untitled)";
2895+
if (_file == null) return "(Untitled)";
2896+
// if (isUntitled()) return "(Untitled)";
28982897
return _file.getName();
28992898
}
29002899

@@ -3030,19 +3029,18 @@ protected DDReconstructor makeReconstructor() {
30303029
new WeakHashMap<DefinitionsDocument.WrappedPosition, Integer>();
30313030

30323031
public String getText() {
3033-
final String image = _image;
3032+
String image = _image;
30343033
if (image != null) return image;
30353034

30363035
// Document has not yet been read from disk; read it and set _image before returning text.
30373036
// Synchronization on this was eliminated because it does not prevent the returned string from becoming
30383037
// inconsistent with _doc/_file in the presence of huge scheduling delays. Of course, all getText operations
30393038
// can return stale data in the presence of such delays.
3040-
try {
3041-
_image = IOUtil.toString(_file);
3042-
return _image;
3043-
}
3044-
catch(IOException e) { /* do nothing; return null */ }
3045-
return null;
3039+
try { image = IOUtil.toString(_file); }
3040+
catch(IOException e) { image = ""; }
3041+
// System.err.println("Returning image '" + image + " for file " + _file);
3042+
_image = image;
3043+
return _image;
30463044
}
30473045

30483046
public DefinitionsDocument make() throws IOException, BadLocationException, FileMovedException {
@@ -3051,16 +3049,15 @@ public DefinitionsDocument make() throws IOException, BadLocationException, File
30513049
DefinitionsDocument newDefDoc = new DefinitionsDocument(_notifier);
30523050
newDefDoc.setOpenDefDoc(ConcreteOpenDefDoc.this);
30533051

3054-
if (_image != null) {
3055-
_editorKit.read(new StringReader(_image), newDefDoc, 0);
3056-
_log.log("Reading from image for " + _file + " containing " + _image.length() + " chars");
3057-
}
3058-
else if (! isUntitled()) {
3059-
final InputStreamReader reader = new FileReader(_file);
3060-
_editorKit.read(reader, newDefDoc, 0);
3061-
reader.close(); // win32 needs readers closed explicitly!
3062-
}
3063-
_loc = Math.min(_loc, newDefDoc.getLength()); // make sure not past end
3052+
/* Initialize doc contents */
3053+
String image = getText();
3054+
if (image.length() > 0) newDefDoc.insertString(0, image, null); // Do not call insertString on an empty doc
3055+
// else if (! isUntitled()) {
3056+
// final InputStreamReader reader = new FileReader(_file);
3057+
// _editorKit.read(reader, newDefDoc, 0);
3058+
// reader.close(); // win32 needs readers closed explicitly!
3059+
// }
3060+
_loc = Math.min(_loc, image.length()); // make sure not past end
30643061
_loc = Math.max(_loc, 0); // make sure not less than 0
30653062
newDefDoc.setCurrentLocation(_loc);
30663063
for (DocumentListener d : _list) {
@@ -3151,7 +3148,7 @@ public void saveDocInfo(DefinitionsDocument doc) {
31513148
String text = doc.getText();
31523149
if (text.length() > 0) {
31533150
_image = text;
3154-
_log.log("Saving image containing " + _image.length() + " chars for " + _file);
3151+
// _log.log("Saving image containing " + _image.length() + " chars for " + _file);
31553152
}
31563153
_loc = doc.getCurrentLocation();
31573154
_list = doc.getDocumentListeners();
@@ -3302,7 +3299,6 @@ public void saveTo(OutputStream os) throws IOException {
33023299
}
33033300
}
33043301

3305-
33063302
/** This method tells the document to prepare all the DrJavaBook and PagePrinter objects. */
33073303
public void preparePrintJob() throws BadLocationException, FileMovedException {
33083304
String fileName = "(Untitled)";
@@ -3321,7 +3317,6 @@ public void print() throws PrinterException, BadLocationException, FileMovedExce
33213317
cleanUpPrintJob();
33223318
}
33233319

3324-
33253320
/** throws UnsupportedOperationException */
33263321
public void startCompile() throws IOException {
33273322
throw new UnsupportedOperationException("AbstractGlobalModel does not support compilation");
@@ -3356,7 +3351,7 @@ public boolean isModifiedSinceSave() {
33563351
else return false;
33573352
}
33583353

3359-
public void documentSaved() { _cacheAdapter.documentSaved(getFileName()); }
3354+
public void documentSaved() { _cacheAdapter.documentSaved(); }
33603355

33613356
public void documentModified() { _cacheAdapter.documentModified(); }
33623357

@@ -3372,7 +3367,6 @@ public boolean modifiedOnDisk() {
33723367
return ret;
33733368
}
33743369

3375-
33763370
/** Determines if document has a class file consistent with its current state. If this document is unmodified,
33773371
* this method examines the primary class file corresponding to this document and compares the timestamps of
33783372
* the class file to that of the source file. An empty untitled document is consider to be "in sync".
@@ -3734,7 +3728,7 @@ File _getSourceRoot(String packageName) throws InvalidPackageException {
37343728
}
37353729

37363730
public String toString() { return getFileName(); }
3737-
3731+
37383732
/** Orders ODDs by their id's. */
37393733
public int compareTo(OpenDefinitionsDocument o) { return _id - o.id(); }
37403734

@@ -3772,13 +3766,7 @@ public Position createPosition(int offs) throws BadLocationException {
37723766

37733767
public Position getEndPosition() { return getDocument().getEndPosition(); }
37743768

3775-
public int getLength() {
3776-
// synchronized(_cache._cacheLock) { // lock down the cache
3777-
// if (_cacheAdapter.isReady() || _image == null)
3778-
return getDocument().getLength();
3779-
// return _image.length();
3780-
// }
3781-
}
3769+
public int getLength() { return _cacheAdapter.getLength(); }
37823770

37833771
public Object getProperty(Object key) { return getDocument().getProperty(key); }
37843772

@@ -4070,13 +4058,14 @@ private static class TrivialFSS implements FileSaveSelector {
40704058
public boolean shouldSaveAfterFileMoved(OpenDefinitionsDocument doc, File oldFile) { return true; }
40714059
}
40724060

4073-
/** Creates a ConcreteOpenDefDoc for a new DefinitionsDocument.
4061+
/** Creates a ConcreteOpenDefDoc for a NullFile object f (corresponding to a new empty document)
40744062
* @return OpenDefinitionsDocument object for a new document
40754063
*/
4076-
protected ConcreteOpenDefDoc _createOpenDefinitionsDocument() { return new ConcreteOpenDefDoc(); }
4064+
protected ConcreteOpenDefDoc _createOpenDefinitionsDocument(NullFile f) { return new ConcreteOpenDefDoc(f); }
40774065

4078-
/** Creates a ConcreteOpenDefDoc for a given file f
4066+
/** Creates a ConcreteOpenDefDoc for an existing file f.
40794067
* @return OpenDefinitionsDocument object for f
4068+
* @throws FileNotFoundException if file f does not exist
40804069
*/
40814070
protected ConcreteOpenDefDoc _createOpenDefinitionsDocument(File f) throws IOException {
40824071
if (! f.exists()) throw new FileNotFoundException("file " + f + " cannot be found");

drjava/src/edu/rice/cs/drjava/model/DefaultGlobalModel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import edu.rice.cs.util.FileOpenSelector;
6262
import edu.rice.cs.drjava.model.FileSaveSelector;
6363
import edu.rice.cs.util.FileOps;
64+
import edu.rice.cs.util.NullFile;
6465
import edu.rice.cs.util.OperationCanceledException;
6566
import edu.rice.cs.util.UnexpectedException;
6667
import edu.rice.cs.util.newjvm.AbstractMasterJVM;
@@ -461,7 +462,7 @@ class ConcreteOpenDefDoc extends AbstractGlobalModel.ConcreteOpenDefDoc {
461462
ConcreteOpenDefDoc(File f) { super(f); }
462463

463464
/* Standard constructor for a new document (no associated file) */
464-
ConcreteOpenDefDoc() { super(); }
465+
ConcreteOpenDefDoc(NullFile f) { super(f); }
465466

466467
/** Starting compiling this document. Used only for unit testing */
467468
public void startCompile() throws IOException { _compilerModel.compile(ConcreteOpenDefDoc.this); }
@@ -551,7 +552,7 @@ public void removeFromDebugger() {
551552
/** Creates a ConcreteOpenDefDoc for a new DefinitionsDocument.
552553
* @return OpenDefinitionsDocument object for a new document
553554
*/
554-
protected ConcreteOpenDefDoc _createOpenDefinitionsDocument() { return new ConcreteOpenDefDoc(); }
555+
protected ConcreteOpenDefDoc _createOpenDefinitionsDocument(NullFile f) { return new ConcreteOpenDefDoc(f); }
555556

556557
/** Creates a ConcreteOpenDefDoc for a given file f
557558
* @return OpenDefinitionsDocument object for f

drjava/src/edu/rice/cs/drjava/model/cache/DCacheAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ public interface DCacheAdapter {
5050
*/
5151
public DefinitionsDocument getDocument() throws IOException, FileMovedException;
5252

53+
public int getLength();
54+
5355
/* Gets the entire text of this document. */
5456
public String getText();
5557

@@ -71,7 +73,7 @@ public interface DCacheAdapter {
7173
public void addDocumentListener(DocumentListener l);
7274

7375
/* Method for notifying the DCacheAdapter that this document has been saved to a file. */
74-
public void documentSaved(String fileName);
76+
public void documentSaved();
7577

7678
/* Method for notifying the DCacheAdapter that this document has been modified. */
7779
public void documentModified();

drjava/src/edu/rice/cs/drjava/model/cache/DocumentCache.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public DocumentCache(int size) {
103103
* @return an adapter that allows its owner to access its definitions document
104104
*/
105105
public DCacheAdapter register(OpenDefinitionsDocument odd, DDReconstructor rec) {
106-
DocManager mgr = new DocManager(rec, odd.toString(), odd.isUntitled());
106+
DocManager mgr = new DocManager(rec, odd.isUntitled());
107107
notifyRegistrationListeners(odd, mgr); // runs synchronously; only used in tests
108108
// System.err.println("register(" + odd + ", " + rec + ") called");
109109
return mgr;
@@ -136,6 +136,7 @@ private void kickOut(DocManager[] removed) {
136136
public int getCacheSize() { return CACHE_SIZE; }
137137
public int getNumInCache() { return _residentQueue.size(); }
138138

139+
public String toString() { return _residentQueue.toString(); }
139140

140141

141142
///////////////////////////// DocManager //////////////////////////
@@ -155,18 +156,17 @@ private class DocManager implements DCacheAdapter {
155156
private final DDReconstructor _rec;
156157
private volatile int _stat; // I know, this is not very OO
157158
private volatile DefinitionsDocument _doc;
158-
private volatile String _fileName;
159159

160160
/** Instantiates a manager for the documents that are produced by the given document reconstructor.
161161
* @param rec The reconstructor used to create the document
162162
*/
163-
public DocManager(DDReconstructor rec, String fn, boolean isUntitled) {
163+
public DocManager(DDReconstructor rec, boolean isUntitled) {
164164
// Utilities.showDebug("DocManager(" + rec + ", " + fn + ", " + isUntitled + ")");
165165
_rec = rec;
166166
if (isUntitled) _stat = UNTITLED;
167167
else _stat = NOT_IN_QUEUE;
168168
_doc = null;
169-
_fileName = fn;
169+
// System.err.println(this + " constructed");
170170
}
171171

172172
/** Adds DocumentListener to the reconstructor. */
@@ -180,13 +180,15 @@ private DefinitionsDocument makeDocument() {
180180
}
181181
catch(Exception e) { throw new UnexpectedException(e); }
182182
// Utilities.showDebug("Document " + _doc + " reconstructed; _stat = " + _stat);
183+
// System.err.println("Making document for " + this);
183184
if (_stat == NOT_IN_QUEUE) add(); // add this to queue
184185
return _doc;
185186
}
186187

187-
/** Gets the physical document (DD) for this manager. If DD is not in memory, it loads it into memory and returns
188-
* it. If the document has been modified in memory since it was last fetched, make it "unmanaged", removing it from
189-
* the queue. It will remain in memory until saved. If a document is not in the queue, add it.
188+
/** Gets the physical document (DD) for this manager. If DD is not in memory, it loads it from its image in its
189+
* DDReconstructor and returns it. If the document has been modified in memory since it was last fetched, make
190+
* it "unmanaged", removing it from the queue. It will remain in memory until saved. If a document is not in
191+
* the queue, add it.
190192
* @return the physical document that is managed by this adapter
191193
*/
192194
public DefinitionsDocument getDocument() throws IOException, FileMovedException {
@@ -201,17 +203,26 @@ public DefinitionsDocument getDocument() throws IOException, FileMovedException
201203
}
202204
}
203205

206+
/** Gets the length of this document using (i) cached _doc or (ii) reconstructor (which may force the document
207+
* to be loaded. */
208+
public int getLength() {
209+
final DefinitionsDocument doc = _doc; // create a snapshot of _doc
210+
if (doc != null) return doc.getLength();
211+
return _rec.getText().length(); // There is a technical race here; _doc could be set and modified before here
212+
}
213+
214+
204215
/** Gets the text of this document using in order of preference (i) cached _doc; (ii) cached reconstructor _image;
205216
* and (iii) the document after forcing it to be loaded. */
206217
public String getText() {
207218
final DefinitionsDocument doc = _doc; // create a snapshot of _doc
208219
if (doc != null) return doc.getText();
209-
String image = _rec.getText(); // There is a technical race here; _doc could be set and modified before here
210-
if (image != null) return image;
211-
synchronized(_cacheLock) { // lock the state of this DocManager
212-
if (_doc != null) return _doc.getText(); // _doc may have changed since test outside of _cacheLock
213-
return makeDocument().getText();
214-
}
220+
return _rec.getText(); // There is a technical race here; _doc could be set and modified before here
221+
// if (image != null) return image;
222+
// synchronized(_cacheLock) { // lock the state of this DocManager
223+
// if (_doc != null) return _doc.getText(); // _doc may have changed since test outside of _cacheLock
224+
// return makeDocument().getText();
225+
// }
215226
}
216227

217228
/* Gets the specified substring of this document; throws an exception if the specification is ill-formed. */
@@ -250,11 +261,11 @@ public void documentReset() {
250261
}
251262

252263
/** Updates status of this document in the cache. */
253-
public void documentSaved(String fileName) {
254-
// Utilities.showDebug("Document " + _doc + " has been saved as " + fileName);
264+
public void documentSaved() {
265+
// Utilities.showDebug("Document " + _doc + " has been saved");
266+
// System.err.println("Document " + _doc + " has been saved");
255267
synchronized(_cacheLock) { // lock the document manager so that document manager fields can be updated
256268
if (isUnmanagedOrUntitled()) {
257-
_fileName = fileName;
258269
add(); // add formerly unmanaged/untitled document to queue
259270
}
260271
}
@@ -263,6 +274,7 @@ public void documentSaved(String fileName) {
263274
/** Adds this DocManager to the queue and sets status to IN_QUEUE. Assumes _cacheLock is already held. */
264275
private void add() {
265276
// Utilities.showDebug("add " + this + " to the QUEUE\n" + "QUEUE = " + _residentQueue);
277+
// System.err.println("adding " + this + " to the QUEUE\n" + "QUEUE = " + _residentQueue);
266278
if (! _residentQueue.contains(this)) {
267279
_residentQueue.add(this);
268280
_stat = IN_QUEUE;
@@ -304,7 +316,7 @@ private void kickOut(boolean isClosing) {
304316
_stat = NOT_IN_QUEUE;
305317
}
306318

307-
public String toString() { return _fileName; }
319+
public String toString() { return "DocManager for " + _rec.toString() + "[stat = " + _stat + "]"; }
308320
}
309321

310322
////////////////////////////////////////

0 commit comments

Comments
 (0)