1 package org.apache.maven.plugin.version; 2 3 import java.util.Calendar ; 4 import java.util.Date ; 5 import java.util.HashMap ; 6 import java.util.Map ; 7 import java.util.regex.Matcher ; 8 import java.util.regex.Pattern ; 9 10 25 26 public final class IntervalUtils 27 { 28 29 private static final String PERIOD_PART_PATTERN = "[0-9]+[WwDdHhMm]?"; 30 31 private static final Map PART_TYPE_CONTRIBUTIONS; 32 33 static 34 { 35 Map contributions = new HashMap (); 36 37 contributions.put( "w", new Long ( 7 * 24 * 60 * 60 * 1000 ) ); 38 contributions.put( "d", new Long ( 24 * 60 * 60 * 1000 ) ); 39 contributions.put( "h", new Long ( 60 * 60 * 1000 ) ); 40 contributions.put( "m", new Long ( 60 * 1000 ) ); 41 42 PART_TYPE_CONTRIBUTIONS = contributions; 43 } 44 45 private IntervalUtils() 46 { 47 } 49 50 public static boolean isExpired( String intervalSpec, Date 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 intervalPart = intervalSpec.substring( "interval:".length() ); 63 64 long period = IntervalUtils.parseInterval(intervalPart); 66 67 Calendar cal = Calendar.getInstance(); 68 69 cal.setTimeInMillis( System.currentTimeMillis() - period ); 70 71 Date test = cal.getTime(); 72 73 return lastChecked == null || test.after( lastChecked ); 74 } 75 else 76 { 77 throw new IllegalArgumentException ( "Invalid interval specification: \'" + intervalSpec + "\'" ); 78 } 79 } 80 81 public static long parseInterval( String interval ) 82 { 83 Matcher partMatcher = Pattern.compile(PERIOD_PART_PATTERN).matcher(interval); 84 85 long period = 0; 86 87 while( partMatcher.find() ) 88 { 89 String part = partMatcher.group(); 90 91 period += getPartPeriod( part ); 92 } 93 94 return period; 95 } 96 97 private static long getPartPeriod( String part ) 98 { 99 char type = part.charAt( part.length() - 1 ); 100 101 String coefficientPart; 102 103 if( Character.isLetter(type)) 104 { 105 coefficientPart = part.substring( 0, part.length() - 1); 106 } 107 else 108 { 109 coefficientPart = part; 111 112 type = 'm'; 113 } 114 115 int coefficient = Integer.parseInt( coefficientPart ); 116 117 Long period = (Long ) 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 |