Author: adrianc
Date: Mon Sep 22 18:07:16 2008 New Revision: 698055 URL: http://svn.apache.org/viewvc?rev=698055&view=rev Log: Moved a couple of the temporal expression related classes to the base component, removed some POC code, and added elapsed time calculation to the TimeDuration class. Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java (with props) ofbiz/trunk/framework/base/src/org/ofbiz/base/util/TimeDuration.java (with props) Removed: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/DateRange.java ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TimeDuration.java Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpression.java ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressions.java Added: 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=698055&view=auto ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java (added) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java Mon Sep 22 18:07:16 2008 @@ -0,0 +1,115 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util; + +import java.io.Serializable; +import java.util.Date; + +/** An immutable range of dates. + */ +@SuppressWarnings("serial") +public class DateRange implements Serializable { + public static final Date MIN_DATE = new Date(Long.MIN_VALUE); + public static final Date MAX_DATE = new Date(Long.MAX_VALUE); + public static final DateRange FullRange = new DateRange(); + + protected Date start = MIN_DATE; + protected Date end = MAX_DATE; + + protected DateRange() {} + + public DateRange(Date start, Date end) { + if (start != null) { + this.start = start; + } + if (end != null) { + this.end = end; + } + } + + public long durationInMillis() { + if (this.end.after(this.start)) { + return this.end.getTime() - this.start.getTime(); + } else { + return this.start.getTime() - this.end.getTime(); + } + } + + public boolean equals(Object obj) { + return obj instanceof DateRange && ((DateRange)obj).start.equals(this.start) && ((DateRange)obj).end.equals(this.end); + } + + public String toString() { + return super.toString() + ", start = " + this.start + ", end = " + this.end; + } + + public Date start() { + return (Date) this.start.clone(); + } + + public Date end() { + return (Date) this.end.clone(); + } + + public boolean isAscending() { + return this.end.after(this.start) && !this.end.equals(this.start); + } + + public boolean isPoint() { + return this.end.equals(this.start); + } + + public boolean includesDate(Date 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)); + } + } + + public boolean intersectsRange(DateRange dateRange) { + return intersectsRange(dateRange.start, dateRange.end); + } + + public boolean intersectsRange(Date start, Date end) { + if (start == null) { + throw new IllegalArgumentException("start argument cannot be null"); + } + if (end == null) { + throw new IllegalArgumentException("end argument cannot be null"); + } + if (isPoint()) { + return end.equals(start) && this.start.equals(start); + } + if (isAscending()) { + if (start.after(end)) { + return false; + } + return (this.end.equals(start) || start.before(this.end)) && (this.start.equals(end) || end.after(this.start)); + } else { + if (end.after(start)) { + return false; + } + return (this.end.equals(start) || start.after(this.end)) && (this.start.equals(end) || end.before(this.start)); + } + } +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/DateRange.java ------------------------------------------------------------------------------ svn:eol-style = native Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/TimeDuration.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/TimeDuration.java?rev=698055&view=auto ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/TimeDuration.java (added) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/TimeDuration.java Mon Sep 22 18:07:16 2008 @@ -0,0 +1,206 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.base.util; + +import java.io.Serializable; +import java.util.Calendar; + +/** A representation of a period of time. */ +@SuppressWarnings("serial") +public class TimeDuration implements Serializable { + public static final TimeDuration ZeroTimeDuration = new NullDuration(); + + protected int millis = 0; + protected int seconds = 0; + protected int minutes = 0; + protected int hours = 0; + protected int days = 0; + protected int months = 0; + protected int years = 0; + + protected TimeDuration() {} + + public TimeDuration(int millis, int seconds, int minutes, int hours, int days, int months, int years) { + this.millis = millis; + this.seconds = seconds; + this.minutes = minutes; + this.hours = hours; + this.days = days; + this.months = months; + this.years = years; + } + + public TimeDuration(Calendar cal1, Calendar cal2) { + this.set(cal1, cal2); + } + + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + try { + TimeDuration that = (TimeDuration) obj; + return this.years == that.years && this.months == that.months && this.days == that.days + && this.hours == that.hours && this.minutes == that.minutes && this.seconds == that.seconds + && this.millis == that.millis; + } catch (Exception e) {} + return false; + } + + public String toString() { + return this.years + ":" + this.months + ":" + this.days + ":" + this.hours + ":" + this.minutes + ":" + this.seconds + ":" + this.millis; + } + + public int millis() { + return this.millis; + } + + public int seconds() { + return this.seconds; + } + + public int minutes() { + return this.minutes; + } + + public int hours() { + return this.hours; + } + + public int days() { + return this.days; + } + + public int months() { + return this.months; + } + + public int years() { + return this.years; + } + + /** Add this time duration to a Calendar instance. Returns the original + * Calendar instance. + * @param cal + * @return The <code>cal</code> argument + */ + public Calendar addToCalendar(Calendar cal) { + cal.add(Calendar.MILLISECOND, this.millis); + cal.add(Calendar.SECOND, this.seconds); + cal.add(Calendar.MINUTE, this.minutes); + cal.add(Calendar.HOUR, this.hours); + cal.add(Calendar.DAY_OF_MONTH, this.days); + cal.add(Calendar.MONTH, this.months); + cal.add(Calendar.YEAR, this.years); + return cal; + } + + /** Subtract this time duration to a Calendar instance. Returns the original + * Calendar instance. + * @param cal + * @return The <code>cal</code> argument + */ + public Calendar subtractFromCalendar(Calendar cal) { + cal.add(Calendar.MILLISECOND, -this.millis); + cal.add(Calendar.SECOND, -this.seconds); + cal.add(Calendar.MINUTE, -this.minutes); + cal.add(Calendar.HOUR, -this.hours); + cal.add(Calendar.DAY_OF_MONTH, -this.days); + cal.add(Calendar.MONTH, -this.months); + cal.add(Calendar.YEAR, -this.years); + return cal; + } + + + protected void set(Calendar cal1, Calendar cal2) { + // set up Calendar objects + Calendar calStart = null; + Calendar calEnd = null; + if (cal1.before(cal2)) { + calStart = (Calendar) cal1.clone(); + calEnd = (Calendar) cal2.clone(); + } else { + calStart = (Calendar) cal2.clone(); + calEnd = (Calendar) cal1.clone(); + } + + // this will be used to speed up time comparisons + long targetMillis = calEnd.getTimeInMillis(); + long deltaMillis = targetMillis - calStart.getTimeInMillis(); + + // shortcut for equal dates + if (deltaMillis == 0) { + return; + } + + // compute elapsed years + long yearMillis = 86400000 * calStart.getMinimum(Calendar.DAY_OF_YEAR); + float units = deltaMillis / yearMillis; + this.years = advanceCalendar(calStart, calEnd, (int) units, Calendar.YEAR); + deltaMillis = targetMillis - calStart.getTimeInMillis(); + + // compute elapsed months + long monthMillis = 86400000 * calStart.getMinimum(Calendar.DAY_OF_MONTH); + units = deltaMillis / monthMillis; + this.months = advanceCalendar(calStart, calEnd, (int) units, Calendar.MONTH); + deltaMillis = targetMillis - calStart.getTimeInMillis(); + + // compute elapsed days + units = deltaMillis / 86400000; + this.days = advanceCalendar(calStart, calEnd, (int) units, Calendar.DAY_OF_MONTH); + deltaMillis = targetMillis - calStart.getTimeInMillis(); + + // compute elapsed hours + units = deltaMillis / 3600000; + this.hours = advanceCalendar(calStart, calEnd, (int) units, Calendar.HOUR); + deltaMillis = targetMillis - calStart.getTimeInMillis(); + + // compute elapsed minutes + units = deltaMillis / 60000; + this.minutes = advanceCalendar(calStart, calEnd, (int) units, Calendar.MINUTE); + deltaMillis = targetMillis - calStart.getTimeInMillis(); + + // compute elapsed seconds + units = deltaMillis / 1000; + this.seconds = advanceCalendar(calStart, calEnd, (int) units, Calendar.SECOND); + deltaMillis = targetMillis - calStart.getTimeInMillis(); + + this.millis = (int) deltaMillis; + } + + protected int advanceCalendar(Calendar start, Calendar end, int units, int type) { + if (units >= 1) { + start.add(type, units); + while (start.after(end)) { + start.add(type, -1); + units--; + } + } + return units; + } + + protected static class NullDuration extends TimeDuration { + public Calendar addToCalendar(Calendar cal) { + return cal; + } + public Calendar subtractFromCalendar(Calendar cal) { + return cal; + } + } +} Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/TimeDuration.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpression.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpression.java?rev=698055&r1=698054&r2=698055&view=diff ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpression.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpression.java Mon Sep 22 18:07:16 2008 @@ -81,7 +81,7 @@ * @param cal The starting date * @return A Set of matching <code>Date</code> objects */ - public Set<Date> getRange(org.ofbiz.service.calendar.DateRange range, Calendar cal) { + public Set<Date> getRange(org.ofbiz.base.util.DateRange range, Calendar cal) { Set<Date> set = new TreeSet<Date>(); Date last = range.start(); Calendar next = first(cal); Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressions.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressions.java?rev=698055&r1=698054&r2=698055&view=diff ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressions.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressions.java Mon Sep 22 18:07:16 2008 @@ -114,7 +114,7 @@ return null; } - public Set<Date> getRange(org.ofbiz.service.calendar.DateRange range, Calendar cal) { + public Set<Date> getRange(org.ofbiz.base.util.DateRange range, Calendar cal) { Set<Date> rawSet = new TreeSet<Date>(); Set<Date> finalSet = new TreeSet<Date>(); for (TemporalExpression expression : this.expressionSet) { @@ -220,7 +220,7 @@ } } - public Set<Date> getRange(org.ofbiz.service.calendar.DateRange range, Calendar cal) { + public Set<Date> getRange(org.ofbiz.base.util.DateRange range, Calendar cal) { Set<Date> finalSet = new TreeSet<Date>(); Set<Date> rawSet = new TreeSet<Date>(); Date last = range.start(); @@ -311,7 +311,7 @@ return next; } - public Set<Date> getRange(org.ofbiz.service.calendar.DateRange range, Calendar cal) { + public Set<Date> getRange(org.ofbiz.base.util.DateRange range, Calendar cal) { Set<Date> finalSet = new TreeSet<Date>(); Set<Date> rawSet = this.included.getRange(range, cal); Calendar checkCal = (Calendar) cal.clone(); @@ -331,11 +331,11 @@ /** A temporal expression that represents a range of dates. */ public static class DateRange extends TemporalExpression { - protected org.ofbiz.service.calendar.DateRange range = null; + protected org.ofbiz.base.util.DateRange range = null; public DateRange(Date start, Date end) { this.sequence = 1000; - this.range = new org.ofbiz.service.calendar.DateRange(start, end); + this.range = new org.ofbiz.base.util.DateRange(start, end); if (Debug.verboseOn()) { Debug.logVerbose("Created " + this, module); } |
Free forum by Nabble | Edit this page |