|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DocumentRegion.java | 58.3% | 70.4% | 50% | 62.7% |
|
||||||||||||||
| 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 | /** Class for a simple document region that only records region offsets, not positions. Instances of this class represent | |
| 40 | * dummy regions used in searching SortSets of DocumentRegions. Hence, getLineStart() and getLineEnd() should never be | |
| 41 | * called. WARNING: this class overrides the equals method but does not override the hashCode method to maintain | |
| 42 | * consistency. Hence, instances can only be used as keys in identity based hash tables. NOTE: since class instances | |
| 43 | * are mutable, a hashCode method consistent with equals WOULD NOT WORK anyway. | |
| 44 | * @version $Id: DocumentRegion.java 5354 2010-08-10 22:41:14Z mgricken $ | |
| 45 | */ | |
| 46 | public class DocumentRegion implements OrderedDocumentRegion { | |
| 47 | protected final OpenDefinitionsDocument _doc; | |
| 48 | // The following two fields are ignored in subclasses of DocumentRegion | |
| 49 | protected volatile int _start; | |
| 50 | protected volatile int _end; // _end >= _start | |
| 51 | ||
| 52 | /** Create a new simple document region using offsets. | |
| 53 | * @param doc document that contains this region, which cannot be null | |
| 54 | * @param file file that contains the region | |
| 55 | */ | |
| 56 | ||
| 57 | /** Create a new simple document region with a bona fide document */ | |
| 58 | 95 | public DocumentRegion(OpenDefinitionsDocument doc, int start, int end) { |
| 59 | 95 | assert doc != null; |
| 60 | 95 | assert end >= start; // split in two to help diagnose bug 2906538: AssertionError After Go To Find Result and Edit |
| 61 | 95 | _doc = doc; |
| 62 | 95 | _start = start; |
| 63 | 95 | _end = end; |
| 64 | } | |
| 65 | ||
| 66 | /** Defines the equality relation on DocumentRegions. This equivalence relation is consistent with the equivalence | |
| 67 | * relation induced by the compareTo method. | |
| 68 | */ | |
| 69 | 17 | public final boolean equals(Object o) { |
| 70 | 0 | if (o == null || ! (o instanceof IDocumentRegion)) return false; |
| 71 | 17 | IDocumentRegion r = (IDocumentRegion) o; |
| 72 | 17 | return getDocument() == r.getDocument() && getStartOffset() == r.getStartOffset() && getEndOffset() == r.getEndOffset(); |
| 73 | } | |
| 74 | ||
| 75 | /** Totally orders regions lexicographically based on (_doc, endOffset, startOffset). This method is typically applied | |
| 76 | * to regions within the same document. | |
| 77 | */ | |
| 78 | 126 | public int compareTo(OrderedDocumentRegion r) { |
| 79 | 126 | int docRel = getDocument().compareTo(r.getDocument()); |
| 80 | 0 | if (docRel != 0) return docRel; |
| 81 | // At this point, we know that this and r have identical file paths, but they do not have to be the same allocation | |
| 82 | ||
| 83 | 126 | assert getDocument() == r.getDocument(); // DrJava never creates two ODD objects with the same path |
| 84 | 126 | int end1 = getEndOffset(); |
| 85 | 126 | int end2 = r.getEndOffset(); |
| 86 | 126 | int endDiff = end1 - end2; |
| 87 | 68 | if (endDiff != 0) return endDiff; |
| 88 | ||
| 89 | 58 | int start1 = getStartOffset(); |
| 90 | 58 | int start2 = r.getStartOffset(); |
| 91 | 58 | return start1 - start2; |
| 92 | } | |
| 93 | ||
| 94 | /** WARNING: The hashCode function is left unchanged making it inconsisent with equality. Hence, only Identity based | |
| 95 | * hash tables should use this type as keys. */ | |
| 96 | ||
| 97 | /** @return the document, or null if it hasn't been established yet */ | |
| 98 | 557 | public OpenDefinitionsDocument getDocument() { return _doc; } |
| 99 | ||
| 100 | /** @return the start offset */ | |
| 101 | 158 | public int getStartOffset() { return _start; } |
| 102 | ||
| 103 | /** @return the end offset */ | |
| 104 | 252 | public int getEndOffset() { return _end; } |
| 105 | ||
| 106 | /** Throws exception indicating that getLineStart() is not supported. */ | |
| 107 | 0 | public int getLineStartOffset() { |
| 108 | 0 | throw new UnsupportedOperationException("DocumentRegion does not suppport getLineStart()"); |
| 109 | } | |
| 110 | ||
| 111 | /** Throws exception indicating that getLineEnd() is not supported. */ | |
| 112 | 0 | public int getLineEndOffset() { |
| 113 | 0 | throw new UnsupportedOperationException("DocumentRegion does not suppport getLineEnd()"); |
| 114 | } | |
| 115 | ||
| 116 | /** Throws exception indicating that getString() is not supported. */ | |
| 117 | 0 | public String getString() { |
| 118 | 0 | throw new UnsupportedOperationException("DocumentRegion does not suppport getString()"); |
| 119 | } | |
| 120 | ||
| 121 | /** Throws exception indicating that getString() is not supported. */ | |
| 122 | 0 | public void update() { |
| 123 | 0 | throw new UnsupportedOperationException("DocumentRegion does not suppport updateLines()"); |
| 124 | } | |
| 125 | ||
| 126 | 0 | public boolean isEmpty() { return getStartOffset() == getEndOffset(); } |
| 127 | ||
| 128 | 0 | public String toString() { |
| 129 | 0 | return (/* _doc != null ? */ _doc.toString() /* : "null" */) + "[" + getStartOffset() + " .. " + getEndOffset() + "]"; |
| 130 | } | |
| 131 | } |
|
||||||||||