Author: adrianc
Date: Thu Nov 13 14:44:52 2008 New Revision: 713848 URL: http://svn.apache.org/viewvc?rev=713848&view=rev Log: Temporal Expression class improvements: 1. Hardened classes. 2. Added accessor methods to expression classes and added a visitor interface. These are in preparation for the upcoming iCalendar integration. 3. JavaDoc improvements. Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionVisitor.java (with props) Modified: ofbiz/trunk/framework/service/data/ServiceDemoData.xml ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/RecurrenceInfo.java ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpression.java ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionWorker.java ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressions.java ofbiz/trunk/specialpurpose/projectmgr/data/ProjectMgrDemoData.xml Modified: ofbiz/trunk/framework/service/data/ServiceDemoData.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/data/ServiceDemoData.xml?rev=713848&r1=713847&r2=713848&view=diff ============================================================================== --- ofbiz/trunk/framework/service/data/ServiceDemoData.xml (original) +++ ofbiz/trunk/framework/service/data/ServiceDemoData.xml Thu Nov 13 14:44:52 2008 @@ -153,10 +153,10 @@ <TemporalExpressionAssoc fromTempExprId="GOVT_WORK_SCHED" toTempExprId="MON_TO_FRI" exprAssocType="INCLUDE"/> <TemporalExpressionAssoc fromTempExprId="GOVT_WORK_SCHED" toTempExprId="US_FED_HOLIDAYS" exprAssocType="EXCLUDE"/> - <!-- An 8am-5pm Mon-Fri expression that excludes US federal holidays --> + <!-- An 8am Mon-Fri expression that excludes US federal holidays --> <TemporalExpression tempExprId="DAILY_GRIND" tempExprTypeId="INTERSECTION"/> - <TemporalExpression tempExprId="8AM_TO_5PM" tempExprTypeId="TIME_OF_DAY_RANGE" string1="08:00" string2="17:00"/> - <TemporalExpressionAssoc fromTempExprId="DAILY_GRIND" toTempExprId="8AM_TO_5PM"/> + <TemporalExpression tempExprId="8AM" tempExprTypeId="TIME_OF_DAY_RANGE" string1="08:00" string2="08:00"/> + <TemporalExpressionAssoc fromTempExprId="DAILY_GRIND" toTempExprId="8AM"/> <TemporalExpressionAssoc fromTempExprId="DAILY_GRIND" toTempExprId="GOVT_WORK_SCHED"/> <!-- An every-other-Monday at 5pm expression --> Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/RecurrenceInfo.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/RecurrenceInfo.java?rev=713848&r1=713847&r2=713848&view=diff ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/RecurrenceInfo.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/RecurrenceInfo.java Thu Nov 13 14:44:52 2008 @@ -371,5 +371,6 @@ next.setTimeInMillis(result); return next; } + public void accept(TemporalExpressionVisitor visitor) {} } } 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=713848&r1=713847&r2=713848&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 Thu Nov 13 14:44:52 2008 @@ -37,6 +37,10 @@ * on correct ordering. Expressions are evaluated from lowest value * to highest value. */ protected int subSequence = Integer.MAX_VALUE; + + /** A unique ID for this expression. This field is intended to be used by + * persistence classes. */ + protected String id = null; /** Returns true if this expression includes the specified date. * @param cal A date to evaluate @@ -63,6 +67,11 @@ */ public abstract Calendar next(Calendar cal); + /** Handles a <code>TemporalExpressionVisitor</code> visit. + * @param visitor + */ + public abstract void accept(TemporalExpressionVisitor visitor); + public int compareTo(TemporalExpression obj) { if (this.equals(obj)) { return 0; @@ -96,6 +105,20 @@ return set; } + /** Returns this expression's ID. + * @return Expression ID String + */ + public String getId() { + return this.id; + } + + /** Sets this expression's ID. + * @param id Expression ID String + */ + public void setId(String id) { + this.id = id; + } + protected boolean containsExpression(TemporalExpression expression) { return false; } Added: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionVisitor.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionVisitor.java?rev=713848&view=auto ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionVisitor.java (added) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionVisitor.java Thu Nov 13 14:44:52 2008 @@ -0,0 +1,34 @@ +/******************************************************************************* + * 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.service.calendar; + +/** Temporal expression visitor interface. */ +public interface TemporalExpressionVisitor { + void visit(TemporalExpressions.Null expr); + void visit(TemporalExpressions.Union expr); + void visit(TemporalExpressions.Intersection expr); + void visit(TemporalExpressions.Difference expr); + void visit(TemporalExpressions.DateRange expr); + void visit(TemporalExpressions.TimeOfDayRange expr); + void visit(TemporalExpressions.DayOfWeekRange expr); + void visit(TemporalExpressions.MonthRange expr); + void visit(TemporalExpressions.DayOfMonthRange expr); + void visit(TemporalExpressions.DayInMonth expr); + void visit(TemporalExpressions.Frequency expr); +} Propchange: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionVisitor.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionWorker.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionWorker.java?rev=713848&r1=713847&r2=713848&view=diff ============================================================================== --- ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionWorker.java (original) +++ ofbiz/trunk/framework/service/src/org/ofbiz/service/calendar/TemporalExpressionWorker.java Thu Nov 13 14:44:52 2008 @@ -50,7 +50,7 @@ /** Get a <code>TemporalExpression</code> from persistent storage. * @param delegator * @param tempExprId - * @return + * @return A <code>TemporalExpression</code> instance based on <code>tempExprId</code> * @throws GenericEntityException */ public static TemporalExpression getTemporalExpression(GenericDelegator delegator, String tempExprId) throws GenericEntityException { @@ -69,7 +69,7 @@ * avoid endless loops.</p> * @param delegator * @param exprValue - * @return + * @return A <code>TemporalExpression</code> instance based on <code>exprValue</code> * @throws GenericEntityException */ public static TemporalExpression makeTemporalExpression(GenericDelegator delegator, GenericValue exprValue) throws GenericEntityException { 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=713848&r1=713847&r2=713848&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 Thu Nov 13 14:44:52 2008 @@ -20,19 +20,24 @@ import java.io.Serializable; import java.util.Calendar; +import java.util.Collections; import java.util.Date; import java.util.Set; import java.util.TreeSet; import org.ofbiz.base.util.Debug; -/** A collection of TemporalExpression classes. */ +/** A collection of TemporalExpression classes. + * <p>For the most part, these classes are immutable - with the exception + * of the <code>id</code> field. The basic idea is to construct an expression + * tree in memory, and then query it.</p> + */ @SuppressWarnings("serial") public class TemporalExpressions implements Serializable { public static final String module = TemporalExpressions.class.getName(); public static final TemporalExpression NullExpression = new Null(); /** This class represents a null expression. */ - protected static class Null extends TemporalExpression { + public static class Null extends TemporalExpression { public Calendar first(Calendar cal) { return null; } @@ -42,14 +47,15 @@ public Calendar next(Calendar cal) { return null; } + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } } /** This class represents a mathematical union of all of its * member expressions. */ public static class Union extends TemporalExpression { - protected Set<TemporalExpression> expressionSet = null; - - protected Union() {} + protected final Set<TemporalExpression> expressionSet; public Union(Set<TemporalExpression> expressionSet) { if (expressionSet == null) { @@ -114,6 +120,10 @@ return null; } + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + public Set<Date> getRange(org.ofbiz.base.util.DateRange range, Calendar cal) { Set<Date> rawSet = new TreeSet<Date>(); Set<Date> finalSet = new TreeSet<Date>(); @@ -130,6 +140,14 @@ return finalSet; } + /** Returns the member expression <code>Set</code>. The + * returned set is unmodifiable. + * @return The member expression <code>Set</code> + */ + public Set<TemporalExpression> getExpressionSet() { + return Collections.unmodifiableSet(this.expressionSet); + } + protected boolean containsExpression(TemporalExpression expression) { for (TemporalExpression setItem : this.expressionSet) { if (setItem.containsExpression(expression)) { @@ -143,9 +161,7 @@ /** This class represents a mathematical intersection of all of its * member expressions. */ public static class Intersection extends TemporalExpression { - protected Set<TemporalExpression> expressionSet = null; - - protected Intersection() {} + protected final Set<TemporalExpression> expressionSet; public Intersection(Set<TemporalExpression> expressionSet) { if (expressionSet == null) { @@ -220,6 +236,10 @@ } } + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + public Set<Date> getRange(org.ofbiz.base.util.DateRange range, Calendar cal) { Set<Date> finalSet = new TreeSet<Date>(); Set<Date> rawSet = new TreeSet<Date>(); @@ -243,6 +263,14 @@ return finalSet; } + /** Returns the member expression <code>Set</code>. The + * returned set is unmodifiable. + * @return The member expression <code>Set</code> + */ + public Set<TemporalExpression> getExpressionSet() { + return Collections.unmodifiableSet(this.expressionSet); + } + protected boolean containsExpression(TemporalExpression expression) { for (TemporalExpression setItem : this.expressionSet) { if (setItem.containsExpression(expression)) { @@ -255,8 +283,8 @@ /** This class represents a difference of two temporal expressions. */ public static class Difference extends TemporalExpression { - protected TemporalExpression included = null; - protected TemporalExpression excluded = null; + protected final TemporalExpression included; + protected final TemporalExpression excluded; public Difference(TemporalExpression included, TemporalExpression excluded) { if (included == null) { @@ -311,6 +339,10 @@ return next; } + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + 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); @@ -324,6 +356,20 @@ return finalSet; } + /** Returns the excluded expression. + * @return The excluded <code>TemporalExpression</code> + */ + public TemporalExpression getExcluded() { + return this.excluded; + } + + /** Returns the included expression. + * @return The included <code>TemporalExpression</code> + */ + public TemporalExpression getIncluded() { + return this.included; + } + protected boolean containsExpression(TemporalExpression expression) { return this.included.containsExpression(expression) || this.excluded.containsExpression(expression); } @@ -331,7 +377,7 @@ /** A temporal expression that represents a range of dates. */ public static class DateRange extends TemporalExpression { - protected org.ofbiz.base.util.DateRange range = null; + protected final org.ofbiz.base.util.DateRange range; public DateRange(Date start, Date end) { this.sequence = 1000; @@ -366,18 +412,29 @@ public Calendar next(Calendar cal) { return includesDate(cal) ? cal : null; } + + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + + /** Returns the contained <code>org.ofbiz.base.util.DateRange</code>. + * @return The contained <code>org.ofbiz.base.util.DateRange</code> + */ + public org.ofbiz.base.util.DateRange getDateRange() { + return this.range; + } } /** A temporal expression that represents a time of day range. */ public static class TimeOfDayRange extends TemporalExpression { - protected String startStr = null; - protected String endStr = null; - protected int startSecs = 0; - protected int startMins = 0; - protected int startHrs = 0; - protected int endSecs = 0; - protected int endMins = 0; - protected int endHrs = 0; + protected final String startStr; + protected final String endStr; + protected final int startSecs; + protected final int startMins; + protected final int startHrs; + protected final int endSecs; + protected final int endMins; + protected final int endHrs; /** * @param start A time String in the form of hh:mm:ss (24 hr clock) @@ -392,7 +449,26 @@ } this.startStr = start; this.endStr = end; - init(); + String strArray[] = this.startStr.split(":"); + if (strArray.length == 0 || strArray.length > 3) { + throw new IllegalArgumentException("Invalid start time argument"); + } + this.startHrs = Integer.valueOf(strArray[0]); + this.startMins = strArray.length > 1 ? Integer.valueOf(strArray[1]) : 0; + this.startSecs = strArray.length > 2 ? Integer.valueOf(strArray[2]) : 0; + if (this.startHrs > 23 || this.startMins > 59 || this.startSecs > 59) { + throw new IllegalArgumentException("Invalid start time argument"); + } + strArray = this.endStr.split(":"); + if (strArray.length == 0 || strArray.length > 3) { + throw new IllegalArgumentException("Invalid end time argument"); + } + this.endHrs = Integer.valueOf(strArray[0]); + this.endMins = strArray.length > 1 ? Integer.valueOf(strArray[1]) : 0; + this.endSecs = strArray.length > 2 ? Integer.valueOf(strArray[2]) : 0; + if (this.endHrs > 23 || this.endMins > 59 || this.endSecs > 59) { + throw new IllegalArgumentException("Invalid end time argument"); + } this.sequence = 600; this.subSequence = (this.startHrs * 4000) + (this.startMins * 60) + this.startSecs; if (Debug.verboseOn()) { @@ -434,35 +510,32 @@ return first(cal); } - protected void init() { - String strArray[] = this.startStr.split(":"); - if (strArray.length == 0 || strArray.length > 3) { - throw new IllegalArgumentException("Invalid time argument"); - } - this.startHrs = Integer.valueOf(strArray[0]); - if (strArray.length > 1) { - this.startMins = Integer.valueOf(strArray[1]); - } - if (strArray.length > 2) { - this.startSecs = Integer.valueOf(strArray[2]); - } - if (this.startHrs > 23 || this.startMins > 59 || this.startSecs > 59) { - throw new IllegalArgumentException("Invalid time argument"); - } - strArray = this.endStr.split(":"); - if (strArray.length == 0 || strArray.length > 3) { - throw new IllegalArgumentException("Invalid time argument"); - } - this.endHrs = Integer.valueOf(strArray[0]); - if (strArray.length > 1) { - this.endMins = Integer.valueOf(strArray[1]); - } - if (strArray.length > 2) { - this.endSecs = Integer.valueOf(strArray[2]); - } - if (this.endHrs > 23 || this.endMins > 59 || this.endSecs > 59) { - throw new IllegalArgumentException("Invalid time argument"); - } + public int getEndHours() { + return this.endHrs; + } + + public int getEndMins() { + return this.endMins; + } + + public int getEndSecs() { + return this.endSecs; + } + + public int getStartHours() { + return this.startHrs; + } + + public int getStartMins() { + return this.startMins; + } + + public int getStartSecs() { + return this.startSecs; + } + + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); } protected Calendar advanceCalendar(Calendar cal, int hrs, int mins, int secs) { @@ -497,8 +570,8 @@ /** A temporal expression that represents a day of week range. */ public static class DayOfWeekRange extends TemporalExpression { - protected int start; - protected int end; + protected final int start; + protected final int end; /** * @param start An integer in the range of <code>Calendar.SUNDAY</code> @@ -571,12 +644,30 @@ } return setStartOfDay(next); } + + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + + /** Returns the starting day of this range. + * @return The starting day of this range + */ + public int getStartDay() { + return this.start; + } + + /** Returns the ending day of this range. + * @return The ending day of this range + */ + public int getEndDay() { + return this.end; + } } /** A temporal expression that represents a month range. */ public static class MonthRange extends TemporalExpression { - protected int start; - protected int end; + protected final int start; + protected final int end; /** * @param start An integer in the range of <code>Calendar.JANUARY</code> @@ -651,12 +742,30 @@ } return next; } + + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + + /** Returns the starting month of this range. + * @return The starting month of this range + */ + public int getStartMonth() { + return this.start; + } + + /** Returns the ending month of this range. + * @return The ending month of this range + */ + public int getEndMonth() { + return this.end; + } } /** A temporal expression that represents a day of month range. */ public static class DayOfMonthRange extends TemporalExpression { - protected int start; - protected int end; + protected final int start; + protected final int end; /** * @param start An integer in the range of 1 to 31 @@ -714,12 +823,30 @@ } return next; } + + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + + /** Returns the starting day of this range. + * @return The starting day of this range + */ + public int getStartDay() { + return this.start; + } + + /** Returns the ending day of this range. + * @return The ending day of this range + */ + public int getEndDay() { + return this.end; + } } /** A temporal expression that represents a day in the month. */ public static class DayInMonth extends TemporalExpression { - protected int dayOfWeek; - protected int occurrence; + protected final int dayOfWeek; + protected final int occurrence; /** * @param dayOfWeek An integer in the range of <code>Calendar.SUNDAY</code> @@ -794,6 +921,24 @@ return next; } + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + + /** Returns the day of week in this expression. + * @return The day of week in this expression + */ + public int getDayOfWeek() { + return this.dayOfWeek; + } + + /** Returns the occurrence in this expression. + * @return The occurrence in this expression + */ + public int getOccurrence() { + return this.occurrence; + } + protected Calendar alignDayOfWeek(Calendar cal) { cal.set(Calendar.DAY_OF_MONTH, 1); if (this.occurrence > 0) { @@ -815,9 +960,9 @@ /** A temporal expression that represents a frequency. */ public static class Frequency extends TemporalExpression { - protected Date start; - protected int freqType; - protected int freqCount; + protected final Date start; + protected final int freqType; + protected final int freqCount; /** * @param start Starting date, defaults to current system time @@ -885,6 +1030,31 @@ return next; } + public void accept(TemporalExpressionVisitor visitor) { + visitor.visit(this); + } + + /** Returns the start date of this expression. + * @return The start date of this expression + */ + public Date getStartDate() { + return (Date) this.start.clone(); + } + + /** Returns the frequency type of this expression. + * @return The frequency type of this expression + */ + public int getFreqType() { + return this.freqType; + } + + /** Returns the frequency count of this expression. + * @return The frequency count of this expression + */ + public int getFreqCount() { + return this.freqCount; + } + protected Calendar prepareCal(Calendar cal) { // Performs a "sane" skip forward in time - avoids time consuming loops // like incrementing every second from Jan 1 2000 until today Modified: ofbiz/trunk/specialpurpose/projectmgr/data/ProjectMgrDemoData.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/specialpurpose/projectmgr/data/ProjectMgrDemoData.xml?rev=713848&r1=713847&r2=713848&view=diff ============================================================================== --- ofbiz/trunk/specialpurpose/projectmgr/data/ProjectMgrDemoData.xml (original) +++ ofbiz/trunk/specialpurpose/projectmgr/data/ProjectMgrDemoData.xml Thu Nov 13 14:44:52 2008 @@ -148,9 +148,9 @@ <!-- Recurring calendar event demo - set up a staff meeting on the first Monday of each month --> <TemporalExpression tempExprId="STAFF_MTG" tempExprTypeId="INTERSECTION"/> - <TemporalExpression tempExprId="10AM_TO_11AM" tempExprTypeId="TIME_OF_DAY_RANGE" string1="10:00" string2="11:00"/> + <TemporalExpression tempExprId="10AM" tempExprTypeId="TIME_OF_DAY_RANGE" string1="10:00" string2="10:00"/> <TemporalExpressionAssoc fromTempExprId="STAFF_MTG" toTempExprId="1ST_MONDAY_IN_MONTH"/> - <TemporalExpressionAssoc fromTempExprId="STAFF_MTG" toTempExprId="10AM_TO_11AM"/> + <TemporalExpressionAssoc fromTempExprId="STAFF_MTG" toTempExprId="10AM"/> <FixedAsset fixedAssetId="DEMO_PROJECTOR" fixedAssetName="Overhead Projector" fixedAssetTypeId="EQUIPMENT"/> <WorkEffort workEffortId="STAFF_MTG" workEffortTypeId="MEETING" currentStatusId="CAL_TENTATIVE" lastStatusUpdate="2008-01-01 00:00:00.0" scopeEnumId="WES_PUBLIC" workEffortName="Staff Meeting" description="Staff Meeting" tempExprId="STAFF_MTG" estimatedStartDate="2008-01-01 00:00:00.0" estimatedMilliSeconds="3600000"/> <WorkEffortFixedAssetAssign workEffortId="STAFF_MTG" fixedAssetId="DEMO_PROJECTOR" statusId="FA_ASGN_REQUESTED" availabilityStatusId="WEFA_IN_USE" fromDate="2008-01-01 00:00:00.0"/> @@ -158,4 +158,4 @@ <WorkEffortPartyAssignment workEffortId="STAFF_MTG" partyId="DemoEmployee2" statusId="PRTYASGN_ASSIGNED" roleTypeId="CAL_ATTENDEE" availabilityStatusId="WEPA_AV_BUSY" fromDate="2008-01-01 00:00:00.0"/> <WorkEffortPartyAssignment workEffortId="STAFF_MTG" partyId="DemoEmployee3" statusId="PRTYASGN_ASSIGNED" roleTypeId="CAL_ATTENDEE" availabilityStatusId="WEPA_AV_BUSY" fromDate="2008-01-01 00:00:00.0"/> -</entity-engine-xml> \ No newline at end of file +</entity-engine-xml> |
Free forum by Nabble | Edit this page |