/** An interval represent an integer interval, from one integer low to another integer high.
    The integer low must be smaller than high */

// There is no main

class Interval implements Cloneable{
  private int from;
  private int to;

  // invariant: non-empty intervals: from < to
  // invariant: empty intervals: from >= to

  /** Return the lower limit of this interval */
  public int from(){return(from);}

  /** Return the upper limit of this interval*/
  public int to(){return(to);}

  private static Interval emptyInterval = new Interval(1,0);

  /** Construct an interval from its two limits. The lower is the first parameter*/
  public Interval(int from, int to){
    this.from = from; 
    this.to = to;
  }

  /** Return whether the interval is empty: from() is larger than to(). */
  public boolean empty(){
    return (from >= to);
  }

  /** Return the the overlapping interval of this interval and the parameter interval */
  public Interval overlapInterval (Interval other){
    try{
       if (this.empty() || other.empty() )
          return (Interval)emptyInterval.clone();
       else if (this.from > other.to || this.to < other.from)
          return (Interval)emptyInterval.clone();
       else if (this.from < other.from && other.to < this.to)
          return (Interval)other.clone();
       else if (other.from <= this.from && this.to <= other.to)
          return (Interval)this.clone();
       else if (this.from <= other.from && other.from <= this.to)
          return (new Interval(other.from, this.to));
       else if (other.from <= this.from && this.from <= other.to)
          return (new Interval(this.from, other.to));
       else return null;
    }
    catch (CloneNotSupportedException e){
       return null;
    }

  }

  /** Return a string representation of this interval */
  public String toString(){
    if (this.empty() )
      return ("Empty interval");
    else return ("Interval: [" + this.from + ", " + this.to + "]");
  }

}


