home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Main.bin
/
Range.java
< prev
next >
Wrap
Text File
|
1998-10-25
|
5KB
|
151 lines
/*
* Copyright 1998 Symantec Corporation, All Rights Reserved.
*/
package com.symantec.itools.vcafe.openapi;
/**
* A byte offset and length in a <code>SourceFile</code>. It is <i>not</i> the range of the unicode
* version of that text. Note that this means that a <code>Range</code> cannot be computed arbitrarily
* from the unicode <code>StringBuffer</code> equivalent text. Ranges are meant to be passed from
* Visual Cafe to plug-ins, and not vice-versa.
*
* @see SourceFile
*
* @author Symantec Internet Tools Division
* @version 1.0
* @since VCafe 3.0
*/
public final class Range
{
/**
* Starting character position in bytes, numbering from zero.
*/
int start;
/**
* Length of range in bytes.
*/
int length;
/**
* Constructs a <code>Range</code> that starts at the specified character position (in bytes), and
* has the given length (also in bytes).
* @param start starting character position in bytes, numbering from zero.
* @param length length of the range in bytes.
*/
public Range(int start, int length) {
this.start = start;
this.length = length;
}
/**
* Constructs a <code>Range</code> that has the same start and length as the given <code>Range</code>.
* @param r the given <code>Range</code>.
*/
public Range(Range r) {
this(r.start, r.length);
}
/**
* Determines whether the range is empty (<code>Range</code> length is zero). If this is true, this
* <code>Range</code> is an insertion point, otherwise it is an area of text.
* @return <code>true</code> if the range is empty.
*/
public boolean isEmpty() {
return length == 0;
}
/**
* Gets the starting character position, in zero-based bytes.
* @return the start of this <code>Range</code> in zero-based bytes.
*/
public int getStart() {
return start;
}
/**
* Gets the length.
* @return the length of this <code>Range</code> in bytes.
*/
public int getLength() {
return length;
}
/**
* Determines the ending character position, in zero-based bytes.
* @return the ending character position in bytes, in zero-based based.
*/
public int getEnd() {
if (length == 0) return start;
return start+length-1;
}
/**
* Creates a new <code>Range</code> that contains the full extend of both this and the
* given <code>Range</code>.
* @param r the given <code>Range</code> (may be <code>null</code>).
* @return a new <code>Range</code> whose extent contains both this and the given <code>Range</code>.
*/
public Range union(Range r) {
if (r == null) return new Range(this);
int newStart, newEnd;
int currentEnd = getEnd();
newStart = (this.start < r.start) ? this.start : r.start;
newEnd = (currentEnd < r.getEnd()) ? r.getEnd() : currentEnd;
return new Range(newStart, newEnd - newStart + 1);
}
/**
* Determines whether the extent of this <code>Range</code> contains the specified offset.
* @param offset the offset, in zero-based bytes.
* @return <code>true</code> if this <code>Range</code> contains the offset, <code>false</code> otherwise.
*/
public boolean contains(int offset) {
if (offset < start) return false;
if (offset > getEnd()) return false;
return true;
}
/**
* Determines whether this <code>Range</code> fully contains the give <code>Range r</code>.
* @param r the given <code>Range</code>.
* @return <code>true</code> if the given <code>Range</code> is fully contained by this
* <code>Range</code>, <code>false</code> otherwise.
*/
public boolean contains(Range r) {
return this.contains(r.start) && this.contains(r.getEnd());
}
/**
* Determines whether this <code>Range</code> has the identical extent as the given <code>Range</code>.
* @param r the given <code>Range</code>.
* @return <code>true</code> if the extents are the same, <code>false</code> otherwise.
*/
public boolean equals(Range r) {
return (this.start == r.start) && (this.length == r.length);
}
/**
* Determines the absolute distance of the given offset from this <code>Range</code>. If the offset is
* contained within the extent of this <code>Range</code>, 0 is returned.
* @param offset the given offset
* @return the offset's absolute distance away, or 0 if the offset is contained by this <code>Range</code>.
*/
public int getDistance(int offset) {
if (offset < start) return start - offset;
offset -= getEnd();
if (offset > 0) return offset;
return 0;
}
/**
* Gets a <code>String</code> that represents this object.
* @return the <code>String</code>.
*/
public String toString() {
return "Range[" + start + "," + length + ")";
}
}