View Javadoc

1   package org.apache.maven.plugin.version;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // don't allow construction
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              // subtract the specified period from now() and see if it's still after the lastChecked date.
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             // if the interval doesn't specify a resolution, assume minutes.
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 }