corina
Class Range

java.lang.Object
  |
  +--corina.Range
All Implemented Interfaces:
Comparable

public class Range
extends Object
implements Comparable

This class represents a range of years which can be redated safely. It's immutable; all destructive operations on it return new Ranges. Unfortunately, this data structure violates single-instance storage: we hope that the usage of Range and Sample will always keep Sample.data.size() == Range.span(), but there aren't any built-in ways to do this, so it's up to you. The problem is that end is a duplicate of Sample.data.size() + start.

Version:
$Id: Range.java,v 1.3 2002/11/11 17:04:28 bitpoet Exp $
Author:
Ken Harris
See Also:
Year, Sample

Constructor Summary
Range()
          Construct a new empty range, starting at Year.DEFAULT.
Range(String s)
          Construct a range from a String.
Range(Year y, int span)
          Construct a range, given a starting year and span.
Range(Year y1, Year y2)
          Construct a new range, from y1 to y2.
 
Method Summary
 int compareTo(Object o)
          Compares this and o, for placing in fallback order.
 boolean contains(Year y)
          Return true if (and only if) the given year is inside the range, inclusive.
 boolean endOfRow(Year y)
          Is this year the end of a row?
 boolean equals(Object o)
          Compare two ranges for equality.
 Year getEnd()
          Get the ending year of this range.
 Year getStart()
          Get the starting year of this range.
 Range intersection(Range r)
          The intersection of this range with r.
 int overlap(Range r)
          Return the number of years overlap between this range and the given range.
 Range redateBy(int dy)
          Redate a range by a certain number of years.
 Range redateEndTo(Year y)
          Set the ending year of the range, and adjust the start year to maintain the same length.
 Range redateStartTo(Year y)
          Set the starting year of the range, and adjust the ending year to maintain the same length.
 int rows()
          Compute the number of rows this Range will take to display, assuming rows are marked off as the row() method does.
 int span()
          Return the number of years spanned by this range.
 boolean startOfRow(Year y)
          Is this year the start of a row? (Year 1 is considered the start of that row.)
 String toString()
          Return a simple string representation of the range, like "1001 - 1036".
 String toStringWithSpan()
          Return a string representation of the range, including the span, like "(1001 - 1036, n=36)".
 Range union(Range r)
          The union of this range with r.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Range

public Range()
Construct a new empty range, starting at Year.DEFAULT.
See Also:
Year

Range

public Range(Year y1,
             Year y2)
Construct a new range, from y1 to y2. (Neither year may be null.) If y2 < y1, it is an empty interval.
Parameters:
y1 - starting year
y2 - ending year

Range

public Range(Year y,
             int span)
Construct a range, given a starting year and span.
Parameters:
y - the starting year
span - the number of years

Range

public Range(String s)
Construct a range from a String.
Parameters:
s - the String
Method Detail

getStart

public Year getStart()
Get the starting year of this range.
Returns:
the starting year

getEnd

public Year getEnd()
Get the ending year of this range.
Returns:
the ending year

redateStartTo

public Range redateStartTo(Year y)
Set the starting year of the range, and adjust the ending year to maintain the same length.
Parameters:
y - new starting year for the range
See Also:
redateEndTo(corina.Year)

redateBy

public Range redateBy(int dy)
Redate a range by a certain number of years. Usually, you'll use redateStartTo() or redateEndTo(), which are more convenient.
Parameters:
dy - the number of years to shift this range by

redateEndTo

public Range redateEndTo(Year y)
Set the ending year of the range, and adjust the start year to maintain the same length.
Parameters:
y - new ending year for the range
See Also:
redateStartTo(corina.Year)

span

public int span()
Return the number of years spanned by this range. For example, the range 1001 - 1005 spans 5 years.
Returns:
the span of this range (difference between start and end, inclusive)

rows

public int rows()
Compute the number of rows this Range will take to display, assuming rows are marked off as the row() method does.
Returns:
the number of rows this range spans

toString

public String toString()
Return a simple string representation of the range, like "1001 - 1036".
Overrides:
toString in class Object
Returns:
a string representation of the range

toStringWithSpan

public String toStringWithSpan()
Return a string representation of the range, including the span, like "(1001 - 1036, n=36)".
Returns:
a string representation of the range, including span

contains

public boolean contains(Year y)
Return true if (and only if) the given year is inside the range, inclusive.
Parameters:
y - year to check
Returns:
true if y is in the range, else false

startOfRow

public boolean startOfRow(Year y)
Is this year the start of a row? (Year 1 is considered the start of that row.)
Returns:
true, iff this year is the start of a row

endOfRow

public boolean endOfRow(Year y)
Is this year the end of a row?
Returns:
true, iff this year is the end of a row

overlap

public int overlap(Range r)
Return the number of years overlap between this range and the given range.
Parameters:
r - range to compare
Returns:
number of years overlap

intersection

public Range intersection(Range r)
The intersection of this range with r. If they don't overlap, returns an empty range (1 - -1).
Parameters:
r - the range to intersect with this range
Returns:
the intersection of this and r

union

public Range union(Range r)
The union of this range with r. Since there is no concept of "range with a gap" in Corina, it assumes they overlap.
Parameters:
r - the range to union with this range
Returns:
the union of this and r

equals

public boolean equals(Object o)
Compare two ranges for equality.
Overrides:
equals in class Object
Parameters:
r - range to compare with this
Returns:
true, if the ranges are equal, else false

compareTo

public int compareTo(Object o)
Compares this and o, for placing in fallback order.
Specified by:
compareTo in interface Comparable
Parameters:
o - Object to compare
Returns:
>0, ==0, or <0 if this is greater-than, equal-to, or less-than o
Throws:
ClassCastException - if o is not a Range