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 Map contributions = new HashMap();
39
40 contributions.put( "w", new Long( 7 * 24 * 60 * 60 * 1000 ) );
41 contributions.put( "d", new Long( 24 * 60 * 60 * 1000 ) );
42 contributions.put( "h", new Long( 60 * 60 * 1000 ) );
43 contributions.put( "m", new Long( 60 * 1000 ) );
44
45 PART_TYPE_CONTRIBUTIONS = contributions;
46 }
47
48 private IntervalUtils()
49 {
50
51 }
52
53 public static boolean isExpired( String intervalSpec, Date lastChecked )
54 {
55 if( "never".equalsIgnoreCase( intervalSpec ) )
56 {
57 return false;
58 }
59 else if( "always".equalsIgnoreCase( intervalSpec ) )
60 {
61 return true;
62 }
63 else if( intervalSpec != null && intervalSpec.toLowerCase().startsWith("interval:") && intervalSpec.length() > "interval:".length())
64 {
65 String intervalPart = intervalSpec.substring( "interval:".length() );
66
67
68 long period = IntervalUtils.parseInterval(intervalPart);
69
70 Calendar cal = Calendar.getInstance();
71
72 cal.setTimeInMillis( System.currentTimeMillis() - period );
73
74 Date test = cal.getTime();
75
76 return lastChecked == null || test.after( lastChecked );
77 }
78 else
79 {
80 throw new IllegalArgumentException( "Invalid interval specification: \'" + intervalSpec + "\'" );
81 }
82 }
83
84 public static long parseInterval( String interval )
85 {
86 Matcher partMatcher = Pattern.compile(PERIOD_PART_PATTERN).matcher(interval);
87
88 long period = 0;
89
90 while( partMatcher.find() )
91 {
92 String part = partMatcher.group();
93
94 period += getPartPeriod( part );
95 }
96
97 return period;
98 }
99
100 private static long getPartPeriod( String part )
101 {
102 char type = part.charAt( part.length() - 1 );
103
104 String coefficientPart;
105
106 if( Character.isLetter(type))
107 {
108 coefficientPart = part.substring( 0, part.length() - 1);
109 }
110 else
111 {
112
113 coefficientPart = part;
114
115 type = 'm';
116 }
117
118 int coefficient = Integer.parseInt( coefficientPart );
119
120 Long period = (Long) PART_TYPE_CONTRIBUTIONS.get( "" + Character.toLowerCase( type ) );
121
122 long result = 0;
123
124 if( period != null )
125 {
126 result = coefficient * period.longValue();
127 }
128
129 return result;
130 }
131
132 }