import java.security.InvalidParameterException;
import java.util.Date;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
public class SomeSpan<T extends Comparable<T>> {
static Logger log = Logger.getLogger(SomeSpan.class);
private T begin;
private T end;
public SomeSpan(T begin, T end) {
setBegin(begin);
setEnd(end);
}
/**
* @param args
*/
public static void main(String[] args) {
BasicConfigurator.configure();
SomeSpan<Date> t1 = new SomeSpan<Date>(
new Date(1979 - 1900, 8 - 1, 17), new Date(2011 - 1900, 1 - 1,
26));
SomeSpan<Date> t2 = new SomeSpan<Date>(
new Date(2000 - 1900, 10 - 1, 5), new Date(2011 - 1900, 1 - 1,
25));
SomeSpan<Integer> t3 = new SomeSpan<Integer>(new Integer(2000),
new Integer(2011));
SomeSpan<Integer> t4 = new SomeSpan<Integer>(new Integer(1000),
new Integer(2000));
log.debug(t1);
log.debug(t2);
log.debug(t3);
log.debug(t4);
log.debug(t1.isInsection(t2));
log.debug(t3.isDisjoint(t4));
}
public void setBegin(T begin) {
this.begin = begin;
}
public T getBegin() {
return begin;
}
public void setEnd(T end) {
if (end.compareTo(this.getBegin()) < 0) {
throw new InvalidParameterException("Error: end < begin!");
}
this.end = end;
}
public T getEnd() {
return end;
}
/**
* 判断是否相离,相离的定义参见 http://en.wikipedia.org/wiki/Disjoint_sets
*/
public boolean isDisjoint(SomeSpan<T> another) {
return (this.getBegin().compareTo(another.getEnd()) > 0)
|| (this.getEnd().compareTo(another.getBegin()) < 0);
}
/**
* 判断是否相交,相交的定义参见 http://en.wikipedia.org/wiki/Intersection_%28set_theory%29
*/
public boolean isInsection(SomeSpan<T> another) {
return !isDisjoint(another);
}
@Override
public String toString() {
return getBegin().toString() + " -> " + getEnd().toString();
}
}
没有评论:
发表评论