|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BrowserDocumentRegion.java | 50% | 66.7% | 50% | 59.4% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.model; | |
| 38 | ||
| 39 | import javax.swing.text.Position; | |
| 40 | import javax.swing.tree.DefaultMutableTreeNode; | |
| 41 | import java.io.File; | |
| 42 | ||
| 43 | /** Class for document regions that totally ordered by allocation chronology. They do not conform to the invariants | |
| 44 | * required for OrderedDocumentRegions. | |
| 45 | * Warning: this class defines compareTo which implicitly defines a coarser equality relation than equals | |
| 46 | * @version $Id: BrowserDocumentRegion.java 5425 2011-02-03 06:46:51Z rcartwright $ | |
| 47 | */ | |
| 48 | public class BrowserDocumentRegion implements IDocumentRegion, Comparable<BrowserDocumentRegion> { | |
| 49 | private static volatile int _indexCounter = 0; // sequence number counter for browser regions | |
| 50 | private final int _index; // unique sequence number for this region | |
| 51 | protected final OpenDefinitionsDocument _doc; // document for this region | |
| 52 | protected final File _file; // file for this region | |
| 53 | protected Position _startPosition; // start position for this region | |
| 54 | protected Position _endPosition; // final position for this region | |
| 55 | protected volatile DefaultMutableTreeNode _treeNode; | |
| 56 | ||
| 57 | /** Create a new simple document region with a bona fide document. | |
| 58 | * @param doc document that contains this region | |
| 59 | * @param file file that contains the region | |
| 60 | * @param sp start position of the region | |
| 61 | * @param ep end position of the region | |
| 62 | */ | |
| 63 | 28 | public BrowserDocumentRegion(OpenDefinitionsDocument doc, Position sp, Position ep) { |
| 64 | 28 | assert doc != null; |
| 65 | 28 | _index = _indexCounter++; // sequence number records allocation order |
| 66 | 28 | _doc = doc; |
| 67 | 28 | _file = doc.getRawFile(); // don't check the validity of _file here |
| 68 | 28 | _startPosition = sp; |
| 69 | 28 | _endPosition = ep; |
| 70 | 28 | _treeNode = null; |
| 71 | } | |
| 72 | ||
| 73 | /* Relying on default equals operation. */ | |
| 74 | ||
| 75 | /** This hash function is consistent with equals. */ | |
| 76 | 21 | public int hashCode() { return _index; } |
| 77 | ||
| 78 | /** This relation is coarset than equals. */ | |
| 79 | 0 | public int compareTo(BrowserDocumentRegion r) { return _index - r._index; } |
| 80 | ||
| 81 | 0 | public int getIndex() { return _index; } |
| 82 | ||
| 83 | /** @return the document. */ | |
| 84 | 56 | public OpenDefinitionsDocument getDocument() { return _doc; } |
| 85 | ||
| 86 | /** @return the file. */ | |
| 87 | 0 | public File getFile() { return _file; } |
| 88 | ||
| 89 | /** @return the start offset */ | |
| 90 | 14 | public int getStartOffset() { return _startPosition.getOffset(); } |
| 91 | ||
| 92 | /** @return the end offset */ | |
| 93 | 0 | public int getEndOffset() { return _endPosition.getOffset(); } |
| 94 | ||
| 95 | /* The following are commented out because a Position object includes a reference to the corresponding object, | |
| 96 | * preventing it from being garbage-collected. We cannot retain any Position objects for documents that have | |
| 97 | * been kicked out the document cache assuming we want such documents to be garbage-collected (which is the | |
| 98 | * primary motivation for creating the document cache). | |
| 99 | */ | |
| 100 | ||
| 101 | // /** @return the start position */ | |
| 102 | // public Position getStartPosition() { return _startPosition; } | |
| 103 | ||
| 104 | // /** @return the end offset */ | |
| 105 | // public Position getEndPosition() { return _endPosition; } | |
| 106 | ||
| 107 | 7 | public void update(BrowserDocumentRegion other) { |
| 108 | 0 | if (other.getDocument() != _doc) throw new IllegalArgumentException("Regions must have the same document."); |
| 109 | 7 | _startPosition = other._startPosition; |
| 110 | 7 | _endPosition = other._endPosition; |
| 111 | } | |
| 112 | ||
| 113 | 0 | public String toString() { |
| 114 | 0 | return _doc.toString() + "[" + getStartOffset() + "]"; |
| 115 | } | |
| 116 | } |
|
||||||||||