1 package com.google.code.jetm.maven.data; 2 3 import com.google.code.jetm.maven.TimingReportMojo; 4 5 /** 6 * Enumerations of the time units supported by this report. 7 * 8 * @author jrh3k5 9 * 10 */ 11 12 public enum TimeUnit { 13 /** 14 * Milliseconds. 15 */ 16 MILLISECONDS("ms", 1), 17 /** 18 * Seconds 19 */ 20 SECONDS("sec", 1000); 21 22 /** 23 * Get a time unit from the appropriate {@link TimingReportMojo} abbreviate. The comparison is case-insensitive. 24 * 25 * @param abbreviation 26 * The abbreviation from which to obtain the corresponding enumeration. 27 * @return A {@link TimeUnit} enumeration corresponding to the given abbreviation: 28 * <ul> 29 * <li><b>MILLIS</b>: returns {@link #MILLISECONDS}.</li> 30 * <li><b>SECS</b>: returns {@link #SECONDS}</li> 31 * @throws IllegalArgumentException 32 * If the given abbreviation is not known. 33 */ 34 public static TimeUnit fromMojoAbbreviation(String abbreviation) { 35 if ("SECS".equalsIgnoreCase(abbreviation)) 36 return SECONDS; 37 else if ("MILLIS".equalsIgnoreCase(abbreviation)) 38 return MILLISECONDS; 39 40 throw new IllegalArgumentException("Unrecognized time unit abbreviation: " + abbreviation); 41 } 42 43 private final long division; 44 private final String displayName; 45 46 /** 47 * Create a time unit enumeration. 48 * 49 * @param displayName 50 * The name to be displayed on the reports. 51 * @param division 52 * The amount by which microseconds are to be divided in order to convert the microseconds into this unit's measurement. 53 */ 54 private TimeUnit(String displayName, long division) { 55 this.displayName = displayName; 56 this.division = division; 57 } 58 59 /** 60 * Convert microseconds to this unit's value. 61 * 62 * @param micros 63 * The number of microseconds. 64 * @return The given number of microseconds in this unit's measurement. 65 */ 66 public double fromMilliseconds(double micros) { 67 return micros / division; 68 } 69 70 /** 71 * Get the display name of this time unit. 72 * 73 * @return The display name of the time unit. 74 */ 75 public String getDisplayName() { 76 return displayName; 77 } 78 79 /** 80 * Get the value by which this unit divides in order to convert from milliseconds to this unit. 81 * 82 * @return The division value. 83 */ 84 public long getDivisionValue() { 85 return division; 86 } 87 }