/* Interval.java

	Purpose:
		
	Description:
		
	History:
		Oct 23, 2014 10:14:15 AM, Created by henrichen

	Copyright (C) 2014 Potix Corporation. All Rights Reserved.
*/

package org.zkoss.zssex.util;

import java.io.Serializable;

/**
 * @author henri
 *
 */
/// An interval with associated [payload]. You can override [updatePayload] to
/// update this [Interval] and override [deletePayload] to indicate if really
/// delete the payload.
public class Interval<T> implements Serializable {
	private static final long serialVersionUID = 5435082250077651744L;

	/// Low value of this interval
	private int low;

	/// High value of this interval
	private int high;
	
	/// Payload of tis interval
	private T payload;
	
	public int getLow() {
		return low;
	}

	public void setLow(int low) {
		this.low = low;
	}

	public int getHigh() {
		return high;
	}

	public void setHigh(int high) {
		this.high = high;
	}

	public T getPayload() {
		return payload;
	}

	public void setPayload(T payload) {
		this.payload = payload;
	}
	
	/// Constructor
	public Interval(int low, int high, T payload) {
		this.low = low;
		this.high = high;
		this.payload = payload;
	}
	
	public Interval<T> clone() {
	  return new Interval<T>(this.low, this.high, this.payload);
	}
	
//	/// Make this an invalid Interval.
//	public void setInvalid() {
//	  low = high = -100;
//	}
//	
//	public boolean isInvalid() {
//		return low == -100;
//	}
	
	/// Return the length of this interval
	public int size() {
		return low == -100 ? 0 : (high - low + 1);
	}
	
	@Override
	public String toString() {
		return ""+getClass().getName()+"(" + low + ":" + high + ")";
	}
	
	public static class InfiniteInterval<T> extends Interval<T> {
		//Constructor
		public InfiniteInterval(int high, T payload) {
			super(0, high, payload);
		}
		
		@Override
		public void setLow(int low) {
			//do nothing
		}
		
		@Override
		public void setHigh(int high) {
			//do nothing
		}
	}
}
