1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.criteria.impl

File PeriodFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

0
55
34
1
372
170
34
0.62
1.62
34
1

Classes

Class Line # Actions
PeriodFactory 30 55 0% 34 67
0.2471910124.7%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.criteria.impl;
21   
22    import org.joda.time.DateTime;
23    import org.joda.time.MutableDateTime;
24    import org.joda.time.format.DateTimeFormat;
25    import org.joda.time.format.DateTimeFormatter;
26   
27    /**
28    * Helper factory class for creating Period objects in velocity.
29    */
 
30    public class PeriodFactory
31    {
32    /**
33    * The minimum date considered when retrieving all-time statistics.
34    */
35    private static final DateTime MIN_DATE = new DateTime(1000, 1, 1, 0, 0, 0, 0);
36   
37    /**
38    * The maximum date considered when retrieving all-time statistics.
39    */
40    private static final DateTime MAX_DATE = new DateTime(9999, 12, 31, 23, 59, 59, 999);
41   
42    /**
43    * The period of time between {@link #MIN_DATE} and {@link #MAX_DATE}.
44    */
45    public static final Period ALL_TIME =
46    createPeriod(MIN_DATE.getMillis(), MAX_DATE.getMillis());
47   
48    private static final DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd");
49   
 
50  0 toggle public PeriodFactory()
51    {
52    }
53   
54    /**
55    * @see Period#Period(long, long)
56    */
 
57  20 toggle public static Period createPeriod(long start, long end)
58    {
59  20 return new Period(start, end);
60    }
61   
62    /**
63    * Creates a new custom period. The start and end dates must have the following format: "yyyyMMdd" .
64    *
65    * @param start The start date
66    * @param end The end date
67    * @return A new Period instance
68    * @see java.text.SimpleDateFormat
69    */
 
70  0 toggle public static Period createPeriod(String start, String end)
71    {
72  0 return createPeriod(formatter.parseMillis(start), formatter.parseMillis(end));
73    }
74   
75    /**
76    * Creates a new Period instance that matches exactly the day that includes the specified time stamp.
77    *
78    * @param timestamp The milliseconds from 1970-01-01T00:00:00Z
79    * @return A new Period instance
80    */
 
81  1 toggle public static Period createDayPeriod(long timestamp)
82    {
83  1 MutableDateTime mdt = new MutableDateTime(timestamp);
84  1 return createPeriod(toDayStart(mdt).getMillis(), toDayEnd(mdt).getMillis());
85    }
86   
87    /**
88    * Creates a new Period instance that matches exactly the day that includes the specified date.
89    *
90    * @param date The string representation of a date uniquely identifying a day. Use the "yyyyMMdd" format.
91    * @return The corresponding Period object
92    * @see java.text.SimpleDateFormat
93    */
 
94  0 toggle public static Period createDayPeriod(String date)
95    {
96  0 return createDayPeriod(formatter.parseMillis(date));
97    }
98   
99    /**
100    * Creates a new Period instance that matches exactly the hour that includes the specified time stamp.
101    *
102    * @param timestamp The milliseconds from 1970-01-01T00:00:00Z
103    * @return A new Period instance
104    */
 
105  0 toggle public static Period createHourPeriod(long timestamp)
106    {
107  0 MutableDateTime mdt = new MutableDateTime(timestamp);
108  0 return createPeriod(toHourStart(mdt).getMillis(), toHourEnd(mdt).getMillis());
109    }
110   
111    /**
112    * Creates a new Period instance that matches exactly the hour that includes the specified date.
113    *
114    * @param date The string representation of a date uniquely identifying a day. Use the "yyyyMMdd" format.
115    * @return The corresponding Period object
116    * @see java.text.SimpleDateFormat
117    */
 
118  0 toggle public static Period createHourPeriod(String date)
119    {
120  0 return createHourPeriod(formatter.parseMillis(date));
121    }
122   
123    /**
124    * Creates a new Period instance that matches all the instants between N hours before the instantiation and the
125    * instantiation.
126    *
127    * @param numberOfHours number of hours to substract from current date
128    * @return The corresponding period object
129    */
 
130  0 toggle public static Period createSinceHoursPeriod(int numberOfHours)
131    {
132  0 DateTime dt = new DateTime();
133  0 return createPeriod(dt.minusHours(numberOfHours).getMillis(), dt.getMillis());
134    }
135   
136    /**
137    * Creates a new Period instance that matches all the instants between N days before the instantiation and the
138    * instantiation.
139    *
140    * @param numberOfDays number of days to substract from current date
141    * @return The corresponding period object
142    */
 
143  0 toggle public static Period createSinceDaysPeriod(int numberOfDays)
144    {
145  0 DateTime dt = new DateTime();
146  0 return createPeriod(dt.minusDays(numberOfDays).getMillis(), dt.getMillis());
147    }
148   
149    /**
150    * Creates a new Period instance that matches all the instants between N weeks before the instantiation and the
151    * instantiation.
152    *
153    * @param numberOfWeeks number of weeks to substract from current date
154    * @return The corresponding period object
155    */
 
156  0 toggle public static Period createSinceWeeksPeriod(int numberOfWeeks)
157    {
158  0 DateTime dt = new DateTime();
159  0 return createPeriod(dt.minusWeeks(numberOfWeeks).getMillis(), dt.getMillis());
160    }
161   
162    /**
163    * Creates a new Period instance that matches all the instants between N months before the instantiation and the
164    * instantiation.
165    *
166    * @param numberOfMonths number of months to substract from current date
167    * @return The corresponding period object
168    */
 
169  0 toggle public static Period createSinceMonthsPeriod(int numberOfMonths)
170    {
171  0 DateTime dt = new DateTime();
172  0 return createPeriod(dt.minusMonths(numberOfMonths).getMillis(), dt.getMillis());
173    }
174   
175    /**
176    * Creates a new Period instance that matches all the instants between N years before the instantiation and the
177    * instantiation.
178    *
179    * @param numberOfYears number of years to substract from current date
180    * @return The corresponding period object
181    */
 
182  0 toggle public static Period createSinceYearsPeriod(int numberOfYears)
183    {
184  0 DateTime dt = new DateTime();
185  0 return createPeriod(dt.minusYears(numberOfYears).getMillis(), dt.getMillis());
186    }
187   
188    /**
189    * @return The period of time matching the current day
190    */
 
191  0 toggle public static Period getCurrentDay()
192    {
193  0 return createDayPeriod(new DateTime().getMillis());
194    }
195   
196    /**
197    * Creates a new Period instance that matches exactly the week that includes the specified time stamp.
198    *
199    * @param timestamp The milliseconds from 1970-01-01T00:00:00Z
200    * @return A new Period instance
201    */
 
202  0 toggle public static Period createWeekPeriod(long timestamp)
203    {
204  0 MutableDateTime mdt = new MutableDateTime(timestamp);
205  0 return createPeriod(toWeekStart(mdt).getMillis(), toWeekEnd(mdt).getMillis());
206    }
207   
208    /**
209    * Creates a new Period instance that matches exactly the week that includes the specified date.
210    *
211    * @param date The string representation of a date uniquely identifying a week. Use the "yyyyMMdd" format.
212    * @return The corresponding Period object
213    * @see java.text.SimpleDateFormat
214    */
 
215  0 toggle public static Period createWeekPeriod(String date)
216    {
217  0 return createWeekPeriod(formatter.parseMillis(date));
218    }
219   
220    /**
221    * @return The period of time matching the current week
222    */
 
223  0 toggle public static Period getCurrentWeek()
224    {
225  0 return createWeekPeriod(new DateTime().getMillis());
226    }
227   
228    /**
229    * Creates a new Period instance that matches exactly the month that includes the specified time stamp.
230    *
231    * @param timestamp The milliseconds from 1970-01-01T00:00:00Z
232    * @return A new Period instance
233    */
 
234  1 toggle public static Period createMonthPeriod(long timestamp)
235    {
236  1 MutableDateTime mdt = new MutableDateTime(timestamp);
237  1 return createPeriod(toMonthStart(mdt).getMillis(), toMonthEnd(mdt).getMillis());
238    }
239   
240    /**
241    * Creates a new Period instance that matches exactly the month that includes the specified date.
242    *
243    * @param date The string representation of a date uniquely identifying a month. Use the "yyyyMMdd" format.
244    * @return The corresponding Period object
245    * @see java.text.SimpleDateFormat
246    */
 
247  0 toggle public static Period createMonthPeriod(String date)
248    {
249  0 return createMonthPeriod(formatter.parseMillis(date));
250    }
251   
252    /**
253    * @return The period of time matching the current month
254    */
 
255  0 toggle public static Period getCurrentMonth()
256    {
257  0 return createMonthPeriod(new DateTime().getMillis());
258    }
259   
260    /**
261    * Creates a new Period instance that matches exactly the year that includes the specified time stamp.
262    *
263    * @param timestamp The milliseconds from 1970-01-01T00:00:00Z
264    * @return A new Period instance
265    */
 
266  0 toggle public static Period createYearPeriod(long timestamp)
267    {
268  0 MutableDateTime mdt = new MutableDateTime(timestamp);
269  0 return createPeriod(toYearStart(mdt).getMillis(), toYearEnd(mdt).getMillis());
270    }
271   
272    /**
273    * Creates a new Period instance that matches exactly the year that includes the specified date.
274    *
275    * @param date The string representation of a date uniquely identifying a year. Use the "yyyyMMdd" format.
276    * @return The corresponding Period object
277    * @see java.text.SimpleDateFormat
278    */
 
279  0 toggle public static Period createYearPeriod(String date)
280    {
281  0 return createYearPeriod(formatter.parseMillis(date));
282    }
283   
284    /**
285    * Creates a new Period instance that starts at the minimum value allowed by Date and ends at the maximum value
286    * allowed by Date.
287    *
288    * @return The corresponding Period object
289    */
 
290  14 toggle public static Period createMaximumPeriod()
291    {
292  14 return createPeriod(Long.MIN_VALUE, Long.MAX_VALUE);
293    }
294   
295    /**
296    * @return The period of time matching the current year
297    */
 
298  0 toggle public static Period getCurrentYear()
299    {
300  0 return createYearPeriod(new DateTime().getMillis());
301    }
302   
 
303  0 toggle private static MutableDateTime toHourStart(MutableDateTime mdt)
304    {
305  0 mdt.setMinuteOfHour(mdt.minuteOfHour().getMinimumValue());
306  0 mdt.setSecondOfMinute(mdt.secondOfMinute().getMinimumValue());
307  0 mdt.setMillisOfSecond(mdt.millisOfSecond().getMinimumValue());
308  0 return mdt;
309    }
310   
 
311  0 toggle private static MutableDateTime toHourEnd(MutableDateTime mdt)
312    {
313  0 mdt.addHours(1);
314  0 return toHourStart(mdt);
315    }
316   
 
317  4 toggle private static MutableDateTime toDayStart(MutableDateTime mdt)
318    {
319  4 mdt.setMillisOfDay(mdt.millisOfDay().getMinimumValue());
320  4 return mdt;
321    }
322   
 
323  2 toggle private static MutableDateTime toDayEnd(MutableDateTime mdt)
324    {
325  2 mdt.addDays(1);
326  2 return toDayStart(mdt);
327    }
328   
 
329  0 toggle private static MutableDateTime toWeekStart(MutableDateTime mdt)
330    {
331  0 mdt.setDayOfWeek(mdt.dayOfWeek().getMinimumValue());
332  0 return toDayStart(mdt);
333    }
334   
 
335  0 toggle private static MutableDateTime toWeekEnd(MutableDateTime mdt)
336    {
337  0 mdt.setDayOfWeek(mdt.dayOfWeek().getMaximumValue());
338  0 return toDayEnd(mdt);
339    }
340   
 
341  1 toggle private static MutableDateTime toMonthStart(MutableDateTime mdt)
342    {
343  1 mdt.setDayOfMonth(mdt.dayOfMonth().getMinimumValue());
344  1 return toDayStart(mdt);
345    }
346   
 
347  1 toggle private static MutableDateTime toMonthEnd(MutableDateTime mdt)
348    {
349  1 mdt.setDayOfMonth(mdt.dayOfMonth().getMaximumValue());
350  1 return toDayEnd(mdt);
351    }
352   
 
353  0 toggle private static MutableDateTime toYearStart(MutableDateTime mdt)
354    {
355  0 mdt.setDayOfYear(mdt.dayOfYear().getMinimumValue());
356  0 return toDayStart(mdt);
357    }
358   
 
359  0 toggle private static MutableDateTime toYearEnd(MutableDateTime mdt)
360    {
361  0 mdt.setDayOfYear(mdt.dayOfYear().getMaximumValue());
362  0 return toDayEnd(mdt);
363    }
364   
365    /**
366    * Helper method for accessing {@link #ALL_TIME} static field in velocity
367    */
 
368  0 toggle public static Period getALL_TIME()
369    {
370  0 return ALL_TIME;
371    }
372    }