Author: adrianc Date: Wed Dec 24 20:21:46 2014 New Revision: 1647839 URL: http://svn.apache.org/r1647839 Log: Bug fix: Make DateRange.java truly immutable.
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilDateTime.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=1647839&r1=1647838&r2=1647839&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 Wed Dec 24 20:21:46 2014 @@ -30,9 +30,9 @@ import org.ofbiz.base.lang.ComparableRan @SuppressWarnings("serial") 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(); + public static final Date MIN_DATE = UtilDateTime.unmodifiableDate(UtilDateTime.getEarliestDate()); /** A <code>Date</code> instance initialized to the latest possible date.*/ - public static final Date MAX_DATE = UtilDateTime.getLatestDate(); + public static final Date MAX_DATE = UtilDateTime.unmodifiableDate(UtilDateTime.getLatestDate()); /** A <code>DateRange</code> instance initialized to the widest possible range of dates.*/ public static final DateRange FullRange = new DateRange(MIN_DATE, MAX_DATE); @@ -50,7 +50,8 @@ public class DateRange extends Comparabl * @param end If null, defaults to <a href="#MAX_DATE">MAX_DATE</a> */ public DateRange(Date start, Date end) { - super(start == null ? MIN_DATE : timestampToDate(start), end == null ? MAX_DATE : timestampToDate(end)); + super(start == null ? MIN_DATE : UtilDateTime.unmodifiableDate(timestampToDate(start)), end == null ? MAX_DATE + : UtilDateTime.unmodifiableDate(timestampToDate(end))); } @Override Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilDateTime.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilDateTime.java?rev=1647839&r1=1647838&r2=1647839&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilDateTime.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilDateTime.java Wed Dec 24 20:21:46 2014 @@ -1162,4 +1162,67 @@ public class UtilDateTime { cal.set(Calendar.MILLISECOND, 999); return cal.getTime(); } + + /** + * Returns a copy of <code>date</code> that cannot be modified. + * Attempts to modify the returned date will result in an + * <tt>UnsupportedOperationException</tt>. + * + * @param date + */ + public static Date unmodifiableDate(Date date) { + if (date instanceof ImmutableDate) { + return date; + } + return new ImmutableDate(date.getTime()); + } + + @SuppressWarnings("serial") + private static class ImmutableDate extends Date { + private ImmutableDate(long date) { + super(date); + } + + @Override + public Object clone() { + // No need to clone an immutable object. + return this; + } + + @Override + public void setYear(int year) { + throw new UnsupportedOperationException(); + } + + @Override + public void setMonth(int month) { + throw new UnsupportedOperationException(); + } + + @Override + public void setDate(int date) { + throw new UnsupportedOperationException(); + } + + @Override + public void setHours(int hours) { + throw new UnsupportedOperationException(); + } + + @Override + public void setMinutes(int minutes) { + throw new UnsupportedOperationException(); + } + + @Override + public void setSeconds(int seconds) { + throw new UnsupportedOperationException(); + } + + @Override + public void setTime(long time) { + throw new UnsupportedOperationException(); + } + + } }