1

我写了一个类来检查两个整数间隔是否重叠。但是我不太喜欢这个解决方案。我认为有可能以更好和更简单的方式做到这一点。

public class IntegerInterval implements Interval {
private Integer start;
private Integer end;

public IntegerInterval(Integer start, Integer end) {
    this.start = start;
    this.end = end;
}

public Integer getStart() {
    return start;
}

public Integer getEnd() {
    return end;
}

public boolean isOverlapping(IntegerInterval other) {
    if (other != null) {
        return isInInterval(start, other) || isInInterval(end, other)
                || isInInterval(other.start, this) || isInInterval(other.end, this);
    }
    return false;
}

public boolean isInInterval(Integer number, IntegerInterval interval) {
    if (number != null && interval != null) {
        if(interval.getStart() == null && interval.getEnd() != null) {
            return number.intValue() <= interval.getEnd().intValue();
        }
        if(interval.getStart() != null && interval.getEnd() == null) {
            return number.intValue() >= interval.getStart().intValue();
        }
        if(interval.getStart() == null && interval.getEnd() == null) {
            return true;
        }
        return interval.getStart() <= number && number <= interval.getEnd();
    }
    else if(number == null && interval != null) {
        return interval.getStart() == null && interval.getEnd() == null;
    }
    return false;
}

}

4

3 回答 3

3

下面的代码应该更简单:

public boolean isOverlapping(IntegerInterval other) {
    if (other == null) return false; // for readability's sake, this condition is pulled out

    // overlap happens ONLY when this's end is on the right of other's start
    // AND this's start is on the left of other's end.
    return (((this.end == null) || (other.start == null) || (this.end.intValue() >= other.start.intValue())) &&
        ((this.start == null) || (other.end == null) || (this.start.intValue() <= other.end.intValue())));
}

更新如果Date按照@Adam 实际询问的方式进行比较,代码将是:

private static boolean dateRangesAreOverlaping(Date start1, Date end1,
                                               Date start2, Date end2) {
return (((end1 == null) || (start2 == null) || end1.after(start2)) &&
         ((start1 == null) || (end2 == null) || start1.before(end2)));

}

于 2013-01-08T08:46:53.633 回答
2

您应该将其包装startend一个Comparable能够封装null. 这样你只需要调用compareToinisInInterval而不需要打扰null.

该类也可以明确表示正无穷和负无穷。

编辑:
如果您<T extends Comparable<T>>在类声明中添加类型参数并将类型startend类型声明为,Comparable<T>那么您可以使用任何Comparable与您一起实现的类型Interval,而不仅仅是Integer.

于 2013-01-08T08:41:34.830 回答
1

假设start < end. start相对于的位置应该有 3 个检查other:左、中和右(右是为了完整性,因为不可能有交集)。所以这里有2个剩余的检查:

 (start <= other.start && end >= other.start) || 
 (start >= other.start && start <= other.end)  
 // start > other.end means no intersection as end > start > other.end

如果您检查 as 的位置,start那么if第二个 chech 可以是(start <= other.end)

 if (start <= other.start) return end >= other.start;
 else if (start <= other.end) return true;
 else return false;

根据您的需要调整“=”部分并null适当地添加您的检查(即使用 SpaceTrucker 答案来制作隐藏在类内的 null 的comaprison)。

于 2013-01-08T08:40:41.523 回答