1 | |
package org.apache.maven.plugin.version; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.util.Calendar; |
23 | |
import java.util.Date; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Map; |
26 | |
import java.util.regex.Matcher; |
27 | |
import java.util.regex.Pattern; |
28 | |
|
29 | |
public final class IntervalUtils |
30 | |
{ |
31 | |
|
32 | |
private static final String PERIOD_PART_PATTERN = "[0-9]+[WwDdHhMm]?"; |
33 | |
|
34 | |
private static final Map PART_TYPE_CONTRIBUTIONS; |
35 | |
|
36 | |
static |
37 | |
{ |
38 | 1 | Map contributions = new HashMap(); |
39 | |
|
40 | 1 | contributions.put( "w", new Long( 7 * 24 * 60 * 60 * 1000 ) ); |
41 | 1 | contributions.put( "d", new Long( 24 * 60 * 60 * 1000 ) ); |
42 | 1 | contributions.put( "h", new Long( 60 * 60 * 1000 ) ); |
43 | 1 | contributions.put( "m", new Long( 60 * 1000 ) ); |
44 | |
|
45 | 1 | PART_TYPE_CONTRIBUTIONS = contributions; |
46 | 1 | } |
47 | |
|
48 | |
private IntervalUtils() |
49 | 0 | { |
50 | |
|
51 | 0 | } |
52 | |
|
53 | |
public static boolean isExpired( String intervalSpec, Date lastChecked ) |
54 | |
{ |
55 | 3 | if( "never".equalsIgnoreCase( intervalSpec ) ) |
56 | |
{ |
57 | 1 | return false; |
58 | |
} |
59 | 2 | else if( "always".equalsIgnoreCase( intervalSpec ) ) |
60 | |
{ |
61 | 1 | return true; |
62 | |
} |
63 | 1 | else if( intervalSpec != null && intervalSpec.toLowerCase().startsWith("interval:") && intervalSpec.length() > "interval:".length()) |
64 | |
{ |
65 | 1 | String intervalPart = intervalSpec.substring( "interval:".length() ); |
66 | |
|
67 | |
|
68 | 1 | long period = IntervalUtils.parseInterval(intervalPart); |
69 | |
|
70 | 1 | Calendar cal = Calendar.getInstance(); |
71 | |
|
72 | 1 | cal.setTimeInMillis( System.currentTimeMillis() - period ); |
73 | |
|
74 | 1 | Date test = cal.getTime(); |
75 | |
|
76 | 1 | return lastChecked == null || test.after( lastChecked ); |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 0 | throw new IllegalArgumentException( "Invalid interval specification: \'" + intervalSpec + "\'" ); |
81 | |
} |
82 | |
} |
83 | |
|
84 | |
public static long parseInterval( String interval ) |
85 | |
{ |
86 | 11 | Matcher partMatcher = Pattern.compile(PERIOD_PART_PATTERN).matcher(interval); |
87 | |
|
88 | 11 | long period = 0; |
89 | |
|
90 | 31 | while( partMatcher.find() ) |
91 | |
{ |
92 | 20 | String part = partMatcher.group(); |
93 | |
|
94 | 20 | period += getPartPeriod( part ); |
95 | |
} |
96 | |
|
97 | 11 | return period; |
98 | |
} |
99 | |
|
100 | |
private static long getPartPeriod( String part ) |
101 | |
{ |
102 | 20 | char type = part.charAt( part.length() - 1 ); |
103 | |
|
104 | |
String coefficientPart; |
105 | |
|
106 | 20 | if( Character.isLetter(type)) |
107 | |
{ |
108 | 20 | coefficientPart = part.substring( 0, part.length() - 1); |
109 | |
} |
110 | |
else |
111 | |
{ |
112 | |
|
113 | 0 | coefficientPart = part; |
114 | |
|
115 | 0 | type = 'm'; |
116 | |
} |
117 | |
|
118 | 20 | int coefficient = Integer.parseInt( coefficientPart ); |
119 | |
|
120 | 20 | Long period = (Long) PART_TYPE_CONTRIBUTIONS.get( "" + Character.toLowerCase( type ) ); |
121 | |
|
122 | 20 | long result = 0; |
123 | |
|
124 | 20 | if( period != null ) |
125 | |
{ |
126 | 20 | result = coefficient * period.longValue(); |
127 | |
} |
128 | |
|
129 | 20 | return result; |
130 | |
} |
131 | |
|
132 | |
} |