|
Author: adrianc
Date: Sun Jan 24 00:04:22 2010 New Revision: 902512 URL: http://svn.apache.org/viewvc?rev=902512&view=rev Log: Refactored DateRange.java: 1. Extends new class ComparableRange. 2. downcastTimestamp protected method renamed to timestampToDate and made static. 3. Misc code cleanups. 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=902512&r1=902511&r2=902512&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 Sun Jan 24 00:04:22 2010 @@ -22,71 +22,53 @@ import java.sql.Timestamp; import java.util.Date; -/** An immutable range of dates. */ +/** An immutable range of dates. This class is here for backward compatibility - + * new code should use <code>ComparableRange<Date></code> instead. + */ @SuppressWarnings("serial") -public class DateRange implements Serializable { +public class DateRange extends ComparableRange<Date> implements Serializable { /** A <code>Date</code> instance initialized to the earliest possible date.*/ public static final Date MIN_DATE = UtilDateTime.getEarliestDate(); /** A <code>Date</code> instance initialized to the latest possible date.*/ public static final Date MAX_DATE = UtilDateTime.getLatestDate(); /** A <code>DateRange</code> instance initialized to the widest possible range of dates.*/ - public static final DateRange FullRange = new DateRange(); + public static final DateRange FullRange = new DateRange(MIN_DATE, MAX_DATE); - protected Date start = MIN_DATE; - protected Date end = MAX_DATE; - protected DateRange() {} + protected static Date timestampToDate(Date date) { + // Testing for equality between a Date instance and a Timestamp instance + // will always return false. + if (date instanceof Timestamp) { + return new Date(date.getTime()); + } + return date; + } /** * @param start If null, defaults to <a href="#MIN_DATE">MIN_DATE</a> * @param end If null, defaults to <a href="#MAX_DATE">MAX_DATE</a> */ public DateRange(Date start, Date end) { - if (start != null) { - this.start = new Date(start.getTime()); - } - if (end != null) { - this.end = new Date(end.getTime()); - } - } - - /** Returns this range's duration as a millisecond value. - * @return Range duration in milliseconds - */ - public long durationInMillis() { - if (this.end.after(this.start)) { - return this.end.getTime() - this.start.getTime(); - } else { - return this.start.getTime() - this.end.getTime(); - } + super(start == null ? MIN_DATE : timestampToDate(start), end == null ? MAX_DATE : timestampToDate(end)); } @Override - public boolean equals(Object obj) { - return obj instanceof DateRange && ((DateRange)obj).start.equals(this.start) && ((DateRange)obj).end.equals(this.end); + public boolean after(Date date) { + return super.after(timestampToDate(date)); } @Override - public String toString() { - return this.start + " - " + this.end; - } - - /** Returns the starting date of this range. - * @return Range starting date - */ - public Date start() { - return (Date) this.start.clone(); + public boolean before(Date date) { + return super.before(timestampToDate(date)); } - /** Returns the starting date of this range as a <code>Timestamp</code> instance. - * @return Range starting date <code>Timestamp</code> + /** Returns this range's duration as a millisecond value. + * @return Range duration in milliseconds */ - public Timestamp startStamp() { - return new Timestamp(this.start.getTime()); + public long durationInMillis() { + return this.end.getTime() - this.start.getTime(); } - /** Returns the ending date of this range. - * @return Range ending date - */ + @Override public Date end() { return (Date) this.end.clone(); } @@ -98,104 +80,23 @@ return new Timestamp(this.end.getTime()); } - /** Returns <code>true</code> if the ending date is later than the starting date. - * @return <code>true</code> if the ending date is later than the starting date - */ - public boolean isAscending() { - return this.end.after(this.start) && !this.end.equals(this.start); - } - - /** Returns <code>true</code> if the starting and ending dates are equal. - * @return <code>true</code> if the starting and ending dates are equal - */ - public boolean isPoint() { - return this.end.equals(this.start); - } - /** Returns <code>true</code> if <code>date</code> occurs within this range. * @param date * @return <code>true</code> if <code>date</code> occurs within this range */ public boolean includesDate(Date date) { - date = downcastTimestamp(date); - if (isPoint()) { - return date.equals(this.start); - } - if (isAscending()) { - return (this.start.equals(date) || date.after(this.start)) && (this.end.equals(date) || date.before(this.end)); - } else { - return (this.start.equals(date) || date.before(this.start)) && (this.end.equals(date) || date.after(this.end)); - } - } - - /** Returns <code>true</code> if this range occurs before <code>date</code>. - * @param date - * @return <code>true</code> if this range occurs before <code>date</code> - */ - public boolean before(Date date) { - date = downcastTimestamp(date); - if (isAscending() || isPoint()) { - return this.end.before(date); - } else { - return this.start.before(date); - } - } - - /** Returns <code>true</code> if the latest date in this range - * occurs before the earliest date in <code>range</code>. - * @param range - * @return <code>true</code> if the latest date in this range - * occurs before the earliest date in <code>range</code> - */ - 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); - } - } - } - - /** Returns <code>true</code> if this range occurs after <code>date</code>. - * @param date - * @return <code>true</code> if this range occurs after <code>date</code> - */ - public boolean after(Date date) { - date = downcastTimestamp(date); - if (isAscending() || isPoint()) { - return this.start.after(date); - } else { - return this.end.after(date); - } + return super.includes(timestampToDate(date)); } - /** Returns <code>true</code> if the earliest date in this range - * occurs after the latest date in <code>range</code>. - * @param range - * @return <code>true</code> if the earliest date in this range - * occurs after the latest date in <code>range</code> + /** Returns <code>true</code> if <code>start</code> and <code>end</code> + * intersect this range. + * @param start If null, defaults to <a href="#MIN_DATE">MIN_DATE</a> + * @param end If null, defaults to <a href="#MAX_DATE">MAX_DATE</a> + * @return <code>true</code> if <code>start</code> and <code>end</code> + * intersect this range */ - 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(Date start, Date end) { + return intersectsRange(new DateRange(start, end)); } /** Returns <code>true</code> if <code>range</code> intersects this range. @@ -209,23 +110,15 @@ return !before(range) && !after(range); } - /** Returns <code>true</code> if <code>start</code> and <code>end</code> - * intersect this range. - * @param start If null, defaults to <a href="#MIN_DATE">MIN_DATE</a> - * @param end If null, defaults to <a href="#MAX_DATE">MAX_DATE</a> - * @return <code>true</code> if <code>start</code> and <code>end</code> - * intersect this range - */ - public boolean intersectsRange(Date start, Date end) { - return intersectsRange(new DateRange(start, end)); + @Override + public Date start() { + return (Date) this.start.clone(); } - protected Date downcastTimestamp(Date date) { - // Testing for equality between a Date instance and a Timestamp instance - // will always return false. - if (date instanceof Timestamp) { - date = new Date(date.getTime()); - } - return date; + /** Returns the starting date of this range as a <code>Timestamp</code> instance. + * @return Range starting date <code>Timestamp</code> + */ + public Timestamp startStamp() { + return new Timestamp(this.start.getTime()); } } |
| Free forum by Nabble | Edit this page |
