|
From: Aron S. <bi...@gm...> - 2006-07-10 05:28:47
|
User: asogor
Date: 06/07/10 01:28:44
Modified: src/calendar/calendarejb/src/java/org/jboss/mail/calendar/util/availability
AvailabilityCalculator.java
Log:
updates to the calculator
Revision Changes Path
1.2 +161 -98 jboss-mail/src/calendar/calendarejb/src/java/org/jboss/mail/calendar/util/availability/AvailabilityCalculator.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: AvailabilityCalculator.java
===================================================================
RCS file: /cvsroot/jboss/jboss-mail/src/calendar/calendarejb/src/java/org/jboss/mail/calendar/util/availability/AvailabilityCalculator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- AvailabilityCalculator.java 8 Jul 2006 01:48:10 -0000 1.1
+++ AvailabilityCalculator.java 10 Jul 2006 05:28:44 -0000 1.2
@@ -10,8 +10,11 @@
import org.jboss.mail.calendar.data.CalendarEvent;
import org.jboss.mail.calendar.data.Invite;
+import com.sun.tools.javac.tree.Tree.Assert;
+
/**
- * AvailabilityCalculator is a utility to figure out possible timeslots for a meeting
+ * AvailabilityCalculator is a utility to figure out possible timeslots for a
+ * meeting
*
* @author Aron Sogor
*
@@ -23,88 +26,136 @@
* The logic of this calculator is simple:
*
* Create segments using the increment between start date and end date.
- * Check which segments are free, meaning there are no meeting during that segment.
- * Find numberOfunits continous free segments.
- * Return all event proposals.
+ * Check which segments are free, meaning there are no meeting during that
+ * segment. Find numberOfunits continous free segments. Return all event
+ * proposals.
*
- * @param invites all the attendees events
- * @param startTime the begining of the search range
- * @param endTime the end of the search range
- * @param incrementSize the size of an increment in minutes
- * @param numberOfunits the number of increments the meeting should last
+ * @param invites
+ * all the attendees events
+ * @param startTime
+ * the begining of the search range
+ * @param endTime
+ * the end of the search range
+ * @param incrementSize
+ * the size of an increment in minutes
+ * @param numberOfunits
+ * the number of increments the meeting should last
*/
- public static CalendarEvent[] calculate(Invite[] invites,Date startTime,Date endTime,int incrementSize,int numberOfunits)
- {
- return (new AvailabilityCalculator()).calculateEvents(invites,startTime,endTime,incrementSize,numberOfunits);
+ public static CalendarEvent[] calculate(Invite[] invites, Date startTime,
+ Date endTime, int incrementSize, int numberOfunits) {
+ return (new AvailabilityCalculator()).calculateEvents(invites,
+ startTime, endTime, incrementSize, numberOfunits);
}
- private CalendarEvent[] calculateEvents(Invite[] invites,Date startTime,Date endTime,int incrementSize,int numberOfunits)
- {
+ private CalendarEvent[] calculateEvents(Invite[] invites, Date startTime,
+ Date endTime, int incrementSize, int numberOfunits) {
ArrayList<CalendarEvent> result = new ArrayList<CalendarEvent>();
- if(startTime.after(endTime))
- {
- log.warn("Availibilty calculator was passed stared and end date reversed");
+ if (startTime.after(endTime)) {
+ log
+ .warn("Availibilty calculator was passed stared and end date reversed");
return new CalendarEvent[0];
}
- double segmentTotal = Math.ceil((startTime.getTime() - endTime.getTime())/(60000 * incrementSize));
-
- //generate the segments
- Segment firstSegment = setupSegments(startTime,endTime,segmentTotal);
- //mark each segment free(true) or busy(false)
- //check each segment as valid
-
- return (CalendarEvent[])result.toArray(new CalendarEvent[0]);
+ double segmentTotal = Math.ceil((startTime.getTime() - endTime
+ .getTime())
+ / (60000 * incrementSize));
+
+ // generate the segments
+ // mark each segment free or busy
+ Segment firstSegment = setupSegments(invites, startTime, endTime,
+ segmentTotal, (60000 * incrementSize));
+ // check each segment as valid
+ Segment cursor = firstSegment;
+ while (cursor!=null) {
+ if(cursor.isFree(numberOfunits))
+ {
+ CalendarEvent event = new CalendarEvent();
+ event.setStartDate(cursor.getStartTime());
+ event.setEndDate(new Date(cursor.getStartTime().getTime() + (60000 * incrementSize)));
+ result.add(event);
+ }
+ }
+ return (CalendarEvent[]) result.toArray(new CalendarEvent[0]);
}
- private Segment setupSegments(Date startTime,Date endTime,double segmentTotal)
- {
+ private Segment setupSegments(Invite[] invites, Date startTime,
+ Date endTime, double segmentTotal, int increment) {
Segment firstSegment = null;
Segment currentSegment = null;
+ // date is mutable do not want to shift starttime
+ Date segmentStart = new Date(startTime.getTime());
for (int segmentCnt = 0; segmentCnt < segmentTotal; segmentCnt++) {
- if(currentSegment == null)
- {
- currentSegment.setNextSegment(new Segment(startTime,endTime,true));
+ if (currentSegment == null) {
+ currentSegment = new Segment(new Date(segmentStart.getTime()),
+ new Date(startTime.getTime() + increment), false);
firstSegment = currentSegment;
- }
- else
- currentSegment.setNextSegment(new Segment(startTime,endTime,true));
+ } else {
+ currentSegment.setNextSegment(new Segment(new Date(segmentStart
+ .getTime()), new Date(startTime.getTime() + increment),
+ false));
currentSegment = currentSegment.getNextSegment();
}
+ evaluateSegmentFreeBusy(invites,currentSegment);
+ segmentStart.setTime(currentSegment.getEndTime().getTime());
+ }
return firstSegment;
}
- private class Segment{
+ private void evaluateSegmentFreeBusy(Invite[] invites, Segment segment) {
+ for (int i = 0; ((i < invites.length)&&(!segment.isBusy)); i++) {
+ segment.setBusy(((segment.getStartTime().getTime() <= invites[i]
+ .getEvent().getStartDate().getTime()) && (segment
+ .getEndTime().getTime() >= invites[i].getEvent()
+ .getStartDate().getTime()))
+ || ((segment.getStartTime().getTime() <= invites[i]
+ .getEvent().getEndDate().getTime()) && (segment
+ .getEndTime().getTime() >= invites[i].getEvent()
+ .getEndDate().getTime())));
+ }
+ }
+
+ private class Segment {
private Date startTime = null;
+
private Date endTime = null;
+
private boolean isBusy = false;
+
private Segment nextSegment = null;
public Date getEndTime() {
return endTime;
}
+
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
+
public boolean isBusy() {
return isBusy;
}
+
public void setBusy(boolean isBusy) {
this.isBusy = isBusy;
}
+
public Date getStartTime() {
return startTime;
}
+
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
+
public Segment getNextSegment() {
return nextSegment;
}
+
public void setNextSegment(Segment nextSegment) {
this.nextSegment = nextSegment;
}
+
public Segment(Date startTime, Date endTime, boolean isBusy) {
super();
// TODO Auto-generated constructor stub
@@ -112,5 +163,17 @@
this.endTime = endTime;
this.isBusy = isBusy;
}
+
+ public boolean isFree(int depth)
+ {
+ // busy, cut the chain
+ if(isBusy) return false;
+ // last element, must be true but if I am wrong:)
+ if(depth == 1) {assert(isBusy==true);return true;}
+ // not enough "time" at the end of the list
+ if(nextSegment == null) return false;
+ //call next
+ return nextSegment.isFree(depth--);
+ }
}
}
|