source: DCWoRMS/trunk/src/schedframe/scheduling/plugin/local/TimeOperations.java @ 477

Revision 477, 15.2 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.scheduling.plugin.local;
2
3import java.util.ArrayList;
4
5import org.joda.time.DateTime;
6
7/**
8 * @author Ariel Oleksiak
9 *
10 */
11public class TimeOperations {
12
13                long timeDelta;
14
15                public static final long TWENTY_FOUR_HOURS = 24*60*60*1000; //24h in millis
16
17                /**
18                 * Constructor of DateOperations class
19                 * @param timeDelta - parameter used to express potential time overheads, e.g.
20                 * while checking if certain period fits into another
21                 */
22                public TimeOperations(long timeDelta) {
23                        this.timeDelta = timeDelta;
24                }
25
26                //Getters/setters
27               
28                public long getTimeDelta() {
29                        return timeDelta;
30                }
31
32                public void setTimeDelta(long timeDelta) {
33                        this.timeDelta = timeDelta;
34                }
35               
36                //end of getters/setters
37               
38               
39                //Comparisons
40               
41                /**
42                 * Compares two Calendar objects taking into account date only, i.e.
43                 * year, month, and day (without hour, minute, second, milis)
44                 * @param date1
45                 * @param date2
46                 * @return true if date1 is before date2; false otherwise
47                 */
48                public boolean beforeDate(DateTime date1, DateTime date2) {
49
50                        if (date1 == null || date2 == null)
51                                return false;
52                       
53                        if (date1.getYear() < date2.getYear())
54                                return true;
55                        if (date1.getYear() > date2.getYear())
56                                return false;
57                                       
58                        if (date1.getMonthOfYear() < date2.getMonthOfYear())
59                                return true;
60                        if (date1.getMonthOfYear() > date2.getMonthOfYear())
61                                return false;
62                       
63                        if (date1.getDayOfMonth() < date2.getDayOfMonth())
64                                return true;
65                        if (date1.getDayOfMonth() > date2.getDayOfMonth())
66                                return false;
67
68                        return false;
69                }
70
71               
72                /**
73                 * Compares two Calendar objects taking into account date only, i.e.
74                 * year, month, and day (without hour, minute, second, milis)
75                 * @param date1
76                 * @param date2
77                 * @return true if dates are equal; false otherwise
78                 */
79                public boolean sameDate(DateTime date1, DateTime date2) {
80                       
81                        if (date1 == null || date2 == null)
82                                return false;
83                       
84                        if (date1.getYear() != date2.getYear())
85                                return false;
86                       
87                        if (date1.getMonthOfYear() != date2.getMonthOfYear())
88                                return false;
89                       
90                        if (date1.getDayOfMonth() != date2.getDayOfMonth())
91                                return false;
92
93                        return true;
94                }
95
96                /**
97                 * Check whether one date is before another date. Only time is taken into account, i.e.
98                 * hour, minute, second, milis (without year, month, and day)
99                 * @param date1
100                 * @param date2
101                 * @return true if date1 is before date2
102                 */
103                public boolean beforeDayTime(DateTime date1, DateTime date2) {
104
105                        if (date1 == null || date2 == null)
106                                  return false;
107
108                        if(date1.getHourOfDay() < date2.getHourOfDay())
109                                return true;
110                        if(date1.getHourOfDay() > date2.getHourOfDay())
111                                return false;
112                       
113                        if(date1.getMinuteOfHour() < date2.getMinuteOfHour())
114                                return true;
115                        if(date1.getMinuteOfHour() > date2.getMinuteOfHour())
116                                return false;
117                       
118                        if(date1.getSecondOfMinute() < date2.getSecondOfMinute())
119                                return true;
120                        if(date1.getSecondOfMinute() > date2.getSecondOfMinute())
121                                return false;
122                       
123                        if(date1.getMillisOfSecond() < date2.getMillisOfSecond())
124                                return true;
125                        if(date1.getMillisOfSecond() > date2.getMillisOfSecond())
126                                return false;
127                       
128                        return false;
129                }
130
131                /**
132                 * Check whether a period, whose length is equal duration, is before specific date. Only time is taken into account, i.e.
133                 * hour, minute, second, milis (without year, month, and day)
134                 * @param date1
135                 * @param duration - duration of a period to be checked
136                 * @param date2
137                 * @return true if date1 + duration is before date2
138                 */
139                public boolean beforeDayTime(DateTime date1, long duration, DateTime date2) {
140                        if (date1 == null || date2 == null)
141                                  return false;
142
143                        date1 = date1.plus(duration + timeDelta);
144                       
145                        return beforeDate(date1, date2);
146                }
147
148                /**
149                 * Check whether dates are equal. Only time is taken into account, i.e.
150                 * hour, minute, second, milis (without year, month, and day)
151                 * @param date1
152                 * @param date2
153                 * @return true if dates are equal
154                 */
155                public boolean sameDayTime(DateTime date1, DateTime date2) {
156
157                        if (date1 == null || date2 == null)
158                                  return false;
159
160                        if (date1.getMillisOfDay() != date2.getMillisOfDay())
161                                return false;
162                       
163                        return true;
164                }
165               
166                /**
167                 * Check whether a period, whose length is equal duration, is after specific date. Only time is taken into account, i.e.
168                 * hour, minute, second, milis (without year, month, and day)
169                 * @param date1
170                 * @param date2
171                 * @return true if date1 is after date2
172                 */
173                public boolean afterDayTime(DateTime date1, DateTime date2) {
174                        return beforeDayTime(date2, date1);
175                }
176
177                /**
178                 * Check whether a period, whose length is equal duration, is after specific date. Only time is taken into account, i.e.
179                 * hour, minute, second, milis (without year, month, and day)
180                 * @param date1
181                 * @param duration - duration of a period to be checked
182                 * @param date2
183                 * @return true if date1 + duration is after date2
184                 */
185                public boolean afterDayTime(DateTime date1, long duration, DateTime date2) {
186
187                        if (date1 == null || date2 == null)
188                                  return false;
189
190                        date1 = date1.plus(duration + timeDelta);
191                       
192                        return afterDayTime(date1, date2);
193                }
194               
195                //end of comparisons
196               
197                //Operations on date and time
198
199                /**
200                 * Copy date from newDate DateTime object into date DateTime object.
201                 * Time (hour, minutes, seconds, milis) is not copied.
202                 * @return DateTime object representing new date
203                 */
204                public DateTime setDate(DateTime date, DateTime newDate) {
205                       
206                        if (date == null || newDate == null)
207                                return date;
208                       
209                        return new DateTime(newDate.getYear(),
210                                                                newDate.getMonthOfYear(),
211                                                                newDate.getDayOfMonth(),
212                                                                date.getHourOfDay(),
213                                                                date.getMinuteOfHour(),
214                                                                date.getSecondOfMinute(),
215                                                                date.getMillisOfSecond());
216                       
217                }
218               
219               
220                /**
221                 * Set time within a day from newTime Calendar object into date Calendar object.
222                 * Date (year, month, day) is not set.
223                 * @return Calendar object representing new time
224                 */
225                public DateTime setDayTime(DateTime date, DateTime newTime) {
226                       
227                        if (date == null || newTime == null)
228                                return date;
229                       
230                        return new DateTime(date.getYear(),
231                                                                date.getMonthOfYear(),
232                                                                date.getDayOfMonth(),
233                                                                newTime.getHourOfDay(),
234                                                                newTime.getMinuteOfHour(),
235                                                                newTime.getSecondOfMinute(),
236                                                                newTime.getMillisOfSecond());
237                       
238                }
239
240                //end of operations on date and time
241               
242               
243                //Operations on periods
244
245                /**
246                 * Calculate period duration in milis
247                 * @param start
248                 * @param end
249                 * @return period duration in milis
250                 */
251                public long periodDuration(DateTime start, DateTime end) {
252                       
253                        if (start == null || end == null)
254                                  return 0;
255                       
256                        return end.getMillis() - start.getMillis();
257                }
258
259                /**
260                 * Calculate period duration in milis taking into account time only, i.e.
261                 * hour, minute, second, milis (without year, month, and day)
262                 * @param start
263                 * @param end
264                 * @return period duration in milis
265                 */
266                public long periodDayTimeDuration(DateTime start, DateTime end) {
267
268                        if (start == null && end == null)
269                                  return TWENTY_FOUR_HOURS;
270
271                        if (start == null) {
272                                start = new DateTime(end.getYear(),
273                                                                        end.getMonthOfYear(),
274                                                                        end.getDayOfMonth(),
275                                                                        0, 0, 0, 0);
276                        }
277                       
278                        if (end == null) {
279                                end = new DateTime(start.getYear(),
280                                                                        start.getMonthOfYear(),
281                                                                        start.getDayOfMonth(),
282                                                                        23, 59, 59, 999);
283                        }       
284                       
285                        //setting the same date
286                        DateTime newEnd = new DateTime(start.getYear(),
287                                                                                        start.getMonthOfYear(),
288                                                                                        start.getDayOfMonth(),
289                                                                                        end.getHourOfDay(),
290                                                                                        end.getMinuteOfHour(),
291                                                                                        end.getSecondOfMinute(),
292                                                                                        end.getMillisOfSecond()
293                                                                                );
294
295                        if (beforeDayTime(start, newEnd))
296                                return newEnd.getMillis() - start.getMillis();
297                        else
298                                return TWENTY_FOUR_HOURS - (start.getMillis() - newEnd.getMillis());
299                }
300               
301                /**
302                 * Check whether date is before given period. Only time is taken into account, i.e.
303                 * hour, minute, second, milis (without year, month, and day)
304                 * @param date - date to be checked
305                 * @param start - begin of period
306                 * @param end - end of period
307                 * @return true if date is before a period; false otherwise
308                 */
309                public boolean beforePeriodDayTime(DateTime date, DateTime start, DateTime end) {
310
311                        if (date == null)
312                                return false;
313
314                        if (start == null)
315                                return false;
316                       
317                        if (end == null)
318                                return beforeDayTime(date, start);
319                       
320                        //date is not before start
321                        if(!beforeDayTime(date, start))
322                                return false;
323                       
324                        //end of period is next day
325                        if(afterDayTime(start, end))
326                                if(!afterDayTime(date, end))
327                                        return false;
328                       
329                        return true;
330                }
331               
332                /**
333                 * Check whether a period, whose length is given by duration, is within a period
334                 * defined by start and end.
335                 * @param date - date to be checked
336                 * @param duration - duration of a period to be checked
337                 * @param start - begin of period
338                 * @param end - end of period
339                 * @return true if a period is within the period <start, end>; false otherwise
340                 */
341                public boolean withinPeriod(DateTime date, long duration, DateTime start, DateTime end) {
342
343                        if (date == null)
344                                return false;
345
346                        if (start == null || end == null)
347                                return true;
348                 
349                        if (date.isBefore(start) || (date.getMillis() + duration + timeDelta > end.getMillis()))
350                                        return false;
351                       
352                    return true;
353                }
354
355                /**
356                 * Check whether a period, whose length is given by duration, is within a period
357                 * defined by start and end.
358                 * @param duration - duration of a period to be checked
359                 * @param start - begin of period
360                 * @param end - end of period
361                 * @return true if a period is within the period <start, end>; false otherwise
362                 */
363                public boolean withinPeriod(long duration, DateTime start, DateTime end) {
364
365                        if (start == null || end == null)
366                                return true;
367                 
368                        if (start.getMillis() + duration + timeDelta > end.getMillis())
369                                        return false;
370                       
371                    return true;
372                }
373               
374                /**
375                 * Check whether date is within a period
376                 * defined by start and end. Only time is taken into account, i.e.
377                 * hour, minute, second, milis (without year, month, and day)
378                 * @param date - date to be checked
379                 * @param start - begin of period
380                 * @param end - end of period
381                 * @return true if date is within the period <start, end>; false otherwise
382                 */
383                public boolean withinPeriodDayTime(DateTime date, DateTime start, DateTime end) {
384
385                        if (date == null)
386                                return false;
387
388                        if (start == null && end == null)
389                                return false;
390                       
391                        if (start == null)
392                                return !afterDayTime(date, end);
393                       
394                        if (end == null)
395                                return !beforeDayTime(date, start);
396
397                        if (afterDayTime(start, end)){
398                                if (beforeDayTime(date, start) && afterDayTime(date, end))
399                                        return false;
400                        }
401                        else {
402                                if (beforeDayTime(date, start) || afterDayTime(date, end))
403                                        return false;
404                        }
405                       
406                    return true;
407                }
408
409                /**
410                 * Check whether a period, whose length is given by duration, is within a period
411                 * defined by start and end. Only time is taken into account, i.e.
412                 * hour, minute, second, milis (without year, month, and day)
413                 * @param date - date to be checked
414                 * @param duration - duration of a period to be checked
415                 * @param start - begin of period
416                 * @param end - end of period
417                 * @return true if a period is within the period <start, end>; false otherwise
418                 */
419                public boolean withinPeriodDayTime(DateTime date, long duration, DateTime start, DateTime end) {
420
421                        if (date == null)
422                                return false;
423
424                        if (start == null && end == null)
425                                return true;
426                       
427                        if (start == null)
428                                return !afterDayTime(date, duration, end);
429                       
430                        if (end == null)
431                                return !beforeDayTime(date, duration, start);
432                       
433                        if (afterDayTime(start, end)){
434                                if (beforeDate(date, start) && afterDayTime(date, duration, end))
435                                        return false;
436                        }
437                        else {
438                                if (beforeDate(date, start) || afterDayTime(date, duration, end))
439                                        return false;
440                        }
441                       
442                    return true;
443                }
444
445                /**
446                 * Check whether a period, whose length is given by duration, overlaps a period
447                 * defined by start and end. Only time is taken into account, i.e.
448                 * hour, minute, second, milis (without year, month, and day)
449                 * @param date - date to be checked
450                 * @param duration - duration of a period to be checked
451                 * @param start - begin of period
452                 * @param end - end of period
453                 * @return true if a period overlaps with the period <start, end>; false otherwise
454                 */
455                public boolean overlapPeriodDayTime(DateTime date, long duration, DateTime start, DateTime end) {
456
457                        if (date == null)
458                                return false;
459
460                        if (start == null && end == null)
461                                return true;
462                       
463                        if (start == null)
464                                return beforeDayTime(date, end);
465                       
466                        if (end == null)
467                                return !beforeDayTime(date, duration, start);
468                       
469                        if (afterDayTime(start, end)){
470                                if (!beforeDate(date, end) && !afterDayTime(date, duration, start))
471                                        return false;
472                        }
473                        else {
474                                if (!beforeDate(date, end) || !afterDayTime(date, duration, start))
475                                        return false;
476                        }
477                       
478                    return true;
479                }
480               
481                /**
482                 * Check whether date is after given period. Only time is taken into account, i.e.
483                 * hour, minute, second, milis (without year, month, and day)
484                 * @param date - date to be checked
485                 * @param start - begin of period
486                 * @param end - end of period
487                 * @return true if date is after a period; false otherwise
488                 */
489                public boolean afterPeriodDayTime(DateTime date, DateTime start, DateTime end) {
490
491                        if (date == null)
492                                return false;
493
494                        if (end == null)
495                                return false;
496                       
497                        if (start == null)
498                                return afterDayTime(date, end);
499                       
500                        //date is not after end
501                        if (!afterDayTime(date, end))
502                                return false;
503                       
504                        //end of period is next day
505                        if (afterDayTime(start, end))
506                                if (!beforeDate(date, start))
507                                        return false;
508
509                        return true;
510                }
511
512                //end of operations on periods
513               
514               
515                //Operations on lists of dates
516
517                /**
518                 * Find date in list of dates
519                 * @param date - date to be found
520                 * @param days - Vector containing Calendar objects
521                 * @return true if data is found; false otherwise
522                 */
523                public boolean findDate(DateTime date, ArrayList<DateTime> days) {
524                   
525                    if (days == null)
526                      return false;
527                         
528                    for (int i=0; i < days.size(); i++) {
529                      DateTime vDate = days.get(i);
530                             
531                      if (!sameDate(date, vDate))
532                          continue;
533                                 
534                      return true;
535                    }
536                           
537                    return false;
538                  }
539
540                  /**
541                   * Find the latest date in inclDates that is not found in exclDates
542                   * @param inclDates
543                   * @param exclDates
544                   * @return the latest date
545                   */
546                  public DateTime findLatestDate(ArrayList <DateTime>inclDates, ArrayList<DateTime> exclDates) {
547                               
548                          if (inclDates == null)
549                                  return null;
550                         
551                          DateTime latest = new DateTime(); //Calendar.getInstance();
552                         
553                          for (int i=0; i < inclDates.size(); i++)
554                                if (beforeDate(latest, inclDates.get(i)))
555                                        if (!findDate(inclDates.get(i), exclDates))
556                                                latest = inclDates.get(i);
557                                       
558                          return latest;
559                  }
560                 
561                //end of operations on lists of dates
562                 
563                /**
564                 * Get DateTime object representing current time
565                 * This method can be overloaded to return logic time, e.g. in simulations
566                 * @return current time
567                 */
568                public DateTime getTime() {
569                        return new DateTime();
570                }
571
572}
573
Note: See TracBrowser for help on using the repository browser.