KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > version > IntervalUtils


1 package org.apache.maven.plugin.version;
2
3 import java.util.Calendar JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.regex.Matcher JavaDoc;
8 import java.util.regex.Pattern JavaDoc;
9
10 /*
11  * Copyright 2001-2005 The Apache Software Foundation.
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  */

25
26 public final class IntervalUtils
27 {
28     
29     private static final String JavaDoc PERIOD_PART_PATTERN = "[0-9]+[WwDdHhMm]?";
30     
31     private static final Map JavaDoc PART_TYPE_CONTRIBUTIONS;
32     
33     static
34     {
35         Map JavaDoc contributions = new HashMap JavaDoc();
36         
37         contributions.put( "w", new Long JavaDoc( 7 * 24 * 60 * 60 * 1000 ) );
38         contributions.put( "d", new Long JavaDoc( 24 * 60 * 60 * 1000 ) );
39         contributions.put( "h", new Long JavaDoc( 60 * 60 * 1000 ) );
40         contributions.put( "m", new Long JavaDoc( 60 * 1000 ) );
41         
42         PART_TYPE_CONTRIBUTIONS = contributions;
43     }
44     
45     private IntervalUtils()
46     {
47         // don't allow construction
48
}
49     
50     public static boolean isExpired( String JavaDoc intervalSpec, Date JavaDoc lastChecked )
51     {
52         if( "never".equalsIgnoreCase( intervalSpec ) )
53         {
54             return false;
55         }
56         else if( "always".equalsIgnoreCase( intervalSpec ) )
57         {
58             return true;
59         }
60         else if( intervalSpec != null && intervalSpec.toLowerCase().startsWith("interval:") && intervalSpec.length() > "interval:".length())
61         {
62             String JavaDoc intervalPart = intervalSpec.substring( "interval:".length() );
63             
64             // subtract the specified period from now() and see if it's still after the lastChecked date.
65
long period = IntervalUtils.parseInterval(intervalPart);
66             
67             Calendar JavaDoc cal = Calendar.getInstance();
68             
69             cal.setTimeInMillis( System.currentTimeMillis() - period );
70             
71             Date JavaDoc test = cal.getTime();
72             
73             return lastChecked == null || test.after( lastChecked );
74         }
75         else
76         {
77             throw new IllegalArgumentException JavaDoc( "Invalid interval specification: \'" + intervalSpec + "\'" );
78         }
79     }
80     
81     public static long parseInterval( String JavaDoc interval )
82     {
83         Matcher JavaDoc partMatcher = Pattern.compile(PERIOD_PART_PATTERN).matcher(interval);
84         
85         long period = 0;
86         
87         while( partMatcher.find() )
88         {
89             String JavaDoc part = partMatcher.group();
90             
91             period += getPartPeriod( part );
92         }
93         
94         return period;
95     }
96
97     private static long getPartPeriod( String JavaDoc part )
98     {
99         char type = part.charAt( part.length() - 1 );
100         
101         String JavaDoc coefficientPart;
102         
103         if( Character.isLetter(type))
104         {
105             coefficientPart = part.substring( 0, part.length() - 1);
106         }
107         else
108         {
109             // if the interval doesn't specify a resolution, assume minutes.
110
coefficientPart = part;
111             
112             type = 'm';
113         }
114         
115         int coefficient = Integer.parseInt( coefficientPart );
116         
117         Long JavaDoc period = (Long JavaDoc) PART_TYPE_CONTRIBUTIONS.get( "" + Character.toLowerCase( type ) );
118         
119         long result = 0;
120         
121         if( period != null )
122         {
123             result = coefficient * period.longValue();
124         }
125         
126         return result;
127     }
128
129 }
130
Popular Tags