Coverage Report - org.apache.maven.plugin.version.IntervalUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
IntervalUtils
87 %
34/39
65 %
13/20
3,5
 
 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  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  
         // don't allow construction
 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  
             // subtract the specified period from now() and see if it's still after the lastChecked date.
 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  20
         }
 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  
             // if the interval doesn't specify a resolution, assume minutes.
 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  
 }