class ConfigHelper {

  public static long parseDuration(String durationStr, TimeUnit defaultUnit) throws InvalidConfigException
  {
    if (null == durationStr) return 0;
    TimeUnit unit = defaultUnit;
    Matcher m = DURATION_PATTERN.matcher(durationStr);
    if (!m.matches()) throw new InvalidConfigException("invalid duration string: " + durationStr);
    if (1 < m.groupCount() && null != m.group(2) && 0 < m.group(2).length())
    {
      char unitChar1 = m.group(2).charAt(0);
      switch (unitChar1)
      {
      case 'n':
      case 'N': unit = TimeUnit.NANOSECONDS; break;
      case 'u':
      case 'U': unit = TimeUnit.MICROSECONDS; break;
      case 'm':
      case 'M':
        char unitChar3 = m.group(2).length() >= 3 ? m.group(2).charAt(2) : ' ';
        unit = ('n' == unitChar3 || 'N' == unitChar3) ? TimeUnit.MINUTES : TimeUnit.MILLISECONDS;
        break;
      case 's':
      case 'S': unit = TimeUnit.SECONDS; break;
      case 'h':
      case 'H': unit = TimeUnit.HOURS; break;
      case 'd':
      case 'D': unit = TimeUnit.DAYS; break;
      }
    }

    long value = Long.parseLong(m.group(1));
    return defaultUnit.convert(value, unit);
  }

}