01: package com.technoetic.xplanner.util;
02:
03: import java.io.Serializable;
04:
05: /** The half-open interval [low, high) inclusive on the low end. This class is derived
06: * from the Interval class developed at limegroup.org */
07: public class Interval implements Serializable, Comparable {
08:
09: private long low;
10: private long high;
11:
12: /** @requires low<=high */
13: public Interval(long low, long high) {
14: if (high < low) {
15: throw new IllegalArgumentException("high, low" + high
16: + ", " + low);
17: }
18: this .low = low;
19: this .high = high;
20: }
21:
22: public Interval(long singleton) {
23: this .low = singleton;
24: this .high = singleton;
25: }
26:
27: /**
28: * Compares this to another interval by the 'low' element of the interval.
29: * If the low elements are the same, then the high element is compared.
30: */
31: public int compareTo(Object o) {
32: Interval other = (Interval) o;
33: if (this .low != other.low) {
34: return (int) (this .low - other.low);
35: } else {
36: return (int) (this .high - other.high);
37: }
38: }
39:
40: /**
41: * True if this and other are adjacent, i.e. the high end of one equals the
42: * low end of the other.
43: */
44: public boolean adjacent(Interval other) {
45: return high == other.low || low == other.high;
46: }
47:
48: /**
49: * True if this and other overlap.
50: */
51: public boolean overlaps(Interval other) {
52: return (this .low < other.high && other.low < this .high)
53: || (other.low < this .high && this .low < other.high);
54: }
55:
56: public String toString() {
57: if (low == high) {
58: return String.valueOf(low);
59: } else {
60: return String.valueOf(low) + "-" + String.valueOf(high);
61: }
62: }
63:
64: public boolean equals(Object o) {
65: if (!(o instanceof Interval)) {
66: return false;
67: }
68: Interval other = (Interval) o;
69: return low == other.low && high == other.high;
70: }
71:
72: public long getLow() {
73: return low;
74: }
75:
76: public long getHigh() {
77: return high;
78: }
79: }
|