Skip to content

Commit

Permalink
Completed Timestamp support in DateRange. Also added some comparison …
Browse files Browse the repository at this point in the history
…methods.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@700660 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
adrian-crum committed Oct 1, 2008
1 parent a7f9d85 commit 702b426
Showing 1 changed file with 71 additions and 2 deletions.
73 changes: 71 additions & 2 deletions framework/base/src/org/ofbiz/base/util/DateRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.ofbiz.base.util;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

/** An immutable range of dates.
Expand All @@ -36,10 +37,10 @@ protected DateRange() {}

public DateRange(Date start, Date end) {
if (start != null) {
this.start = new Date(start.getTime());
this.start = downcastTimestamp(start);
}
if (end != null) {
this.end = new Date(end.getTime());
this.end = downcastTimestamp(end);
}
}

Expand All @@ -63,10 +64,18 @@ public Date start() {
return (Date) this.start.clone();
}

public Timestamp startStamp() {
return new Timestamp(this.start.getTime());
}

public Date end() {
return (Date) this.end.clone();
}

public Timestamp endStamp() {
return new Timestamp(this.end.getTime());
}

public boolean isAscending() {
return this.end.after(this.start) && !this.end.equals(this.start);
}
Expand All @@ -76,6 +85,7 @@ public boolean isPoint() {
}

public boolean includesDate(Date date) {
date = downcastTimestamp(date);
if (isPoint()) {
return date.equals(this.start);
}
Expand All @@ -86,6 +96,56 @@ public boolean includesDate(Date date) {
}
}

public boolean before(Date date) {
date = downcastTimestamp(date);
if (isAscending() || isPoint()) {
return this.start.before(date);
} else {
return this.end.before(date);
}
}

public boolean before(DateRange range) {
if (isAscending() || isPoint()) {
if (range.isAscending()) {
return this.end.before(range.start);
} else {
return this.end.before(range.end);
}
} else {
if (range.isAscending()) {
return this.start.before(range.start);
} else {
return this.start.before(range.end);
}
}
}

public boolean after(Date date) {
date = downcastTimestamp(date);
if (isAscending() || isPoint()) {
return this.start.after(date);
} else {
return this.end.after(date);
}
}

public boolean after(DateRange range) {
if (isAscending() || isPoint()) {
if (range.isAscending()) {
return this.start.after(range.end);
} else {
return this.start.after(range.start);
}
} else {
if (range.isAscending()) {
return this.end.after(range.end);
} else {
return this.end.after(range.start);
}
}
}

public boolean intersectsRange(DateRange dateRange) {
return intersectsRange(dateRange.start, dateRange.end);
}
Expand All @@ -97,6 +157,8 @@ public boolean intersectsRange(Date start, Date end) {
if (end == null) {
throw new IllegalArgumentException("end argument cannot be null");
}
start = downcastTimestamp(start);
end = downcastTimestamp(end);
if (isPoint()) {
return end.equals(start) && this.start.equals(start);
}
Expand All @@ -112,4 +174,11 @@ public boolean intersectsRange(Date start, Date end) {
return (this.end.equals(start) || start.after(this.end)) && (this.start.equals(end) || end.before(this.start));
}
}

protected Date downcastTimestamp(Date date) {
if (date instanceof Timestamp) {
date = new Date(date.getTime());
}
return date;
}
}

0 comments on commit 702b426

Please sign in to comment.