/** Numeric interval -- closed intervals
 *  Intervals have a maximum width (MAXwidth) 
 */
class Interval {

  private double base;  // low end
  private double width; // interval width
  public static final double MAXwidth= 5;  //max width of Interval
   
  /** Constructor: An Interval has a specified base and width w */
  public Interval(double base, double w) {
    this.base= base;
    setWidth(w);
  }
  
  /** =Get right end of this Interval */
  public double getEnd() { return base + width; }
  
  /** =Get base of this Interval */
  public double getBase() { return base; }
  
  /** Set width of this Interval to w */
  public void setWidth(double w) { width = Math.min(w,MAXwidth); }
  
  /** Expand this Interval by a factor of f (expand to the right) */
  public void expand(double f) {
    setWidth(width*f);
  }

  /** ={This Interval is in Interval i}
   *  If the ends of this Interval and i are exactly equal, consider
   *  this Interval to be in i.
   */
  public boolean isIn(Interval i) {
    return ( getBase()>=i.getBase() && getEnd()<=i.getEnd() );
  }

  /** =String description of this Interval */
  public String toString(){
    return "[" + getBase() + "," + getEnd() + "]";
  }
  
  /** =The overlapped Interval between Intervals a and b */
  public static Interval overlap(Interval a, Interval b) {
    Interval olap;      // overlapped interval
    double left, right; // olap's left & right

    left= Math.max(a.getBase(),b.getBase());
    right= Math.min(a.getEnd(),b.getEnd());
    if ((right-left)<=0 ) //treat overlap of width 0 as no overlap
      olap= null;
    else
      olap= new Interval(left, right-left);
    return olap;
  }
  
} //class Interval

