Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
Period | 30 | 8 | 0% | 7 | 0 |
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.Interval; | |
23 | import org.joda.time.format.DateTimeFormat; | |
24 | import org.joda.time.format.DateTimeFormatter; | |
25 | ||
26 | /** | |
27 | * Immutable period for retrieving statistics. A period of time is uniquely identified by its bounds, and not by its | |
28 | * span. Two periods of time with the same span are different if they don't start at the same time. | |
29 | */ | |
30 | public class Period | |
31 | { | |
32 | /** | |
33 | * Formatter associated with periods that have less than a month | |
34 | */ | |
35 | private static final DateTimeFormatter DAY_PERIOD_FORMATTER = | |
36 | DateTimeFormat.forPattern("yyyyMMdd"); | |
37 | ||
38 | /** | |
39 | * Formatter associated with periods that have at least one month | |
40 | */ | |
41 | private static final DateTimeFormatter MONTH_PERIOD_FORMATTER = | |
42 | DateTimeFormat.forPattern("yyyyMM"); | |
43 | ||
44 | private Interval interval; | |
45 | ||
46 | /** | |
47 | * Creates a new time Period from the specified start time to the specified end time. Both ends of the period are | |
48 | * given as time stamps (the number of milliseconds from 1970-01-01T00:00:00Z) | |
49 | * | |
50 | * @param start The period start as the number of milliseconds from 1970-01-01T00:00:00Z | |
51 | * @param end The period end as the number of milliseconds from 1970-01-01T00:00:00Z | |
52 | */ | |
53 | 20 | public Period(long start, long end) |
54 | { | |
55 | 20 | this.interval = new Interval(start, end); |
56 | } | |
57 | ||
58 | /** | |
59 | * @return The period start as the number of milliseconds from 1970-01-01T00:00:00Z | |
60 | */ | |
61 | 22 | public long getStart() |
62 | { | |
63 | 22 | return this.interval.getStartMillis(); |
64 | } | |
65 | ||
66 | /** | |
67 | * @return The period end as the number of milliseconds from 1970-01-01T00:00:00Z | |
68 | */ | |
69 | 22 | public long getEnd() |
70 | { | |
71 | 22 | return this.interval.getEndMillis(); |
72 | } | |
73 | ||
74 | /** | |
75 | * @return The start of the period formatted with the associated formatter. Usually this is how the start of the | |
76 | * period is stored in the database. | |
77 | * @see #getFormatter() | |
78 | */ | |
79 | 2 | public int getStartCode() |
80 | { | |
81 | 2 | return Integer.parseInt(getFormatter().print(this.interval.getStart())); |
82 | } | |
83 | ||
84 | /** | |
85 | * @return The end of the period formatted with the associated formatter. Usually this is how the end of the period | |
86 | * is stored in the database. | |
87 | * @see #getFormatter() | |
88 | */ | |
89 | 2 | public int getEndCode() |
90 | { | |
91 | 2 | return Integer.parseInt(getFormatter().print(this.interval.getEnd())); |
92 | } | |
93 | ||
94 | /** | |
95 | * @return The formatter associated with this Period instance | |
96 | * @see #DAY_PERIOD_FORMATTER | |
97 | * @see #MONTH_PERIOD_FORMATTER | |
98 | */ | |
99 | 4 | private DateTimeFormatter getFormatter() |
100 | { | |
101 | 4 | if (this.interval.toPeriod().getMonths() >= 1) { |
102 | 2 | return MONTH_PERIOD_FORMATTER; |
103 | } | |
104 | 2 | return DAY_PERIOD_FORMATTER; |
105 | } | |
106 | } |