Author: adrianc
Date: Tue Sep 30 19:34:16 2008 New Revision: 700660 URL: http://svn.apache.org/viewvc?rev=700660&view=rev Log: Completed Timestamp support in DateRange. Also added some comparison methods. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java?rev=700660&r1=700659&r2=700660&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java Tue Sep 30 19:34:16 2008 @@ -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. @@ -36,10 +37,10 @@ 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); } } @@ -63,10 +64,18 @@ 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); } @@ -76,6 +85,7 @@ } public boolean includesDate(Date date) { + date = downcastTimestamp(date); if (isPoint()) { return date.equals(this.start); } @@ -86,6 +96,56 @@ } } + 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); } @@ -97,6 +157,8 @@ 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); } @@ -112,4 +174,11 @@ 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; + } } |
Free forum by Nabble | Edit this page |